Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the 3rd bit (bit 2) of variable 'flags' is set.
Embedded C
if (flags & [1]) { // bit is set }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong bit position shift.
Confusing bit numbering starting from 1 instead of 0.
✗ Incorrect
To check the 3rd bit (bit 2), we use 1 shifted left by 2 positions: 1 << 2.
2fill in blank
mediumComplete the code to check if the 5th bit (bit 4) of 'status' is set.
Embedded C
if ((status & [1]) != 0) { // bit is set }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
1 << 5 which checks the 6th bit.Not comparing the result to zero.
✗ Incorrect
The 5th bit is bit 4 (counting from 0), so use 1 << 4.
3fill in blank
hardFix the error in the code to correctly check if bit 1 of 'reg' is set.
Embedded C
if (reg & [1]) { // bit is set }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
1 << 2 which checks bit 2 instead of bit 1.Using
1 << 0 which checks bit 0.✗ Incorrect
Bit 1 means shifting 1 by 1: 1 << 1.
4fill in blank
hardFill both blanks to complete the for loop that maps each bit position to whether it is set in 'value'.
Embedded C
for (int [1] = 0; [1] < 8; [1]++) { bits[[1]] = (value & (1 << [2])) != 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop and bit shift causing errors.
Using undefined variables.
✗ Incorrect
Use the same loop variable i for both the loop and the bit shift.
5fill in blank
hardFill all three blanks to create a function that returns 1 if the bit at 'pos' in 'num' is set, else 0.
Embedded C
int is_bit_set(unsigned int num, int [1]) { return (num & (1 << [2])) [3] 0 ? 1 : 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for bit position.
Using '==' which returns 1 if bit is not set.
✗ Incorrect
The function uses pos as parameter and bit position, and checks if the bit is not zero.