1. All 32 bits
Process every bit of a 32-bit integer.
for (let i = 0; i < 32; i += 1)Typorithm · easy
Step 1 of 8 · intro card
Bit Manipulation
Pull n's lowest bit and push it into result, shifting both, for all 32 bits.
Reversing bits builds the output one bit at a time from the input's low end.
Each step shifts result left to make room, then ORs in the lowest bit of n.
Using the unsigned shift >>> avoids JavaScript's signed 32-bit quirks.
Shift result left, OR in n & 1.
Use >>> for unsigned shifts.
Run exactly 32 times.
function reverseBits(n) {
let result = 0;
for (let i = 0; i < 32; i += 1) {
result = (result << 1) | (n & 1);
n = n >>> 1;
}
return result >>> 0;
}Connect each part of the code to the concept before you type.
Process every bit of a 32-bit integer.
for (let i = 0; i < 32; i += 1)Make room in result, then append n's lowest bit.
result = (result << 1) | (n & 1)Unsigned right shift discards the bit just read.
n = n >>> 1>>> 0 reinterprets the result as an unsigned 32-bit value.
return result >>> 0Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputUnsigned n = 13 · 32-bit word
Follow temporary values, local state and supporting structures at this step.
Result word32 bits
Local state · indices and saved values
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state