Set, Clear and Read Register Bits

Setting and clearing register bits are fundamental operations used in embedded programming. Applications are based on this technique.

The Read-Modify-Write (RMW) operations are a class of atomic operations that both read a memory location and write a new value to it simultaneously, either with a completely new value or some part of the previous value.

Since it has a wide applicability, reading the value of a bit is mainly used in conditional expressions (e.g. if statement) and as a condition in loop expressions (e.g. while statement). A common use case of this technique is polling on an interrupt flag (reading the value of the bit and executing a set of instructions if the bit is set/clear).

Note: For further details on binary arithmetic see Bitwise Operators.