Skip to content

Commit da5c5c6

Browse files
Jaehyeon Robert HanJaehyeon Robert Han
authored andcommitted
reverse-bit solution
1 parent b57c526 commit da5c5c6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

reverse-bits/Zioq.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number} n - a positive integer
3+
* @return {number} - a positive integer
4+
*/
5+
var reverseBits = function(n) {
6+
let result = 0; //Initial value
7+
for (let i=0; i < 32; i++) { //The loop iterates 32 times, as the input n is a 32-bit unsigned integer
8+
result = (result << 1) | (n & 1); // Shift the result to the left by 1 bit OR it with the least significant bit of n.
9+
n >>= 1; // Shifts the bits of n one place to the right, effectively "removing" the processed LSB.
10+
}
11+
return result >>> 0;
12+
};
13+
/*
14+
Time Complexity: O(1), because we always loop exactly 32 times, regardless of the input.
15+
Space Complexity: O(1), because we use a constant amount of space.
16+
*/
17+
18+
19+

0 commit comments

Comments
 (0)