Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the 3rd bit of the variable num.
C
num = num | (1 << [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 instead of 2 for the bit position.
Confusing bit positions starting from 1 instead of 0.
✗ Incorrect
Bits are counted from 0, so the 3rd bit is at position 2. Shifting 1 by 2 sets the 3rd bit.
2fill in blank
mediumComplete the code to clear (set to 0) the 5th bit of num.
C
num = num & ~(1 << [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 instead of 4 for the bit position.
Forgetting to negate the mask before AND operation.
✗ Incorrect
The 5th bit is at position 4 (counting from 0). We shift 1 by 4 and then invert it to clear that bit.
3fill in blank
hardFix the error in the code to toggle the 1st bit of num.
C
num = num [1] (1 << 0);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using AND or OR instead of XOR for toggling.
Using right shift operator which does not toggle bits.
✗ Incorrect
The XOR operator (^) toggles bits. Using it with 1 shifted by 0 toggles the 1st bit.
4fill in blank
hardFill both blanks to check if the 4th bit of num is set (1).
C
if ((num [1] (1 << [2])) != 0) { // bit is set }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR instead of AND for checking bits.
Using wrong bit position like 3 instead of 4.
✗ Incorrect
Use bitwise AND (&) with 1 shifted by 4 to check if the 4th bit is set.
5fill in blank
hardFill all three blanks to create a mask that keeps only the lower 3 bits of num.
C
mask = (1 << [1]) [2] 1; result = num [3] mask;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR instead of AND to apply the mask.
Using addition instead of subtraction in mask creation.
Using wrong bit count for shifting.
✗ Incorrect
Shift 1 by 3, subtract 1 to get 0b0111 mask. Use AND to keep lower 3 bits.