What does it mean to reverse the bits of a number?
Reversing the bits means flipping the order of all bits in the binary form of the number, so the first bit becomes the last, the second becomes the second last, and so on.
Click to reveal answer
beginner
Why do we use bitwise operations to reverse bits?
Bitwise operations let us access and change individual bits quickly and efficiently, which is necessary to reverse bits without converting to strings or arrays.
Click to reveal answer
beginner
In JavaScript, which operator is used to shift bits to the right?
The right shift operator is >> for signed shift and >>> for unsigned shift, which moves bits to the right, dropping bits on the right and filling on the left.
Click to reveal answer
beginner
What is the purpose of using a mask like 1 (binary 0001) in bit reversal?
The mask 1 helps to check the least significant bit (rightmost bit) by using bitwise AND (&). It tells if that bit is 0 or 1.
Click to reveal answer
intermediate
How do you build the reversed number step by step?
Start with 0. For each bit in the original number, shift the reversed number left by 1, then add the extracted bit from the original number. Repeat until all bits are processed.
Click to reveal answer
What is the first step when reversing bits of a number?
AConvert the number to decimal
BAdd 1 to the number
CExtract the least significant bit
DShift the number left
✗ Incorrect
We start by extracting the least significant bit to know what bit to add to the reversed number.
Which operator shifts bits to the left in JavaScript?
A>>
B|
C&
D<<
✗ Incorrect
The << operator shifts bits to the left, adding zeros on the right.
If the original number is 13 (binary 1101), what is the reversed 4-bit number?
A1011 (11 decimal)
B1110 (14 decimal)
C1101 (13 decimal)
D1001 (9 decimal)
✗ Incorrect
Reversing 1101 gives 1011, which is 11 in decimal.
Why do we shift the reversed number left before adding the new bit?
ATo remove the last bit
BTo make space for the new bit on the right
CTo convert to decimal
DTo reset the number
✗ Incorrect
Shifting left moves bits left, making space on the right to add the new bit.
What does the bitwise AND (&) with 1 do?
AExtracts the least significant bit
BSets the bit to 1
CShifts bits right
DClears all bits
✗ Incorrect
AND with 1 keeps only the least significant bit, extracting it.
Explain step-by-step how to reverse the bits of a number using bitwise operations.
Think about how you build the reversed number bit by bit.
You got /5 concepts.
Why is reversing bits useful and where might you use it?
Consider real-world applications of bit operations.
You got /4 concepts.
Practice
(1/5)
1. What does reversing the bits of a number mean in simple terms?
easy
A. Flipping the order of all bits from left to right
B. Changing all 0 bits to 1 bits
C. Adding 1 to the number
D. Multiplying the number by 2
Solution
Step 1: Understand bits as binary digits
Bits are the 0s and 1s that make up a number in binary form.
Step 2: Define reversing bits
Reversing bits means reading the bits from right to left instead of left to right.
Final Answer:
Flipping the order of all bits from left to right -> Option A
Quick Check:
Reverse bits = flip order [OK]
Hint: Think of bits like beads on a string flipped end to end [OK]
Common Mistakes:
Confusing reversing bits with flipping bits (0 to 1)
Thinking reversing bits changes the number by adding or multiplying
Assuming reversing bits means reversing digits in decimal
2. Which of the following JavaScript code snippets correctly reverses the bits of an 8-bit number n?
easy
A. let result = 0; for(let i=0; i<8; i++) { result += (n & 1) << i; n <<= 1; }
B. let result = 0; for(let i=0; i<8; i++) { result |= (n & 1) << i; n >>= 1; }
C. let result = 0; for(let i=0; i<8; i++) { result <<= 1; result |= (n & 1); n >>= 1; }
D. let result = 0; for(let i=7; i>0; i--) { result |= (n & 1) << i; n >>= 1; }
Solution
Step 1: Analyze the loop and bit operations
let result = 0; for(let i=0; i<8; i++) { result <<= 1; result |= (n & 1); n >>= 1; } shifts result left, adds least significant bit of n, then shifts n right. This reverses bits correctly.
Step 2: Check other options for errors
let result = 0; for(let i=0; i<8; i++) { result |= (n & 1) << i; n >>= 1; } does not reverse bits, it copies bits in same order. let result = 0; for(let i=7; i>0; i--) { result |= (n & 1) << i; n >>= 1; } shifts bits incorrectly. let result = 0; for(let i=0; i<8; i++) { result += (n & 1) << i; n <<= 1; } shifts n left, which is wrong.
Final Answer:
let result = 0; for(let i=0; i<8; i++) { result <<= 1; result |= (n & 1); n >>= 1; } -> Option C
Quick Check:
Shift result left + add bit from n right [OK]
Hint: Shift result left, add n's right bit, shift n right [OK]
Common Mistakes:
Shifting n left instead of right
Not shifting result before adding bit
Using wrong loop direction or bit positions
3. What is the output of the following code snippet?
function reverseBits(n) {
let result = 0;
for(let i = 0; i < 4; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
console.log(reverseBits(13));
Hint: Trace bit shifts and OR operations carefully [OK]
Common Mistakes:
Confusing decimal and binary values
Not shifting result before OR
Looping wrong number of times
4. Find the error in this bit reversal function and choose the corrected version:
function reverseBits(n) {
let result = 0;
for(let i = 0; i < 8; i++) {
result |= (n & 1);
result <<= 1;
n >>= 1;
}
return result;
}
medium
A. Change loop to run 16 times
B. Shift n left instead of right
C. Initialize result with 1 instead of 0
D. Swap the order: shift result left before OR operation
Solution
Step 1: Identify order of operations
Current code ORs bit then shifts result left, causing last bit to be lost.
Step 2: Correct by shifting result left before OR
Shift result left first, then OR with current bit to build reversed bits correctly.
Final Answer:
Swap the order: shift result left before OR operation -> Option D
Quick Check:
Shift before OR to keep all bits [OK]
Hint: Always shift result left before adding new bit [OK]
Common Mistakes:
Shifting result after OR loses last bit
Shifting n left instead of right
Wrong loop count for bit size
5. You want to reverse the bits of a 16-bit number and then check if the reversed number is a palindrome in binary (reads same forwards and backwards). Which approach is correct?
hard
A. Reverse bits using a loop, then check if reversed number equals its own reverse
B. Reverse bits using a loop, then reverse again and compare with original
C. Reverse bits using a loop, then compare reversed number with original
D. Reverse bits using a loop, then convert to decimal and check if even
Solution
Step 1: Reverse bits of the number
Use a loop to reverse all 16 bits correctly.
Step 2: Check palindrome by comparing reversed number with its own reverse
If reversed number equals its reverse, it is a binary palindrome.
Final Answer:
Reverse bits using a loop, then check if reversed number equals its own reverse -> Option A