Home / Learn Embedded C / AND, OR, XOR, NOT Refresher
AND, OR, XOR, NOT Refresher
The four core bitwise operators and their truth tables.
Theory in plain words
Definition
AND (&) is 1 only if both bits are 1; OR (|) is 1 if either bit is 1; XOR (^) is 1 if the bits differ; NOT (~) flips every bit.
Why you need this
These four operators, applied with the right mask, are literally the entire toolkit behind setting, clearing, toggling and checking bits — everything in this group is built from them.
How it works
Each operator works bit-by-bit down the whole value at once: 0b1100 & 0b1010 compares each column independently, giving 0b1000.
Where you'll see this
- Register manipulation
- Protocol flag combination
- Masking operations
Try it
Printing bit-by-bit like this makes the result of each operator visible without needing a special format specifier for binary.
| Operator | Rule | Example |
|---|---|---|
| & (AND) | 1 only if both bits are 1 | 0b1100 & 0b1010 = 0b1000 |
| | (OR) | 1 if either bit is 1 | 0b1100 | 0b1010 = 0b1110 |
| ^ (XOR) | 1 if the bits differ | 0b1100 ^ 0b1010 = 0b0110 |
| ~ (NOT) | flips every bit | ~0b00001111 = 0b11110000 (for an 8-bit value) |
Try it: combining operators to clear then set a field
This single line uses all four operators from this lesson together: NOT to build a clearing mask, AND to clear and to constrain, OR to merge the pieces back.
Test what you just read
1. What does XOR (^) produce for two bits that are the same (both 0 or both 1)?
XOR is 1 only when the two bits differ; two matching bits always XOR to 0.