0
0
Embedded Cprogramming~10 mins

Checking if a bit is set in Embedded C - Interactive Code Practice

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

Complete 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'
A1 << 1
B1 << 3
C2 << 1
D1 << 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong bit position shift.
Confusing bit numbering starting from 1 instead of 0.
2fill in blank
medium

Complete 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'
A1 << 4
B4 << 1
C1 << 5
D1 << 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 << 5 which checks the 6th bit.
Not comparing the result to zero.
3fill in blank
hard

Fix 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'
A1 << 2
B1 << 1
C2 << 2
D1 << 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 << 2 which checks bit 2 instead of bit 1.
Using 1 << 0 which checks bit 0.
4fill in blank
hard

Fill 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'
Ai
Bj
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop and bit shift causing errors.
Using undefined variables.
5fill in blank
hard

Fill 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'
Apos
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for bit position.
Using '==' which returns 1 if bit is not set.