0
0
DSA Pythonprogramming~10 mins

Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Python - 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 where both bits are 1
Apply OR (|)
Result: bits set where at least one bit is 1
Apply XOR (^)
Result: bits set where 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 bitwise AND, OR, XOR, NOT, left shift, and right shift operations step-by-step to see how bits change.
Execution Sample
DSA Python
a = 6  # 0b0110
b = 11 # 0b1011
print(a & b)
print(a | b)
print(a ^ b)
print(~a)
print(a << 1)
print(b >> 2)
This code applies bitwise AND, OR, XOR, NOT, left shift, and right shift on two numbers and prints the results.
Execution Table
StepOperationOperands (binary)Result (binary)Result (decimal)Explanation
1AND (&)a=0110, b=101100102Bits set where both bits are 1
2OR (|)a=0110, b=1011111115Bits set where at least one bit is 1
3XOR (^)a=0110, b=1011110113Bits set where bits differ
4NOT (~)a=0000...01101111...1001-7Bits flipped; two's complement negative
5Left Shift (<< 1)a=0110110012Bits shifted left by 1, zero fills right
6Right Shift (>> 2)b=101100102Bits shifted right by 2, zero fills left
7End---All operations completed
💡 All bitwise operations on a=6 and b=11 completed
Variable Tracker
VariableInitial (decimal)Initial (binary)After ANDAfter ORAfter XORAfter NOT (~a)After a << 1After b >> 2
a601102 (0010)15 (1111)13 (1101)-7 (1111...1001)12 (1100)3 (0011)
b1110112 (0010)15 (1111)13 (1101)11 (1011)11 (1011)2 (0010)
Key Moments - 3 Insights
Why does the NOT (~) operation on 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. See step 4 in execution_table where ~6 (0000...0110) becomes 1111...1001 which is -7.
Why does left shift (<<) multiply the number by 2?
Left shift moves bits to the left, adding a zero on the right, effectively multiplying by 2 for each shift. Step 5 shows 6 (0110) shifted left once becomes 12 (1100).
What happens to bits shifted out during right shift (>>)?
Bits shifted out on the right are lost, and zeros (or sign bit for signed numbers) fill in from the left. Step 6 shows 11 (1011) shifted right twice becomes 2 (0010).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of a & b at step 1 in decimal?
A6
B2
C3
D15
💡 Hint
Check the 'Result (decimal)' column in step 1 of execution_table.
At which step does the result become negative due to bitwise operation?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look for negative decimal result in the 'Result (decimal)' column.
If we change a from 6 to 4, what would be the result of a << 1 (left shift by 1)?
A8
B10
C12
D6
💡 Hint
Left shift by 1 doubles the number; check variable_tracker for a << 1.
Concept Snapshot
Bitwise operators work on bits of numbers:
AND (&): 1 if both bits 1
OR (|): 1 if any bit 1
XOR (^): 1 if bits differ
NOT (~): flips bits, two's complement
Left Shift (<<): shifts bits left, multiplies by 2
Right Shift (>>): shifts bits right, divides by 2 (floor)
Full Transcript
This lesson shows how bitwise operations work on two numbers, 6 and 11. We apply AND, OR, XOR, NOT, left shift, and right shift step-by-step. AND keeps bits set only if both bits are 1. OR sets bits if either bit is 1. XOR sets bits where bits differ. NOT flips all bits, resulting in negative number due to two's complement. Left shift moves bits left, multiplying by 2. Right shift moves bits right, dividing by 2. We track each result in binary and decimal to see how bits change.