Pro lesson · Preview
Bit Manipulation & Masks
Set, clear, toggle, and test individual bits without disturbing their neighbors: the read-modify-write idioms behind every GPIO and peripheral driver.
25 min read
This lesson is part of Pro
Upgrade to unlock the full lesson and the complete interview Q&A.
Unlock with Pro5 interview questions on this topic
Each one opens a full worked answer.
How do you set, clear, and toggle a single bit without affecting the others?What's the difference between GPIOA->ODR |= (1u << 5) and GPIOA->ODR = (1u << 5)?Why should you write 1u << n instead of 1 << n for a mask?Your code reads a register field with if (reg & MASK == 0) and it never matches. Why?Is reg |= (1u << n) safe to use on a register that an interrupt also modifies?
This is just the start
Sign up free to track your progress through this course, mark lessons complete, and work through the full interview Q&A.
More in C Programming
The C Compilation PipelinePointers & AddressesPointer Arithmetic & Arrays
Dynamic Memory: malloc & free
Stack vs Heap
Structs, Unions & Bitfields