—
i
Treat the 32-bit integer as a fixed-width bit string and reverse it: peel the least-significant bit one iteration at a time, shifting it into the result from the left, until all 32 positions are filled.
Given a 32-bit unsigned integer, return the integer whose binary representation is the bit-reversal of the input's 32-bit binary representation. n = 43261596 in binary is 00000010100101000001111010011100; reversed it is 00111001011110000010100101000000 = 964176192. Bit 0 (LSB) swaps with bit 31, bit 1 swaps with bit 30, and so on.
n & 1, shift the result left by one (making room), OR the bit in, then unsigned-right-shift the input to consume it. After exactly 32 iterations, the result holds the complete reversal.result = 0. We will accumulate the reversed bits here.bit = n & 1. This isolates the rightmost bit — the one that should move to the other end.result = (result << 1) | bit. The left-shift makes room for the new bit; the OR places it at the newly opened position.n >>>= 1. Unsigned right-shift discards the processed LSB without sign-extending.result >>> 0 to coerce back to an unsigned 32-bit value (JavaScript shifts can produce signed results).<< and >> operators treat their operands as signed 32-bit integers. Always use >>> (unsigned right-shift) when working with unsigned bit patterns, and apply >>> 0 on the final result to convert to unsigned.Both are O(1) since the bit width is fixed at 32. The bit-manipulation version is the expected answer in interviews and avoids string allocation entirely.
43261596 (decimal) = 00000010100101000001111010011100 (32-bit binary). We will read each bit LSB-first and shift it into the result from the left, 32 times.function reverseBits(n: number): number {
let result = 0;
for (let i = 0; i < 32; i++) {
const bit = n & 1; // extract the LSB
result = (result << 1) | bit; // shift result left, insert bit
n >>>= 1; // unsigned right-shift the input
}
return result >>> 0; // coerce to unsigned 32-bit
}result << 1 | bit), which corresponds to prepending on the left of the final reversed view.n & 1 masks everything but the lowest bit. Result is 0 or 1. This is the bit that will migrate to the mirror position in the output.result << 1 makes room on the right; | bitfills that position with the extracted LSB. Over 32 iterations this streams all bits from the input's LSB into the result's MSB-first order.n >>>= 1 is the unsigned right-shift: it shifts zero into the sign bit rather than replicating it. Always prefer >>> over >> for unsigned bit patterns in JavaScript.>>> 0 to the result normalizes it to a 32-bit unsigned integer. JavaScript bitwise ops return signed values; without this step a high bit set would produce a negative decimal result.n = ((n & 0xAAAAAAAA) >>> 1) | ((n & 0x55555555) << 1) swaps adjacent bits; a similar pair of masks with shift 2 swaps pairs; shift 4 swaps nibbles; shift 8 swaps bytes; shift 16 swaps halfwords. Five operations total — elegant and worth knowing for advanced follow-up discussions.>> sign-extends; for unsigned 32-bit work, >>> fills with zeros and keeps the semantics correct.00000000 00000000 00000000 00000001 reversed is 10000000 00000000 00000000 00000000 = 2147483648 — a good mental check.| "reverse bits of a 32-bit integer" | 32-iter loop: peel LSB, shift left into result |
| bit mirror / bit reflection | (result << 1) | (n & 1), n >>>= 1 |
| unsigned integer needed in JS/TS | always use >>> 0 or >>>= 1 |
| "reverse bits many times / cache" | byte-lookup table: 256 entries, split 32-bit into 4 bytes |
function reverseBits(n: number): number {
let result = 0;
for (let i = 0; i < 32; i++) {
const bit = n & 1;
result = (result << 1) | bit;
n >>>= 1;
}
return result >>> 0;
}n & 1 — AND with 1 isolates the lowest bit.n & 1 compute?result = (result << 1) | bit doing?n = 43261596?>>> 0 to the final result?