0
0
DSA Pythonprogramming~10 mins

Why Bit Manipulation and When It Beats Arithmetic in DSA Python - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to double the number using bit manipulation.

DSA Python
result = num [1] 1
Drag options to blanks, or click blank then click option'
A<<
B>>
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift (>>) instead of left shift (<<).
Using arithmetic operators instead of bit shifts.
2fill in blank
medium

Complete the code to check if a number is even using bit manipulation.

DSA Python
is_even = (num [1] 1) == 0
Drag options to blanks, or click blank then click option'
A|
B&
C^
D<<
Attempts:
3 left
💡 Hint
Common Mistakes
Using bitwise OR (|) instead of AND (&).
Using shift operators instead of bitwise AND.
3fill in blank
hard

Fix the error in the code to toggle the 3rd bit of a number.

DSA Python
result = num [1] (1 << 2)
Drag options to blanks, or click blank then click option'
A>>
B|
C&
D^
Attempts:
3 left
💡 Hint
Common Mistakes
Using AND (&) which clears bits instead of toggling.
Using OR (|) which sets bits but does not toggle.
4fill in blank
hard

Fill both blanks to create a mask that clears the 4th bit of a number.

DSA Python
mask = ~(1 [1] 3)
result = num [2] mask
Drag options to blanks, or click blank then click option'
A<<
B&
C|
D^
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR (|) instead of AND (&) to clear bits.
Not inverting the mask before AND operation.
5fill in blank
hard

Fill all three blanks to extract bits 2 to 4 (inclusive) from a number.

DSA Python
mask = ((1 [1] 3) - 1) [2] (1 [3] 1)
result = (num & mask) >> 1
Drag options to blanks, or click blank then click option'
A<<
B&
C|
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using AND (&) instead of OR (|) to combine mask parts.
Incorrect shift amounts causing wrong bits to be selected.