Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
Bitwise Operators in Embedded C: AND, OR, XOR, NOT and Shifts Explained Bit by Bit
Truth tables, bit-level diagrams and compiled examples for & | ^ ~ <>, shifts as multiply and divide, arithmetic versus logical right shift, undefined shift cases, MSB/LSB numbering and precedence traps. Keep this in mind whenever you apply bitwise operators in embedded C.
What You Will Learn
& | ^ ~ << >> and their truth tables~ and shifts on small typesKey Terms
1. Why Embedded C Needs Bitwise Operators
A microcontroller peripheral is controlled by registers, and a register is a 32-bit word in which individual bits (or small groups of bits) mean different things. One bit enables a clock, another selects a pin mode, another reports that a transmission finished. To change one of them without disturbing its neighbors you need operators that work on individual bits. That is the job of the bitwise operators in embedded C. Many register bugs with bitwise operators in embedded C start here.
Where the arithmetic operators treat a variable as a single number, the bitwise operators treat it as a row of independent switches. They compare, combine or move the bits position by position. This is the core rule behind bitwise operators in embedded C.
| Operator | Name | Works on | Typical use |
|---|---|---|---|
& | AND | each bit pair | clear bits, test bits, apply a mask |
| | OR | each bit pair | set bits |
^ | XOR | each bit pair | toggle bits, find differences |
~ | NOT (complement) | each bit | invert, build clear-masks |
<< | left shift | whole value | build a mask, multiply by 2n |
>> | right shift | whole value | extract a field, divide by 2n |
2. Bitwise AND (&)
AND produces a 1 in a result bit only if both input bits are 1. Its two classic jobs are clearing bits (AND with 0 forces a bit to 0) and testing bits (AND with a mask keeps only the bits you care about). Reviewers check bitwise operators in embedded C first in driver code.
| A | B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
- Mask:
x & 0x0Fkeeps only the low 4 bits. - Test:
if (flags & FLAG_READY)is non-zero when that bit is set. - Clear:
x &= ~(1u << n)sets bit n to 0 and leaves the rest (Lesson 5.3). - Odd/even:
n & 1is the lowest bit. - Modulo by a power of two:
x & (N - 1)equalsx % Nfor unsigned x when N is 2, 4, 8, 16 …
3. Bitwise OR (|)
OR produces a 1 if at least one input bit is 1. OR with 1 forces a bit to 1, and OR with 0 leaves it unchanged, which makes OR the natural way to set bits. Interviewers test bitwise operators in embedded C on exactly this point.
| A | B | A | B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
reg |= mask; is safe to repeat, for example in an init function that may run twice.4. Bitwise XOR (^)
XOR (exclusive OR) produces a 1 when the two input bits are different. XOR with 1 flips a bit; XOR with 0 leaves it alone. That makes it the operator for toggling bits and for finding which bits differ between two values.
| A | B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Useful XOR properties
| Property | Expression | Use |
|---|---|---|
| Self-cancelling | x ^ x == 0 | Compare two values: zero means equal |
| Identity | x ^ 0 == x | Leave bits unchanged |
| Double toggle | (x ^ m) ^ m == x | Simple reversible encoding, checksums |
| Commutative and associative | a ^ b ^ a == b | Swap without a temporary (a curiosity) |
a ^= b; b ^= a; a ^= b; swaps two variables without a temporary, but it fails when both names refer to the same object (the value becomes 0) and compilers do a normal swap with registers at least as fast. Use a temporary variable.5. Bitwise NOT (~) and the Promotion Trap
The complement operator ~ is unary and flips every bit: 0 becomes 1 and 1 becomes 0. Combined with AND it builds the “everything except this bit” mask used to clear bits: reg &= ~(1u << n);.
| A | ~A |
|---|---|
| 0 | 1 |
| 1 | 0 |
There is a subtle trap with small types. Operands narrower than int are promoted to int before the operator is applied (Lesson 3.3). The complement of a promoted byte therefore has 32 bits:
uint8_t or uint16_t with a constant, cast the result back: (uint8_t)~x. This is also why MISRA C restricts ~ and shifts on small unsigned types.6. Shift Operators: << and >>
Shifts slide the whole bit pattern left or right by a number of positions. x << n moves every bit n places toward the MSB and fills the vacated low bits with 0. x >> n moves bits toward the LSB; the bits that fall off the end are lost.
Shift as fast multiplication and division
Shifting left by n multiplies an unsigned value by 2n (as long as no 1-bit is pushed out of the top); shifting right by n divides by 2n, discarding the remainder.
| Expression | Same as | Example (x = 12) |
|---|---|---|
x << 1 | x * 2 | 24 |
x << 3 | x * 8 | 96 |
x >> 1 | x / 2 | 6 |
x >> 2 | x / 4 | 3 |
x >> 3 | x / 8 (truncated) | 1 |
x * 8 and x / 8 into shifts automatically. Write the shift when you mean “move bits” (building masks, extracting fields), and write * or / when you mean arithmetic.7. Arithmetic vs Logical Right Shift and Undefined Shifts
When the operand of >> is unsigned, the vacated high bits are filled with 0 (a logical shift). When the operand is a negative signed value, the C standard says the result is implementation-defined. GCC for ARM, like almost every compiler, performs an arithmetic shift: the sign bit is copied into the vacated positions so that the value stays negative.
Shift cases that are undefined behavior
| Case | Example | Problem |
|---|---|---|
| Count ≥ width of the (promoted) type | 1u << 32 on a 32-bit unsigned | Undefined; ARM hardware uses only the low byte of the count and gives 0 for 32 or more, but the compiler may do anything |
| Negative shift count | x >> -1 | Undefined |
| Left shift of a negative signed value | -1 << 3 | Undefined (C11) |
| Left shift that overflows a signed type | 1 << 31 as int | Undefined; use 1u << 31 |
1u << n, not 1 << n, and keep the operand of >> unsigned when you want a logical shift. For a mask wider than 32 bits use 1ULL.8. MSB, LSB and Bit Numbering
The LSB (least significant bit) is bit 0, the rightmost bit, worth 1. The MSB (most significant bit) is the leftmost, bit 7 of a byte or bit 31 of a word; in a signed type it is the sign bit. Reference manuals number bits from 0 on the right, exactly as C shifts do, so 1u << n is the mask for bit n.
1u << 31 = 0x80000000 · bit 0 = 1u << 0 = 0x00000001Extracting bytes and fields with shift + mask
The pattern (value >> position) & mask reads a field; the reverse, (field & mask) << position, writes one. Lesson 5.3 turns these into the five standard register idioms.
9. Bitwise vs Logical Operators
| Bitwise | Logical | |
|---|---|---|
| Symbols | & | ^ ~ | && || ! |
| Operates on | each bit position separately | the whole value as true/false |
| Result | a number of the same width | exactly 0 or 1 |
| Short-circuits? | No: both operands always evaluated | Yes |
| Example: 6 and 3 | 6 & 3 == 2 | 6 && 3 == 1 |
Precedence traps with bitwise operators
- Comparison binds tighter than
&,^and|.x & 4 == 4meansx & (4 == 4). Write(x & 4) == 4. - Shifts bind tighter than comparison, and
+ -bind tighter than shifts:1 << n + 1is1 << (n + 1). - Bitwise precedence order:
&, then^, then|. Parenthesise when mixing them.
10. Common Mistakes With Bitwise Operators
if (reg & MASK == MASK) tests the wrong thing.1 << 31 overflows int. Use 1u.~x for a uint8_t is a 32-bit int; compare with a cast.1u << 32 is undefined; check the count when it comes from data.-7 >> 1 is -4 on GCC (rounds toward minus infinity) while -7 / 2 is -3.1 << 40 is undefined; use 1ULL.11. Best Practices for Bitwise Operators in Embedded C
- Use unsigned types (
uint8_t,uint16_t,uint32_t) for anything you shift or mask. - Write masks as
(1u << n)or named constants (#define LED_PIN (1u << 12)) and never as bare numbers. - Parenthesise every bitwise sub-expression.
- Use hexadecimal for masks and registers so the bits are visible:
0x0000F000. - Cast back to the narrow type after
~or a left shift onuint8_t/uint16_t. - Prefer
*and/for arithmetic; use shifts only for bit work. - Test with the boundary bits (0, 7, 15, 31) when you write a new mask expression.
- Name your fields: use CMSIS macros such as
GPIO_ODR_OD12instead of magic numbers.
Key Takeaways — Lesson 5.2
- Bitwise operators in embedded C act position by position:
&(both),|(either),^(different),~(invert). - AND clears or tests bits, OR sets bits, XOR toggles bits, NOT builds clear-masks.
x << nmultiplies an unsigned value by 2n;x >> ndivides by 2n.- Right shift of a negative signed value is implementation-defined (arithmetic on GCC); shifting by the type width or more is undefined.
~and shifts promote small types toint; cast the result back.- Bit 0 is the LSB;
1u << nis the mask for bit n. - Bitwise and logical operators are not interchangeable, and comparison binds tighter than
&,^,|.
Frequently Asked Questions
Ready to practise?
Next: truth tables with worked examples, shift visualisations, byte-level demos and programs using bit operations.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments