IT lexicon › Hardware › Read-modify-write

Read-modify-write

Hardware På svenska → Updated: 2026-11-03

Reading a value, changing a few bits and writing it back — three steps that look like one in C, and therefore break when something gets in between.

GPIOA->ODR |= (1 << 5); compiles to a load, an OR and a store. If an ISR changes a different bit in the same register between the load and the store, its change is silently overwritten. The bug is rare, timing-dependent and invisible in the source.

The fixes live close to the hardware: STM32 has a separate BSRR register where a write sets or clears individual pins without reading first, Cortex-M3/M4 offer bit-banding, and failing that you disable interrupts around the operation or use an atomic operation. A second trap is write-1-to-clear status registers, where an innocent RMW of one bit also clears every other flag that happened to be set.

← Back to the lexicon