School/□ Systems Engineer/Week 33
Registers and bit masks: where software touches silicon
One line of C turns on a motor driver. Which single bit, at which address, did it change, and what else could it break?
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
Every optimisation in software eventually becomes bits in a register: a clock divider, a sleep mode, a PWM duty. Integration bugs live at this boundary, where a correct algorithm meets a wrongly set bit.
- ◇ Explain how firmware controls hardware through memory-mapped registers.
- ◇ Set, clear, toggle and test individual bits with masks (OR, AND-NOT, XOR, AND).
- ◇ Recognise read–modify–write hazards and why a register write can have side effects.
2.Predict
A control register holds 0b1010_0011. You want to set bit 2 without changing the others. Which operation?
Guess first. Being wrong here is how the surprise works.
3.Discover
In Silicon Studio, build or inspect a small register made of flip-flops with an enable. Write a value, then change a single bit using a mask built from gates. Notice that a 'register' in firmware is exactly this: storage whose outputs drive real hardware.
Can't use the interactive? Read a description instead
In Silicon Studio the learner works with a register built from flip-flops and changes one bit by combining the stored value with a mask through logic gates: OR to set, AND with an inverted mask to clear, XOR to toggle. The point: a firmware register is real storage whose outputs control hardware.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Which C expression clears bit 5 of reg and leaves every other bit unchanged?
5.Understand
Memory-mapped I/O
Microcontrollers give each peripheral (GPIO, timers, ADC, UART) a block of addresses. Writing to an address doesn't store data in RAM; it sets flip-flops whose outputs drive the hardware: a pin's direction, a timer's prescaler, an interrupt enable. Reading may return live hardware state, such as whether a conversion has finished.
The four mask operations
Set bit n: reg |= (1 << n). Clear: reg &= ~(1 << n). Toggle: reg ^= (1 << n). Test: (reg & (1 << n)) != 0. Multi-bit fields (a 3-bit prescaler at bits 4–6) are cleared with a field mask, then the new value is ORed in, shifted into place. Datasheets give each bit's meaning; the code should name them, not use magic numbers.
Read–modify–write hazards
reg |= mask is really three steps: read, OR, write. If an interrupt changes the same register between the read and the write, its change is lost. Fixes: disable interrupts briefly around the update, or use hardware 'set' and 'clear' registers that change only the bits you write as 1. Some status bits clear when read, so even reading has side effects.
Integration means checking both sides
A firmware unit test can't see a wrongly configured clock. Integration tests run real code on real (or accurately simulated) hardware and check observable behaviour: the pin toggles at the expected frequency, the ADC reads a known voltage correctly. Write down what each register should contain and check it in a debugger.
Explain it another way
A register is a row of light switches on a wall you can't see behind. Each switch turns something on in the building. Flip the one you need without brushing the others.
Volatile access: registers are declared volatile so the compiler doesn't cache or reorder accesses. Bit-band or atomic set/clear registers avoid read–modify–write races on some architectures.
Field update: reg = (reg & ~(m << s)) | ((v & m) << s), where m = 2^w − 1 for a w-bit field at shift s. Example: w = 3, s = 4, v = 5: clear mask ~0x70, value 0x50.
Low-power modes are entered by setting a few register bits; a mistaken bit can leave a peripheral clock running and quietly double a sensor node's sleep current, ruining a battery-life budget.
Worked example 1
reg = 0x5A (0101_1010). Clear bit 3, then toggle bit 0. Result?
Clear bit 3: 0101_1010 & 1111_0111 = 0101_0010 (0x52). Toggle bit 0: 0101_0011 = 0x53.
Common mix-ups
- Writing to a register is like writing to a variable.
✓ It changes hardware immediately and can have side effects; reading can too. - reg |= mask is a single, safe operation.
✓ It is read–modify–write and can lose updates made by interrupts in between.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
reg = 0x3C. After reg |= 0x81, what is reg in decimal?
Practice 3
A 3-bit prescaler field sits at bits 4–6 of an 8-bit register that currently holds 0x0F. You write the value 5 into the field. What does the register hold now, in decimal?
Practice 4
Main code does PORT |= LED_BIT while an interrupt handler sets MOTOR_BIT in the same register. Occasionally the motor bit is lost. Why?
7.Apply it
Write a short configuration routine for a hypothetical timer (enable bit 0, 3-bit prescaler at bits 4–6, interrupt enable bit 7) using named masks. Show the register value after each line in hex and binary.
8.Reflect
Concepts: Inside a microcontroller · Binary, hexadecimal and digital signals · Digital input and output