Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to perform bitwise AND of two numbers.
DSA Go
result := a [1] bAttempts:
3 left
💡 Hint
Common Mistakes
Using '|' which is bitwise OR instead of AND.
Using '^' which is bitwise XOR instead of AND.
✗ Incorrect
The '&' operator performs bitwise AND, which sets each bit to 1 only if both bits are 1.
2fill in blank
mediumComplete the code to perform bitwise XOR of two numbers.
DSA Go
result := x [1] yAttempts:
3 left
💡 Hint
Common Mistakes
Using '&' which is bitwise AND instead of XOR.
Using '|' which is bitwise OR instead of XOR.
✗ Incorrect
The '^' operator performs bitwise XOR, which sets each bit to 1 only if the bits are different.
3fill in blank
hardFix the error in the code to perform left shift by 3 bits.
DSA Go
shifted := num [1] 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>>' which is right shift instead of left shift.
Using '^' which is XOR instead of shift.
✗ Incorrect
The '<<' operator shifts bits to the left, multiplying the number by 2 to the power of the shift count.
4fill in blank
hardFill both blanks to create a mask with 1s in the lowest 4 bits and clear other bits.
DSA Go
mask := (1 [1] 4) [2] 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' which gives wrong mask.
Using '^' which is XOR and does not create mask.
✗ Incorrect
Shift 1 left by 4 bits to get 0b10000, then subtract 1 to get 0b1111 which is the mask for lowest 4 bits.
5fill in blank
hardFill all three blanks to toggle the 2nd bit of number n.
DSA Go
mask := 1 [1] 1 result := n [2] mask final := ^result [3] mask
Attempts:
3 left
💡 Hint
Common Mistakes
Using AND instead of OR to set bits.
Using right shift instead of left shift for mask.
✗ Incorrect
Shift 1 left by 1 to get mask for 2nd bit, XOR toggles that bit in n, OR with mask sets that bit.
