Bird
0
0
DSA Cprogramming~10 mins

Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Bit Manipulation Basics AND OR XOR NOT Left Right Shift
Start with two numbers
Apply AND (&)
Result: bits set only if both bits are 1
Apply OR (|)
Result: bits set if either bit is 1
Apply XOR (^)
Result: bits set if bits differ
Apply NOT (~)
Result: bits flipped
Apply Left Shift (<<)
Result: bits shifted left, zeros fill right
Apply Right Shift (>>)
Result: bits shifted right, sign bit or zero fill
End
Start with two numbers, then apply each bitwise operation step-by-step to see how bits change.
Execution Sample
DSA C
int a = 6;  // 00000110
int b = 11; // 00001011
int c;
c = a & b;
c = a | b;
c = a ^ b;
c = ~a;
c = a << 1;
c = b >> 2;
This code applies AND, OR, XOR, NOT, left shift, and right shift on two numbers and stores results.
Execution Table
StepOperationInput Values (a, b)Result (decimal)Result (binary)Explanation
1AND (&)a=6 (00000110), b=11 (00001011)200000010Bits set only where both a and b have 1
2OR (|)a=6 (00000110), b=11 (00001011)1500001111Bits set where either a or b has 1
3XOR (^)a=6 (00000110), b=11 (00001011)1300001101Bits set where a and b bits differ
4NOT (~)a=6 (00000110)-711111001 (2's complement)Bits flipped; in 8-bit signed, ~6 = -7
5Left Shift (<< 1)a=6 (00000110)1200001100Bits shifted left by 1, zero fills right
6Right Shift (>> 2)b=11 (00001011)200000010Bits shifted right by 2, zeros fill left
7End---All operations completed
💡 All bitwise operations applied to inputs; execution ends.
Variable Tracker
VariableInitialAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
a6 (00000110)6666666
b11 (00001011)11111111111111
cundefined2 (00000010)15 (00001111)13 (00001101)-7 (11111001)12 (00001100)2 (00000010)2
Key Moments - 3 Insights
Why does NOT (~) of 6 give -7 instead of a positive number?
Because NOT flips all bits including the sign bit in two's complement representation, resulting in a negative number as shown in step 4 of the execution_table.
Why does left shift (<<) multiply the number by 2?
Left shift moves bits to the left, adding a zero on the right, effectively doubling the number as seen in step 5 where 6 becomes 12.
What happens to bits shifted out in right shift (>>)?
Bits shifted out on the right are discarded, and zeros fill in from the left for unsigned or positive numbers, as shown in step 6 where 11 >> 2 becomes 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the binary result of a ^ b?
A00001101
B00001111
C00000010
D11111001
💡 Hint
Check the 'Result (binary)' column at step 3 in execution_table.
At which step does the variable c become negative?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Look at the 'Result (decimal)' column for negative values in execution_table.
If we change a to 3 (00000011), what would be the result of a & b at step 1?
A2
B3
C1
D0
💡 Hint
AND sets bits only where both inputs have 1; compare bits of 3 and 11.
Concept Snapshot
Bit Manipulation Basics:
- AND (&): bits set if both bits 1
- OR (|): bits set if either bit 1
- XOR (^): bits set if bits differ
- NOT (~): bits flipped (two's complement)
- Left Shift (<<): bits shift left, zero fill right
- Right Shift (>>): bits shift right, zero or sign fill left
Full Transcript
This lesson shows how to use basic bit manipulation operators in C. We start with two numbers, 6 and 11, and apply AND, OR, XOR, NOT, left shift, and right shift. Each operation changes bits in a specific way. AND keeps bits set only if both inputs have 1. OR sets bits if either input has 1. XOR sets bits where inputs differ. NOT flips all bits, which can produce negative numbers in two's complement. Left shift moves bits left, doubling the number. Right shift moves bits right, dividing by powers of two. The execution table traces each step with decimal and binary results. Variable tracking shows how values change. Key moments clarify common confusions about negative results and bit shifts. Visual quizzes test understanding by referencing the execution steps and bit patterns.