1
public class Solution {
2
// you need treat n as an unsigned value
3
public int reverseBits(int n) {
4
int mask = 0;
5
int smask = 0;
6
int j = 0;
7
int rev = 0;
8

9
// basically we are checking that the number is set bit or not
10
// if the number is set bit then we are appending that to our main answer i.e, rev
11
for (int i = 31; i >= 0; i--) {
12
mask = 1 << i;
13
if ((mask & n) != 0) {
14
smask = 1 << j;
15
rev = rev | smask;
16
}
17
j++;
18
}
19

20
// Time Complexity : O(32 for int)
21
// Space Complexity : O(1)
22
return rev;
23
}
24
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0