Reverse Bits


This code is implementing a binary reversal operation. It’s reversing the binary representation of an integer n
over 32 bits and storing the result in res
. Here’s a stepbystep breakdown:
res = 0
: This initializesres
, which will hold the reversed binary number.for _ in range(32)
: This loop will run 32 times. The number 32 is used because we’re assumingn
is a 32bit integer.res = (res<<1) + (n&1)
: This is where the reversal happens.(n&1)
gets the least significant bit ofn
(i.e., the rightmost bit).res<<1
shifts all bits inres
one position to the left (equivalent to multiplyingres
by 2), making room for the new bit on the right. The new bit is added tores
using the+
operator. This effectively adds the least significant bit ofn
to the left side ofres
.n>>=1
: This shifts all bits inn
one position to the right (equivalent to integer division by 2), effectively removing the least significant bit. This is done after the least significant bit has been added tores
, so that bit isn’t lost.return res
: After the loop has run 32 times,res
will hold the binary reversal ofn
, sores
is returned.
So, if n
was the binary number 1001
(which is 9
in decimal), res
will be 1001
(which is also 9
in decimal). Notice how the least significant bit in n
became the most significant bit in res
, and the most significant bit in n
became the least significant bit in res
, which means the binary number was reversed.
Problem Classification
Computer Science Fundamentals: The problem involves fundamental understanding of how computers store and handle data at a low level.
Bit Manipulation: The problem involves understanding and performing operations on binary representations of numbers.
Binary Number Operations: Specifically, the problem deals with reversing the binary representation of an integer.
Bit Shifting and Bitwise Operations: The task requires the understanding and use of bit shifting (right and left) and bitwise operations like bitwise AND.
The specific usage of these concepts might vary depending upon the language and implementation.
Language Agnostic Coding Drills
Breaking down the problem of reversing the bits of a 32bit unsigned integer into smaller units of learning, we can create the following drills:
Understanding Binary Numbers: Familiarize yourself with binary numbers, their representation, and manipulation. This includes understanding how numbers are represented in bits and how to manually convert a decimal number to binary and vice versa.
Bitwise Operations: Study bitwise operations including AND (
&
), OR (
), XOR (^
), NOT (~
), Left Shift (<<
) and Right Shift (>>
). Understand what these operations do and how they can be used to manipulate the bits in a number.Left Shift and Right Shift Operations: Dive deeper into shift operations, particularly understanding what happens when you left shift or right shift a number. Experiment with different examples to understand these operations thoroughly.
Bit Masking: Understand the concept of bit masking, i.e., using bitwise operations to manipulate specific bits of a number.
Iteration: Understand the concept of iteration in a loop. Understand the significance of the number of iterations matching the number of bits you are dealing with (32 in this case).
Combining Operations: Practice combining these operations. For instance, shifting bits and then performing a bitwise AND operation.
The final solution combines all these smaller units. It initiates a result with 0 and then goes through a loop 32 times (since the number is a 32bit integer). In each iteration, it shifts the result to the left by 1 bit (essentially multiplying it by 2), adds the least significant bit of n
, and then right shifts n
by 1 bit (essentially dividing it by 2). This way, it constructs the reverse of n
bit by bit.
Targeted Drills in Python
 Understanding Binary Numbers: You can convert decimal to binary using
bin
and binary to decimal usingint
function in Python.


 Bitwise Operations:


 Left Shift and Right Shift Operations:


 Bit Masking:


 Iteration:


 Combining Operations:


The last example is essentially the solution to the problem. It shows how these individual units combine to solve the problem.