-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15_reverse-bits.cpp
41 lines (34 loc) · 1.03 KB
/
15_reverse-bits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// DATE: 28-July-2023
/* PROGRAM: 15_Binary - Reverse Bits
https://leetcode.com/problems/reverse-bits/
*/
// @ankitsamaddar @July_2023
#include <bitset>
#include <cstdint>
#include <iostream>
using namespace std;
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; i++) { // iterate for 32 bits
// i = 1
// 1010 >> i = 0101; left shift by i to get it to LSB
// 0101 & 1 = 0001 bitwise AND with 1 extracts the last bit
// Shift the bit left i times to get it to LSB
// bitwise AND with 1 to extract it
int bit = (n >> i) & 1;
// right shift bit to get to MSB
// bitwise OR to add it to the result
res = res | (bit << (31 - i));
}
return res;
}
};
int main() {
uint32_t n = 0b00000010100101000001111010011100; // should add 0b to specify its a binary number
Solution sol;
uint32_t res = sol.reverseBits(n);
cout << res << " " << bitset<32>(res) << endl; // bitset to print in binary with 32 bits
return 0;
}