Skip to content

Commit de6f2b8

Browse files
Merge pull request matthewsamuel95#141 from shivansh/reverseBits
[Bit Manipulation] Reverse bits of a 32-bit unsigned integer
2 parents d587dc3 + 41d76b4 commit de6f2b8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
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)