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 instead of string operations?
Bitwise operations work directly on the binary representation and are faster and use less memory than converting to strings and reversing.
Click to reveal answer
beginner
In Java, which operator is used to shift bits to the right?
The '>>' operator shifts bits to the right, filling with the sign bit, and '>>>' shifts bits to the right filling with zero.
Click to reveal answer
beginner
What is the purpose of using '& 1' in bit manipulation?
Using '& 1' extracts the least significant bit (rightmost bit) of a number, which helps in checking if it is 0 or 1.
Click to reveal answer
beginner
How many bits do we usually consider when reversing bits of an integer in Java?
We usually consider 32 bits because Java's int type is 32 bits long.
Click to reveal answer
What is the result of reversing bits of the number 1 (binary 000...0001) in 4 bits?
A8 (binary 1000)
B1 (binary 0001)
C4 (binary 0100)
D0 (binary 0000)
✗ Incorrect
Reversing 0001 in 4 bits gives 1000, which is 8 in decimal.
Which bitwise operator shifts bits to the left in Java?
A&
B>>
C>>>
D<<
✗ Incorrect
The '<<' operator shifts bits to the left, adding zeros on the right.
What does the expression 'n & 1' check in bit manipulation?
AIf n is even or odd
BIf n is positive or negative
CIf n is zero
DIf n is a power of two
✗ Incorrect
'n & 1' checks the least significant bit, which tells if n is even (0) or odd (1).
How many bits are reversed when reversing bits of an int in Java?
A8 bits
B32 bits
C16 bits
D64 bits
✗ Incorrect
Java int is 32 bits, so reversing bits means reversing all 32 bits.
Which of these is the correct way to get the rightmost bit of a number n?
An >> 1
Bn << 1
Cn & 1
Dn | 1
✗ Incorrect
'n & 1' extracts the rightmost bit of n.
Explain step-by-step how to reverse the bits of a 32-bit integer using bitwise operations.
Think about moving bits from right to left one by one.
You got /7 concepts.
Why is reversing bits useful in programming? Give two examples.
Think about where bit order matters.
You got /4 concepts.
Practice
(1/5)
1. What does reversing the bits of a number mean in simple terms?
easy
A. Shifting bits to the left by one position
B. Flipping the order of bits from left to right
C. Adding 1 to the number
D. Changing all bits from 0 to 1
Solution
Step 1: Understand bit order
Bits are the 0s and 1s that make up a number. Reversing means flipping their order.
Step 2: Explain reversing bits
If the bits are 1010, reversing them gives 0101, flipping left to right.
Final Answer:
Flipping the order of bits from left to right -> Option B
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 means adding or subtracting
Mixing bit shifting with bit reversing
2. Which Java syntax correctly reverses bits of an integer n using a loop?
easy
A. for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; }
B. for (int i = 0; i < 32; i++) { result |= (n & 1) << i; n >>= 1; }
C. for (int i = 0; i < 32; i++) { result += (n & 1) << i; n <<= 1; }
D. for (int i = 0; i < 32; i++) { result <<= 1; result |= (n >> i) & 1; }
Solution
Step 1: Analyze bit extraction and shifting
for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; } extracts the least significant bit (n & 1), shifts result left, then adds the bit, and shifts n right.
Step 2: Check correctness of loop operations
for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; } correctly reverses bits by building result from left to right while moving through n's bits right to left.
Final Answer:
for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; } -> Option A
Quick Check:
Shift result left, add bit from n, shift n right [OK]
Hint: Shift result left before adding extracted bit [OK]
Common Mistakes:
Shifting n left instead of right
Adding bits without shifting result
Using incorrect bit extraction
3. What is the output of this Java code snippet?
int n = 13; // binary 00000000000000000000000000001101
int result = 0;
for (int i = 0; i < 4; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
System.out.println(result);