What if flipping bits could be done in a blink, not by slow, error-prone manual work?
Why Reverse Bits of a Number in DSA C++?
Imagine you have a number represented in binary, like a row of light switches turned on or off. You want to flip the order of these switches so the first becomes last and the last becomes first. Doing this by hand means checking each switch one by one and moving it to the new position.
Manually flipping each bit is slow and easy to mess up, especially if the number has many bits. Counting positions and moving bits without mistakes is tricky and takes a lot of time.
Using a simple algorithm, we can automatically reverse the bits by shifting and masking bits step-by-step. This method is fast, reliable, and works for any number size without confusion.
int reverseBits(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
int bit = (n >> i) & 1;
result |= bit << (31 - i);
}
return result;
}unsigned int reverseBits(unsigned int number) {
unsigned int reversed = 0;
for (int i = 0; i < 32; i++) {
reversed <<= 1;
reversed |= (number & 1);
number >>= 1;
}
return reversed;
}This lets us quickly flip the order of bits in any number, which is useful in tasks like data encoding, graphics, and network communication.
In computer graphics, reversing bits helps in generating patterns or textures by flipping pixel data efficiently.
Manual bit reversal is slow and error-prone.
Algorithmic bit reversal uses shifting and masking for speed and accuracy.
This technique is key in many computer science applications like encoding and graphics.
Practice
Solution
Step 1: Understand bit order
Bits are arranged from left (most significant) to right (least significant). Reversing means swapping their order.Step 2: Differentiate from other operations
Flipping bits means changing 0 to 1 or 1 to 0, which is different from reversing order.Final Answer:
Flipping the order of bits from left to right -> Option DQuick Check:
Reverse bits = flip order [OK]
- Confusing reversing bits with flipping bits
- Thinking reversing means shifting bits
- Assuming reversing adds or subtracts value
n?Solution
Step 1: Analyze the loop and bit operations
```cpp unsigned char reverseBits(unsigned char n) { unsigned char result = 0; for (int i = 0; i < 8; i++) { result <<= 1; result |= (n & 1); n >>= 1; } return result; } ``` shifts result left, adds least significant bit of n, then shifts n right. This reverses bits correctly.Step 2: Check other options for correctness
```cpp unsigned char reverseBits(unsigned char n) { unsigned char result = 0; for (int i = 0; i < 8; i++) { result |= (n & 1) << i; n >>= 1; } return result; } ``` does not reverse bits, it copies bits in same order. ```cpp unsigned char reverseBits(unsigned char n) { unsigned char result = 0; for (int i = 0; i < 8; i++) { result <<= 1; result |= ((n >> i) & 1); } return result; } ``` uses wrong bit extraction. ```cpp unsigned char reverseBits(unsigned char n) { unsigned char result = 0; for (int i = 7; i >= 0; i--) { result <<= 1; result |= (n & 1); n >>= 1; } return result; } ``` has wrong loop condition causing infinite loop if i-- is missing.Final Answer:
Option A code correctly reverses bits -> Option AQuick Check:
Shift result left + add LSB of n each step [OK]
- Using wrong loop conditions causing infinite loops
- Not shifting result before adding bits
- Extracting bits incorrectly with wrong shifts
unsigned char reverseBits(unsigned char n) {
unsigned char result = 0;
for (int i = 0; i < 8; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
int main() {
unsigned char n = 13; // binary 00001101
unsigned char reversed = reverseBits(n);
std::cout << (int)reversed << std::endl;
return 0;
}Solution
Step 1: Convert 13 to binary and reverse bits
13 in 8-bit binary is 00001101. Reversing bits gives 10110000.Step 2: Convert reversed binary to decimal
10110000 binary equals 128+32+16=176 decimal.Final Answer:
176 -> Option BQuick Check:
00001101 reversed = 10110000 = 176 [OK]
- Confusing bit reversal with bit flipping
- Not casting unsigned char to int for output
- Misreading binary to decimal conversion
unsigned char reverseBits4(unsigned char n) {
unsigned char result = 0;
for (int i = 0; i <= 4; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}Solution
Step 1: Check loop range
The loop runs from i=0 to i=4 inclusive, so 5 iterations instead of 4.Step 2: Understand effect of extra iteration
Extra iteration shifts result and adds an extra bit, corrupting the reversed bits.Final Answer:
Loop runs one extra time causing incorrect result -> Option AQuick Check:
Loop should run 4 times, not 5 [OK]
- Using <= instead of < in loop condition
- Forgetting to shift result before OR
- Ignoring return statement correctness
Solution
Step 1: Understand divide-and-conquer bit reversal
Divide bits into pairs, swap them; then groups of 4 bits (nibbles), swap; then bytes, then half-words.Step 2: Use masks and shifts for efficient swapping
Use bit masks to isolate groups and shift them to swap positions without looping over each bit.Final Answer:
Swap bits in pairs, then nibbles, then bytes, then half-words using masks and shifts -> Option CQuick Check:
Divide-and-conquer uses masks and shifts [OK]
- Using slow bit-by-bit loops for large bit sizes
- Trying string conversion which is inefficient
- Misunderstanding recursion for bit reversal
Ready to prove it?
Pass the test and this pattern moves unknown
