Home / Learn Embedded C / AND, OR, XOR, NOT Refresher

AND, OR, XOR, NOT Refresher

The four core bitwise operators and their truth tables.

The short versionThe 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

Trying all four operators on the same two valuesC: edit and run

Printing bit-by-bit like this makes the result of each operator visible without needing a special format specifier for binary.

OperatorRuleExample
& (AND)1 only if both bits are 10b1100 & 0b1010 = 0b1000
| (OR)1 if either bit is 10b1100 | 0b1010 = 0b1110
^ (XOR)1 if the bits differ0b1100 ^ 0b1010 = 0b0110
~ (NOT)flips every bit~0b00001111 = 0b11110000 (for an 8-bit value)

Try it: combining operators to clear then set a field

Using NOT, AND and OR together in one expressionC: edit and run

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)?