0
0
Embedded Cprogramming~20 mins

OR for setting bits in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bitwise Mastery Badge
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 bitwise OR operation?

Consider the following C code snippet that uses bitwise OR to set bits in a byte.

unsigned char flags = 0x12;  // binary: 00010010
flags = flags | 0x04;         // set bit 2
printf("0x%02X", flags);

What will be printed?

Embedded C
unsigned char flags = 0x12;
flags = flags | 0x04;
printf("0x%02X", flags);
A0x10
B0x16
C0x14
D0x04
Attempts:
2 left
💡 Hint

Bitwise OR sets bits that are set in either operand.

Predict Output
intermediate
2:00remaining
What is the value of 'status' after setting bits?

Given this C code:

unsigned int status = 0x0F0F;
status |= 0x00F0;
printf("0x%04X", status);

What is the output?

Embedded C
unsigned int status = 0x0F0F;
status |= 0x00F0;
printf("0x%04X", status);
A0x0FFF
B0x0F0F
C0x00F0
D0x0FF0
Attempts:
2 left
💡 Hint

OR operation sets bits that are set in either operand.

🔧 Debug
advanced
2:30remaining
Why does this code not set the intended bits?

Look at this code snippet:

unsigned char reg = 0x01;
reg = reg | 0x02;
reg = reg & 0xFD;
printf("0x%02X", reg);

The programmer wants to set bit 1 and clear bit 2. What is the output and why?

Embedded C
unsigned char reg = 0x01;
reg = reg | 0x02;
reg = reg & 0xFD;
printf("0x%02X", reg);
A0x02; only bit 1 is set
B0x01; bit 1 was never set
C0x05; bits 0 and 2 are set
D0x03; bit 2 is not cleared because 0xFD clears bit 1 instead
Attempts:
2 left
💡 Hint

Check which bit 0xFD clears in binary.

📝 Syntax
advanced
2:00remaining
Which option correctly sets bits 0 and 3 using OR?

Which of the following C statements correctly sets bits 0 and 3 of variable flags without changing other bits?

Embedded C
unsigned char flags = 0x00;
Aflags |= (1 << 0) | (1 << 3);
Bflags = flags | 1 << 0 | 1 << 3;
Cflags |= 1 << (0 | 3);
Dflags = flags | (1 << 0 & 1 << 3);
Attempts:
2 left
💡 Hint

Remember operator precedence and grouping with parentheses.

🚀 Application
expert
2:30remaining
How many bits are set after this sequence of OR operations?

Given this code:

unsigned char mask = 0x00;
mask |= 1 << 1;
mask |= 1 << 4;
mask |= 1 << 7;

How many bits are set to 1 in mask after these operations?

Embedded C
unsigned char mask = 0x00;
mask |= 1 << 1;
mask |= 1 << 4;
mask |= 1 << 7;
A1
B2
C3
D4
Attempts:
2 left
💡 Hint

Count how many bits are set by each OR operation.