-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathreverseBits.js
More file actions
30 lines (26 loc) · 665 Bytes
/
reverseBits.js
File metadata and controls
30 lines (26 loc) · 665 Bytes
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
// TC:O(32) ~ O(1) SC:O(1)
function reverseBits1(num) {
let reverseBitResult = 0;
for(let i=0; i<32; i++) {
let bit = (num >> i) & 1;
if( bit === 1) {
reverseBitResult = reverseBitResult | bit << 31-i;
}
}
return reverseBitResult;
}
function reverseBits2(num) {
let reverseBitResult = 0;
for(let i=0; i< 32; i++) {
reverseBitResult <<= 1;
reverseBitResult |= (1 & num);
num >>= 1;
}
return reverseBitResult;
}
let num1 = 4;
console.log(reverseBits1(num1));
console.log(reverseBits2(num1));
let num2 = 8;
console.log(reverseBits1(num2));
console.log(reverseBits2(num2));