0
0
Embedded Cprogramming~20 mins

Checking if a bit is set in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bitwise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this bit check code?

Consider the following C code snippet that checks if the 3rd bit (bit index 2) of a number is set. What will be printed?

Embedded C
unsigned int num = 10; // binary 1010
if (num & (1 << 2)) {
    printf("Bit is set\n");
} else {
    printf("Bit is not set\n");
}
ABit is not set
BBit is set
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint

Remember that bits are counted from 0 starting at the least significant bit.

Predict Output
intermediate
2:00remaining
What does this code print when checking bit 0?

Given the code below, what will be the output?

Embedded C
unsigned char val = 1; // binary 00000001
if (val & 1) {
    printf("LSB is set\n");
} else {
    printf("LSB is not set\n");
}
ACompilation error
BLSB is not set
CLSB is set
DRuntime error
Attempts:
2 left
💡 Hint

LSB means least significant bit, which is bit 0.

Predict Output
advanced
2:00remaining
Output of checking bit 5 in a signed integer

What will this code print?

Embedded C
int x = -32; // binary representation depends on two's complement
if (x & (1 << 5)) {
    printf("Bit 5 is set\n");
} else {
    printf("Bit 5 is not set\n");
}
ABit 5 is not set
BBit 5 is set
CUndefined behavior
DCompilation error
Attempts:
2 left
💡 Hint

Remember two's complement representation for negative numbers.

Predict Output
advanced
2:00remaining
What error does this code produce?

What error will this code cause?

Embedded C
unsigned int num = 5;
if (num & (1U << 32)) {
    printf("Bit 32 is set\n");
} else {
    printf("Bit 32 is not set\n");
}
AUndefined behavior due to shift overflow
BNo error, prints "Bit 32 is not set"
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint

Check the size of unsigned int and the shift operation limits.

🧠 Conceptual
expert
3:00remaining
How many bits are set in this mask?

Given the mask 0xAA55 (hexadecimal), how many bits are set to 1 in its 16-bit binary representation?

A12
B6
C10
D8
Attempts:
2 left
💡 Hint

Convert hex to binary and count the 1s.