Skip to content
UPEEC

Bit Manipulation in C

Set, clear, toggle and test bits with masks.

Easy
Comments 0
reg |=  (1u << 5);   /* set bit 5 */
reg &= ~(1u << 5);   /* clear */
reg ^=  (1u << 5);   /* toggle */
if (reg & (1u << 5)) /* test */

Use unsigned types for shifts.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.