Challenge - 5 Problems
Bit Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing bits for a small number
What is the output of reversing the bits of the 8-bit number 13 (00001101) using a 8-bit reverse function?
DSA Go
func reverseBits8(n uint8) uint8 {
var result uint8 = 0
for i := 0; i < 8; i++ {
result <<= 1
result |= n & 1
n >>= 1
}
return result
}
fmt.Println(reverseBits8(13))Attempts:
2 left
💡 Hint
Think about how bits shift left and right during reversal.
✗ Incorrect
Reversing bits of 00001101 results in 10110000 which is 176 in decimal.
❓ Predict Output
intermediateOutput of reversing bits for a 16-bit number
What is the output of reversing the bits of the 16-bit number 4660 (0001001000110100) using a 16-bit reverse function?
DSA Go
func reverseBits16(n uint16) uint16 {
var result uint16 = 0
for i := 0; i < 16; i++ {
result <<= 1
result |= n & 1
n >>= 1
}
return result
}
fmt.Println(reverseBits16(4660))Attempts:
2 left
💡 Hint
Reverse the bits carefully and convert back to decimal.
✗ Incorrect
Reversing bits of 0001001000110100 results in 0011001000010000 which is 13328 decimal.
🔧 Debug
advancedIdentify the error in this bit reversal code
What error does this Go code produce when reversing bits of a 32-bit unsigned integer?
DSA Go
func reverseBits32(n uint32) uint32 {
var result uint32 = 0
for i := 0; i < 32; i++ {
result <<= 1
result |= n & 1
n >>= 1
}
return result
}
fmt.Println(reverseBits32(43261596))Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop runs 33 times (i <= 32) instead of 32, causing an extra shift and wrong output.
🧠 Conceptual
advancedWhy use bitwise operations for reversing bits?
Which reason best explains why bitwise operations are preferred for reversing bits of a number?
Attempts:
2 left
💡 Hint
Think about how computers store and handle bits.
✗ Incorrect
Bitwise operations work directly on bits, making reversal fast and memory efficient.
❓ Predict Output
expertOutput of reversing bits for a 32-bit number with leading zeros
What is the output of reversing the bits of the 32-bit number 1 (only the least significant bit set) using a 32-bit reverse function?
DSA Go
func reverseBits32(n uint32) uint32 {
var result uint32 = 0
for i := 0; i < 32; i++ {
result <<= 1
result |= n & 1
n >>= 1
}
return result
}
fmt.Println(reverseBits32(1))Attempts:
2 left
💡 Hint
Consider where the single 1 bit moves after reversal.
✗ Incorrect
Reversing bits of 000...0001 results in 100...0000 which is 2147483648 decimal.
