3.1 Set, Clear and Read Register Bits

Setting and clearing register bits are fundamental operations used in embedded programming and represent a technique any application is based on.

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

As it has 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, which means 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, refer to the Bitwise Operators.