Skip to content

Commit 41d76b4

Browse files
committed
[Bit Manipulation] Reverse bits of a 32-bit unsigned integer
1 parent 2a74cd4 commit 41d76b4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: BitManipulation/reverse_bits/reverseBits.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
uint32_t reverseBits(uint32_t n) {
5+
int retval = 0;
6+
7+
for (int i = 0; i < 32; i++) {
8+
retval <<= 1;
9+
retval |= (n & 1);
10+
n >>= 1;
11+
}
12+
13+
return retval;
14+
}
15+
16+
int main() {
17+
// (Input) 12345678: 00000000 10111100 01100001 01001110
18+
// (Output) 1921400064: 01110010 10000110 00111101 00000000
19+
cout << reverseBits(12345678) << endl;
20+
return 0;
21+
}

0 commit comments

Comments
 (0)