0
0
DSA Pythonprogramming~5 mins

Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the AND (&) bitwise operator do between two bits?
The AND operator returns 1 only if both bits are 1; otherwise, it returns 0.<br>Example: 1 & 1 = 1, 1 & 0 = 0, 0 & 0 = 0.
Click to reveal answer
beginner
Explain the OR (|) bitwise operator with an example.
The OR operator returns 1 if at least one of the bits is 1; otherwise, it returns 0.<br>Example: 1 | 0 = 1, 0 | 0 = 0, 1 | 1 = 1.
Click to reveal answer
beginner
What is the result of XOR (^) between two bits?
XOR returns 1 if the bits are different, and 0 if they are the same.<br>Example: 1 ^ 0 = 1, 1 ^ 1 = 0, 0 ^ 0 = 0.
Click to reveal answer
intermediate
Describe the NOT (~) bitwise operator.
NOT flips each bit: 1 becomes 0, and 0 becomes 1.<br>Example: ~1 (in 1-bit) = 0, ~0 = 1.<br>In computers, it also changes the sign due to two's complement representation.
Click to reveal answer
intermediate
What happens when you perform a left shift (<<) and right shift (>>) on a binary number?
Left shift (<<) moves bits to the left, adding zeros on the right, effectively multiplying by 2 for each shift.<br>Right shift (>>) moves bits to the right, discarding bits on the right, effectively dividing by 2 for each shift.<br>Example: 4 (0100) << 1 = 8 (1000), 4 >> 1 = 2 (0010).
Click to reveal answer
What is the result of 5 & 3 in binary? (5 = 0101, 3 = 0011)
A0101 (5)
B0011 (3)
C0001 (1)
D0111 (7)
Which operator returns 1 only when bits are different?
AAND (&)
BNOT (~)
COR (|)
DXOR (^)
What does the expression ~0 evaluate to in a 4-bit system?
A1111 (-1 in two's complement)
B0000 (0)
C1110 (14)
D0001 (1)
What is the decimal result of 6 << 2?
A24
B12
C3
D48
Which bitwise operator would you use to clear specific bits (set them to 0)?
AOR (|) with mask
BAND (&) with mask
CXOR (^) with mask
DNOT (~)
Explain how the XOR operator works and give a real-life example where it might be useful.
Think about switching a light on/off using XOR.
You got /3 concepts.
    Describe what happens to a binary number when you perform a left shift and a right shift.
    Imagine moving digits in a number to the left or right.
    You got /4 concepts.