Set, Clear, Toggle and Check Bit Value in C


In the embedded system design, the system needs to check the input state and then send signals to other devices, or change the state of the outputs. Since each GPIO pin is connected to a different device (some pins are input and other pins may be output or other functions), it is not possible to check the pin state by comparing with a constant value. The setting, clearing, and toggling pins also have the same situation: constant values can not be assigned directly to the port DATA register. Because each GPIO pin is associated with the corresponding bit in the port DATA register, a bitwise operation can be used to solve these issues.

Figure 1
: Example of Embedded System



Bit Access Functions

The following functions get or set a particular bit of a variable that can make programs easier to read.