We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b57c526 commit da5c5c6Copy full SHA for da5c5c6
reverse-bits/Zioq.js
@@ -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