0
0
DSA Pythonprogramming~10 mins

Reverse Bits of a Number in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Reverse Bits of a Number
Start with number n
Initialize result = 0
Repeat for each bit position i from 0 to 31
Extract bit at position i from n
Shift result left by 1
Add extracted bit to result
Move to next bit
After 32 bits, return result
We take each bit from the original number starting from the least significant bit, and build the reversed number by shifting and adding bits one by one.
Execution Sample
DSA Python
def reverse_bits(n):
    result = 0
    for i in range(32):
        bit = (n >> i) & 1
        result = (result << 1) | bit
    return result
This code reverses the bits of a 32-bit unsigned integer by extracting each bit and building the reversed number.
Execution Table
StepOperationBit ExtractedResult BeforeResult AfterVisual State (bits)
1Extract bit 01000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000001
2Extract bit 10000000000000000000000000000000100000000000000000000000000000001000000000000000000000000000000010
3Extract bit 21000000000000000000000000000001010000000000000000000000000000010100000000000000000000000000000101
4Extract bit 30000000000000000000000000000010100000000000000000000000000000101000000000000000000000000000001010
5Extract bit 41000000000000000000000000000101010000000000000000000000000001010100000000000000000000000000010101
6Extract bit 50000000000000000000000000001010100000000000000000000000000010101000000000000000000000000000101010
7Extract bit 60000000000000000000000000010101000000000000000000000000000101010000000000000000000000000001010100
8Extract bit 70000000000000000000000000101010000000000000000000000000001010100000000000000000000000000010101000
9Extract bit 80000000000000000000000001010100000000000000000000000000010101000000000000000000000000000101010000
10Extract bit 90000000000000000000000010101000000000000000000000000000101010000000000000000000000000001010100000
11Extract bit 100000000000000000000000101010000000000000000000000000001010100000000000000000000000000010101000000
12Extract bit 110000000000000000000001010100000000000000000000000000010101000000000000000000000000000101010000000
13Extract bit 120000000000000000000010101000000000000000000000000000101010000000000000000000000000001010100000000
14Extract bit 130000000000000000000101010000000000000000000000000001010100000000000000000000000000010101000000000
15Extract bit 140000000000000000001010100000000000000000000000000010101000000000000000000000000000101010000000000
16Extract bit 150000000000000000010101000000000000000000000000000101010000000000000000000000000001010100000000000
17Extract bit 160000000000000000101010000000000000000000000000001010100000000000000000000000000010101000000000000
18Extract bit 170000000000000001010100000000000000000000000000010101000000000000000000000000000101010000000000000
19Extract bit 180000000000000010101000000000000000000000000000101010000000000000000000000000001010100000000000000
20Extract bit 190000000000000101010000000000000000000000000001010100000000000000000000000000010101000000000000000
21Extract bit 200000000000001010100000000000000000000000000010101000000000000000000000000000101010000000000000000
22Extract bit 210000000000010101000000000000000000000000000101010000000000000000000000000001010100000000000000000
23Extract bit 220000000000101010000000000000000000000000001010100000000000000000000000000010101000000000000000000
24Extract bit 230000000001010100000000000000000000000000010101000000000000000000000000000101010000000000000000000
25Extract bit 240000000010101000000000000000000000000000101010000000000000000000000000001010100000000000000000000
26Extract bit 250000000101010000000000000000000000000001010100000000000000000000000000010101000000000000000000000
27Extract bit 260000001010100000000000000000000000000010101000000000000000000000000000101010000000000000000000000
28Extract bit 270000010101000000000000000000000000000101010000000000000000000000000001010100000000000000000000000
29Extract bit 280000101010000000000000000000000000001010100000000000000000000000000010101000000000000000000000000
30Extract bit 290001010100000000000000000000000000010101000000000000000000000000000101010000000000000000000000000
31Extract bit 300010101000000000000000000000000000101010000000000000000000000000001010100000000000000000000000000
32Extract bit 310101010000000000000000000000000001010100000000000000000000000000010101000000000000000000000000000
💡 All 32 bits processed, loop ends
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10After 11After 12After 13After 14After 15After 16After 17After 18After 19After 20After 21After 22After 23After 24After 25After 26After 27After 28After 29After 30After 31After 32Final
result00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000001000000000000000000000000000000101000000000000000000000000000010100000000000000000000000000001010100000000000000000000000000101010000000000000000000000000010101000000000000000000000000001010100000000000000000000000000101010000000000000000000000000010101000000000000000000000000001010100000000000000000000000000101010000000000000000000000000010101000000000000000000000000001010100000000000000000000000000101010000000000000000000000000010101000000000000000000000000001010100000000000000000000000000101010000000000000000000000000010101000000000000000000000000001010100000000000000000000000000101010000000000000000000000000010101000000000000000000000000001010100000000000000000000000000101010000000000000000000000000010101000000000000000000000000001010100000000000000000000000000101010000000000000000000000000010101000000000000000000000000001010100000000000000000000000000101010000000000000000000000000010101000000000000000000000000001010100000000000000000000000000010101000000000000000000000000000
Key Moments - 3 Insights
Why do we shift the result left before adding the extracted bit?
Because we build the reversed number from left to right, shifting left makes space for the new bit at the rightmost position. This is shown in execution_table rows where 'Result Before' is shifted left before adding 'Bit Extracted'.
Why do we extract bits from the original number using (n >> i) & 1?
This operation moves the bit at position i to the least significant bit and masks all others, isolating that single bit. The execution_table shows 'Bit Extracted' changing each step, confirming this extraction.
Why do we loop exactly 32 times?
Because we assume a 32-bit number, and each bit must be processed to reverse the entire bit pattern. The exit_note confirms the loop ends after 32 bits.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what is the 'Bit Extracted' value?
A0
B1
C2
DNone
💡 Hint
Check the 'Bit Extracted' column in row with Step 3 in execution_table.
At which step does the 'Result After' first become 00000000000000000000000000000101?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Result After' column in execution_table rows for the exact bit pattern.
If we change the loop to run only 16 times, what will happen to the 'Final' result?
AIt will reverse only the lower 16 bits
BIt will reverse all 32 bits
CIt will produce zero
DIt will cause an error
💡 Hint
Refer to the concept_flow and variable_tracker showing 32 iterations for full reversal.
Concept Snapshot
Reverse Bits of a Number:
- Loop 32 times for each bit
- Extract bit i with (n >> i) & 1
- Shift result left by 1
- Add extracted bit to result
- Return result after loop
- Reverses bit order of 32-bit integer
Full Transcript
This concept reverses the bits of a 32-bit number by extracting each bit from the original number starting from the least significant bit. For each bit, the result is shifted left to make space, then the extracted bit is added. This process repeats 32 times to cover all bits. The execution table shows each step with the bit extracted, the result before and after shifting and adding, and the visual bit pattern. The variable tracker follows the result variable's changes after each iteration. Key moments clarify why shifting is needed before adding bits, how bits are extracted, and why the loop runs 32 times. The visual quiz tests understanding of bit extraction, result changes, and effects of changing loop count.