Loading
0xF0Lesson 16 of 20

Bits and flags in real devices

Use unsigned masks to read and change individual device settings.

22 min 4-question quiz 1 code exercise
By the end of this lesson you can
  • Read an eight-bit value in binary
  • Set, clear and test one bit without changing others
  • Explain why firmware and compact file formats use masks

C often talks to a device through a register: a small group of bits where each position has a meaning. In a fictional sensor board, bit 0 controls an LED and bit 2 controls an alarm. A mask selects the bit you want. Use unsigned values for bit operations, and keep unrelated bits unchanged.

Try it

Up to 15 XP

Program the sensor board

Solve the three flag missions. Predict the eight bits before each click. The register is simulated here; no hardware is accessed.

Mission 1: Turn on LED (bit 0).

Score 0/3

A fictional eight-bit device register. Bit 0 controls an LED; bit 2 controls an alarm. The other bits must stay unchanged.

b70
b60
b50
b40
b30
b20
b10
b00

uint8_t flags = 0;

Choose a mask operation to begin.

flags.c
1#include <stdint.h>
2#include <stdio.h>
3
4int main(void) {
5    uint8_t flags = 0;
6    flags |= 1u;                 /* set bit 0 */
7    flags |= 1u << 2;            /* set bit 2 */
8    printf("%u\n", (unsigned)flags);
9    flags &= (uint8_t)~1u;       /* clear bit 0 */
10    printf("%u\n", (unsigned)flags);
11    printf("%s\n", (flags & (1u << 2)) ? "alarm on" : "alarm off");
12    return 0;
13}
Output
5
4
alarm on

| turns selected bits on, & ~mask turns selected bits off, and & mask tests them. ^ toggles bits. Shifts like 1u << 2 move a one to bit 2. Keep shift counts within the width of the unsigned type; invalid shifts are undefined behavior. The exercise uses a tiny device model, but the same pattern appears in embedded drivers, protocol headers and file permissions.

Key takeaways

  • A mask selects bit positions without disturbing unrelated flags.

  • Use unsigned values and valid shift counts for bit operations.

  • OR sets, AND with a complemented mask clears, AND tests.

Lesson quiz

4 questions · pass with 3 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write C

These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.

Exercise 1

Apply device commands

+25 XP

Read an initial register value from 0 to 255 and one command: LED_ON, ALARM_ON, or LED_OFF. Change only the relevant bit and print the final decimal value. For example, 1 ALARM_ON prints 5.

  • Set alarm, keep LED
  • Clear LED, keep alarm
  • Set LED, keep other bits
main.c
Loading editor…

Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…