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. 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. 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. 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. Example: ~1 (in 1-bit) = 0, ~0 = 1. 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. Right shift (>>) moves bits to the right, discarding bits on the right, effectively dividing by 2 for each shift. 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)
✗ Incorrect
AND compares bits: 0&0=0,1&1=1, so 0101 & 0011 = 0001 which is 1.
Which operator returns 1 only when bits are different?
AAND (&)
BNOT (~)
COR (|)
DXOR (^)
✗ Incorrect
XOR returns 1 if bits differ, 0 if they are the same.
What does the expression ~0 evaluate to in a 4-bit system?
A1111 (-1 in two's complement)
B0000 (0)
C1110 (14)
D0001 (1)
✗ Incorrect
NOT flips all bits: ~0000 = 1111, which is -1 in two's complement.
What is the decimal result of 6 << 2?
A24
B12
C3
D48
✗ Incorrect
Left shift by 2 multiplies by 2^2=4, so 6*4=24.
Which bitwise operator would you use to clear specific bits (set them to 0)?
AOR (|) with mask
BAND (&) with mask
CXOR (^) with mask
DNOT (~)
✗ Incorrect
AND with a mask having 0s clears bits; 1s keep bits unchanged.
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.
Practice
(1/5)
1. What is the result of the bitwise AND operation between 5 and 3? (5 = 0101, 3 = 0011 in binary)
easy
A. 1
B. 7
C. 6
D. 0
Solution
Step 1: Convert numbers to binary
5 in binary is 0101, 3 in binary is 0011.
Step 2: Perform bitwise AND
0101 AND 0011 = 0001 (only bits set in both numbers remain set).
Final Answer:
1 -> Option A
Quick Check:
5 & 3 = 1 [OK]
Hint: AND keeps bits set in both numbers only [OK]
Common Mistakes:
Confusing AND with OR operation
Not converting numbers to binary before operation
Assuming decimal addition instead of bitwise AND
2. Which of the following is the correct syntax to perform a left shift by 2 bits on variable x in Python?
easy
A. x <<< 2
B. x >> 2
C. x << 2
D. x >>| 2
Solution
Step 1: Understand left shift operator in Python
Left shift is done using << operator, shifting bits to the left.
Step 2: Check syntax options
Only x << 2 correctly shifts bits left by 2 places.
Final Answer:
x << 2 -> Option C
Quick Check:
Left shift uses << operator [OK]
Hint: Use << for left shift, >> for right shift [OK]
Common Mistakes:
Using >>> which is not valid in Python
Confusing left shift with right shift operator
Using invalid operators like >>|
3. What is the output of the following Python code?
num = 12 # binary 1100
result = num >> 2
print(result)
medium
A. 6
B. 0
C. 48
D. 3
Solution
Step 1: Convert number and apply right shift
12 in binary is 1100. Right shifting by 2 bits removes last two bits: 1100 >> 2 = 0011.
Step 2: Convert shifted binary back to decimal
0011 in binary is 3 in decimal.
Final Answer:
3 -> Option D
Quick Check:
12 >> 2 = 3 [OK]
Hint: Right shift divides number by 2^shift count [OK]
Common Mistakes:
Confusing right shift with left shift
Not converting binary back to decimal
Assuming shift multiplies instead of divides
4. Identify the error in the following code snippet:
value = 10
result = ~value << 1
print(result)
medium
A. Result is -22 due to operator precedence
B. RuntimeError due to invalid shift
C. Result is 21 due to incorrect bitwise NOT
D. SyntaxError due to incorrect operator usage
Solution
Step 1: Evaluate bitwise NOT and shift
~10 equals -11 (because ~x = -x-1). Then shifting left by 1 bit multiplies by 2: -11 << 1 = -22.
Step 2: Check operator precedence and final value
Because ~ has higher precedence, ~10 is evaluated first, then shifted. So result is -22, but print shows -22.
Final Answer:
Result is -22 due to operator precedence -> Option A
Quick Check:
~10 << 1 = -22 [OK]
Hint: Bitwise NOT flips bits, watch operator precedence [OK]
Common Mistakes:
Assuming ~10 is positive
Ignoring operator precedence between ~ and <<
Expecting shift to apply before NOT
5. Given two integers a = 9 and b = 14, which expression will toggle the bits where a and b differ?
hard
A. a | b
B. a ^ b
C. a & b
D. ~a
Solution
Step 1: Understand toggling bits where numbers differ
Toggling bits means flipping bits that are different between two numbers.
Step 2: Identify bitwise XOR operation
XOR (^) returns 1 where bits differ and 0 where bits are same, effectively toggling differing bits.
Final Answer:
a ^ b -> Option B
Quick Check:
XOR toggles differing bits [OK]
Hint: XOR flips bits where inputs differ [OK]
Common Mistakes:
Using AND or OR which do not toggle bits
Confusing NOT (~) with XOR
Assuming OR toggles bits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.