Bird
0
0
DSA Cprogramming~10 mins

Reverse Bits of a Number in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Reverse Bits of a Number
Start with number n
Initialize result = 0
Loop 8 times
Shift result left by 1
Extract last bit of n
Add extracted bit to result
Shift n right by 1
Repeat loop
Return result with bits reversed
Start with the number, then for each of the 8 bits, shift the result left, add the last bit of the number, and shift the number right. Repeat until all bits are reversed.
Execution Sample
DSA C
unsigned int reverseBits(unsigned int n) {
    unsigned int result = 0;
    for (int i = 0; i < 8; i++) {
        result <<= 1;
        result |= (n & 1);
        n >>= 1;
    }
    return result;
}
This code reverses the bits of an 8-bit unsigned integer by shifting and masking bits.
Execution Table
StepOperationn (binary)Extracted Bitresult (binary)Visual State
1Initialize result=000000000000000000000000000001010-00000000000000000000000000000000result=0, n=10 (binary 1010)
2Shift result left by 100000000000000000000000000001010-00000000000000000000000000000000result=0
3Extract last bit of n (n & 1)00000000000000000000000000001010000000000000000000000000000000000bit=0
4Add extracted bit to result00000000000000000000000000001010000000000000000000000000000000000result=0
5Shift n right by 100000000000000000000000000000101-00000000000000000000000000000000n=5
6Shift result left by 100000000000000000000000000000101-00000000000000000000000000000000result=0
7Extract last bit of n (n & 1)00000000000000000000000000000101100000000000000000000000000000000bit=1
8Add extracted bit to result00000000000000000000000000000101100000000000000000000000000000001result=1
9Shift n right by 100000000000000000000000000000010-00000000000000000000000000000001n=2
10Shift result left by 100000000000000000000000000000010-00000000000000000000000000000010result=2
11Extract last bit of n (n & 1)00000000000000000000000000000010000000000000000000000000000000010bit=0
12Add extracted bit to result00000000000000000000000000000010000000000000000000000000000000010result=2
13Shift n right by 100000000000000000000000000000001-00000000000000000000000000000010n=1
14Shift result left by 100000000000000000000000000000001-00000000000000000000000000000100result=4
15Extract last bit of n (n & 1)00000000000000000000000000000001100000000000000000000000000000100bit=1
16Add extracted bit to result00000000000000000000000000000001100000000000000000000000000000101result=5
17Shift n right by 100000000000000000000000000000000-00000000000000000000000000000101n=0
18Shift result left by 100000000000000000000000000000000-00000000000000000000000000001010result=10
19Extract last bit of n (n & 1)00000000000000000000000000000000000000000000000000000000000001010bit=0
20Add extracted bit to result00000000000000000000000000000000000000000000000000000000000001010result=10
21Shift n right by 100000000000000000000000000000000-00000000000000000000000000001010n=0
22Shift result left by 100000000000000000000000000000000-00000000000000000000000000010100result=20
23Extract last bit of n (n & 1)00000000000000000000000000000000000000000000000000000000000010100bit=0
24Add extracted bit to result00000000000000000000000000000000000000000000000000000000000010100result=20
25Shift n right by 100000000000000000000000000000000-00000000000000000000000000010100n=0
26Shift result left by 100000000000000000000000000000000-00000000000000000000000000101000result=40
27Extract last bit of n (n & 1)00000000000000000000000000000000000000000000000000000000000101000bit=0
28Add extracted bit to result00000000000000000000000000000000000000000000000000000000000101000result=40
29Shift n right by 100000000000000000000000000000000-00000000000000000000000000101000n=0
30Shift result left by 100000000000000000000000000000000-00000000000000000000000001010000result=80
31Extract last bit of n (n & 1)00000000000000000000000000000000000000000000000000000000001010000bit=0
32Add extracted bit to result00000000000000000000000000000000000000000000000000000000001010000result=80
33Shift n right by 100000000000000000000000000000000-00000000000000000000000001010000n=0
34Loop ends after 8 iterations--00000000000000000000000001010000Final reversed bits result=80
💡 Loop ends after 8 iterations, all bits processed
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
n00000000000000000000000000001010 (10)00000000000000000000000000000101 (5)00000000000000000000000000000010 (2)00000000000000000000000000000001 (1)00000000000000000000000000000000 (0)00000000000000000000000000000000 (0)
result00000000000000000000000000000000 (0)00000000000000000000000000000000 (0)00000000000000000000000000000001 (1)00000000000000000000000000000010 (2)00000000000000000000000000000101 (5)00000000000000000000000001010000 (80)
Extracted Bit-01010
Key Moments - 3 Insights
Why do we shift the result left before adding the extracted bit?
Shifting result left makes room for the new bit on the right. This is shown in execution_table rows 2 and 6 where result is shifted before adding the bit in rows 4 and 8.
Why do we extract the last bit of n using (n & 1)?
The expression (n & 1) isolates the least significant bit of n. This is the bit we add to result each iteration, as seen in execution_table rows 3, 7, 11, and 15.
What happens when n becomes zero before 8 iterations?
Even if n becomes zero early (row 17), the loop continues shifting result and adding zeros to complete 8 bits, ensuring all bits are reversed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 8, what is the value of result in binary?
A00000000000000000000000000000001
B00000000000000000000000000000010
C00000000000000000000000000000000
D00000000000000000000000000000101
💡 Hint
Check the 'result (binary)' column at step 8 in the execution_table.
At which step does n first become zero according to the execution_table?
AStep 14
BStep 17
CStep 20
DStep 33
💡 Hint
Look at the 'n (binary)' column to find when it changes to all zeros.
If we did not shift result left before adding the extracted bit, how would the result change at step 8?
AResult would be 0
BResult would be 2
CResult would be 1
DResult would be 5
💡 Hint
Without shifting left, adding bit 1 would set result to 1 directly, check step 8 in execution_table.
Concept Snapshot
Reverse Bits of a Number:
- Initialize result = 0
- Loop 8 times:
  - Shift result left by 1
  - Add last bit of n (n & 1) to result
  - Shift n right by 1
- Return result with bits reversed
Full Transcript
This concept reverses the bits of an 8-bit unsigned integer. We start with the input number n and a result initialized to zero. For each of the 8 bits, we shift the result left by one to make space, then add the least significant bit of n to result. Next, we shift n right by one to process the next bit. This repeats 8 times to reverse all bits. The execution table shows each step with the binary states of n and result, and the extracted bit. Key moments clarify why shifting and masking are needed. The visual quiz tests understanding of intermediate states and effects of operations.