Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the 3rd bit of the variable 'flags'.
Embedded C
flags = flags [1] (1 << 2);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using & will clear bits instead of setting them.
Using >> is a shift operator, not for setting bits.
✗ Incorrect
Using the bitwise OR operator | sets the specific bit without changing others.
2fill in blank
mediumComplete the code to clear the 1st bit of the variable 'flags'.
Embedded C
flags = flags [1] ~(1 << 0);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using | will set bits instead of clearing.
Using ^ toggles bits, not guaranteed to clear.
✗ Incorrect
Using bitwise AND & with the negated bit mask clears the bit.
3fill in blank
hardFix the error in the code to toggle the 2nd bit of 'flags'.
Embedded C
flags = flags [1] (1 << 1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using & will clear bits instead of toggling.
Using | will set bits instead of toggling.
✗ Incorrect
Bitwise XOR ^ toggles the bit: if it was 1, it becomes 0; if 0, it becomes 1.
4fill in blank
hardFill both blanks to check if the 4th bit of 'flags' is set.
Embedded C
if ((flags [1] (1 << 3)) [2] 0) { // bit is set }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using | instead of & will not isolate the bit.
Using == 0 checks if bit is clear, not set.
✗ Incorrect
Use bitwise AND & to isolate the bit, then check if result is not equal != to zero.
5fill in blank
hardFill all three blanks to create a mask for bits 0 to 3 and clear them in 'flags'.
Embedded C
mask = (1 [1] 4) - 1; flags = flags [2] [3]mask; // bits 0-3 cleared
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using | instead of & will set bits instead of clearing.
Not negating mask will clear wrong bits.
✗ Incorrect
Shift 1 left by 4 to get 16, subtract 1 to get mask 0b1111. Then clear bits by AND with negated mask.