Skip to content

Commit b49ef16

Browse files
committed
🚀 13-Jul-2020
1 parent c2a783e commit b49ef16

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Reverse bits of a given 32 bits unsigned integer.
2+
3+
4+
5+
Example 1:
6+
7+
Input: 00000010100101000001111010011100
8+
Output: 00111001011110000010100101000000
9+
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
10+
Example 2:
11+
12+
Input: 11111111111111111111111111111101
13+
Output: 10111111111111111111111111111111
14+
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.
15+
16+
17+
Note:
18+
19+
Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
20+
In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above the input represents the signed integer -3 and the output represents the signed integer -1073741825.
21+
22+
23+
Follow up:
24+
25+
If this function is called many times, how would you optimize it?
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
class Solution {
36+
public:
37+
uint32_t reverseBits(uint32_t n) {
38+
int res=0;
39+
for(int i=0;i<32;i++){
40+
int lsb= n & 1; // check if lsb is set or not
41+
int rev = lsb << 31-i; // left shift set bit to place it into its correct reverse position
42+
res = res | rev;
43+
n = n>> 1;
44+
}
45+
return res;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Reverse bits of a given 32 bits unsigned integer.
2+
3+
4+
5+
Example 1:
6+
7+
Input: 00000010100101000001111010011100
8+
Output: 00111001011110000010100101000000
9+
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
10+
Example 2:
11+
12+
Input: 11111111111111111111111111111101
13+
Output: 10111111111111111111111111111111
14+
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.
15+
16+
17+
Note:
18+
19+
Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
20+
In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above the input represents the signed integer -3 and the output represents the signed integer -1073741825.
21+
22+
23+
Follow up:
24+
25+
If this function is called many times, how would you optimize it?
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
class Solution {
36+
public:
37+
uint32_t reverseBits(uint32_t n) {
38+
int res=0;
39+
for(int i=0;i<32;i++){
40+
int lsb= n & 1; // check if lsb is set or not
41+
int rev = lsb << 31-i; // left shift set bit to place it into its correct reverse position
42+
res = res | rev;
43+
n = n>> 1;
44+
}
45+
return res;
46+
}
47+
};

0 commit comments

Comments
 (0)