Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bit Manipulation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this bitwise AND operation?
Consider the following Python code snippet:

result = 12 & 5
print(result)

What will be printed?
DSA Python
result = 12 & 5
print(result)
A4
B1
C0
D8
Attempts:
2 left
💡 Hint
Think about the binary form of 12 and 5 and where both have 1s.
❓ Predict Output
intermediate
2:00remaining
What is the output after left shifting?
What does this code print?

num = 3
result = num << 2
print(result)
DSA Python
num = 3
result = num << 2
print(result)
A9
B6
C8
D12
Attempts:
2 left
💡 Hint
Left shift by 2 means multiply by 2 twice.
❓ Predict Output
advanced
2:00remaining
What is the output of this XOR and NOT combination?
Analyze this code:

a = 10
b = 7
result = ~(a ^ b)
print(result)

What is printed?
DSA Python
a = 10
b = 7
result = ~(a ^ b)
print(result)
A4
B6
C-14
D-4
Attempts:
2 left
💡 Hint
First find XOR, then apply NOT (bitwise complement).
❓ Predict Output
advanced
2:00remaining
What is the output after right shifting a negative number?
Consider this code:

num = -16
result = num >> 2
print(result)

What is printed?
DSA Python
num = -16
result = num >> 2
print(result)
A-4
B-8
C4
D8
Attempts:
2 left
💡 Hint
Right shift divides by 2, rounding down for negatives.
🧠 Conceptual
expert
3:00remaining
How many bits are set to 1 after this operation?
Given the code:

num = 29
result = num & (num >> 1)
count = bin(result).count('1')
print(count)

What number is printed?
DSA Python
num = 29
result = num & (num >> 1)
count = bin(result).count('1')
print(count)
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Check binary of 29 and 29 shifted right by 1, then AND them.

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

  1. Step 1: Convert numbers to binary

    5 in binary is 0101, 3 in binary is 0011.
  2. Step 2: Perform bitwise AND

    0101 AND 0011 = 0001 (only bits set in both numbers remain set).
  3. Final Answer:

    1 -> Option A
  4. 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

  1. Step 1: Understand left shift operator in Python

    Left shift is done using << operator, shifting bits to the left.
  2. Step 2: Check syntax options

    Only x << 2 correctly shifts bits left by 2 places.
  3. Final Answer:

    x << 2 -> Option C
  4. 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

  1. 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.
  2. Step 2: Convert shifted binary back to decimal

    0011 in binary is 3 in decimal.
  3. Final Answer:

    3 -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Result is -22 due to operator precedence -> Option A
  4. 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

  1. Step 1: Understand toggling bits where numbers differ

    Toggling bits means flipping bits that are different between two numbers.
  2. Step 2: Identify bitwise XOR operation

    XOR (^) returns 1 where bits differ and 0 where bits are same, effectively toggling differing bits.
  3. Final Answer:

    a ^ b -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard