Operators
Lesson 58: Using ^ Bitwise XOR Operator
Learn how ^ toggles bits, compares differences, and supports mask-based state flipping in embedded systems.
Progress indicator
Lesson 58 of 58
Learning Objectives
- Understand what bitwise XOR (^) does at bit level.
- Read XOR truth-table output and predict bit results quickly.
- Use XOR masks to toggle selected bits without changing others.
- Compare XOR with logical operators and avoid mixing intent.
- Apply XOR in practical embedded state-flip patterns.
Concept Explanation
What is the Bitwise XOR Operator (^)
The ^ operator compares each bit pair and returns 1 only when bits are different.
Same bits produce 0. Different bits produce 1. This makes XOR very useful for toggling.
Bitwise XOR Syntax
result = valueA ^ valueB;
flags = flags ^ MASK;How ^ Works
- Convert both values to binary.
- Compare each bit position.
- If bits are different, output bit is 1.
- If bits are same, output bit is 0.
Example: 0b1010 ^ 0b1100 = 0b0110.
Bitwise Truth Table
| A | B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Using ^ with Bit Masks
XOR with a mask flips selected bits:
- Mask bit = 1: target bit toggles.
- Mask bit = 0: target bit stays unchanged.
flags = flags ^ 0b00000001; // toggle bit 0
flags = flags ^ 0b00000001; // toggle back^ vs Logical Operators (Comparison)
| Operator | Works on | Use case |
|---|---|---|
^ | Integer bits | Toggle/compare bit differences |
&& | Boolean conditions | Logical AND in if/else |
|| | Boolean conditions | Logical OR in if/else |
When to Use ^
- Toggling LEDs/features in one instruction.
- Comparing which bits changed between two values.
- Implementing compact state flip logic in embedded systems.
Example Code
This sketch toggles LED and logging bits with XOR, then reads current bit states.
const int LED_PIN = 2;
byte flags = 0b00000000;
const byte LED_TOGGLE_MASK = 0b00000001;
const byte LOG_TOGGLE_MASK = 0b00000100;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
flags = flags ^ LED_TOGGLE_MASK;
flags = flags ^ LOG_TOGGLE_MASK;
byte ledBit = flags & LED_TOGGLE_MASK;
byte logBit = flags & LOG_TOGGLE_MASK;
if (ledBit) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
if (logBit) {
Serial.print("flags=");
Serial.print(flags, BIN);
Serial.print(", ledBit=");
Serial.print(ledBit ? 1 : 0);
Serial.print(", logBit=");
Serial.println(logBit ? 1 : 0);
}
delay(600);
}Example Code Explanation
flagsstarts with all bits 0.flags ^ LED_TOGGLE_MASKflips bit 0 every loop.flags ^ LOG_TOGGLE_MASKflips bit 2 every loop.ledBitandlogBitread current bit states with AND masks.- LED output uses the current value of
ledBit. - Serial prints debug details only when
logBitis active.
What Happens Inside
- CPU loads current flags and XOR mask.
- ALU compares each bit pair with XOR rule.
- Selected bits are toggled in result.
- Updated flags are written back to memory.
- Bit masks read back toggled bits for control flow.
| Operation | Before | After | Meaning |
|---|---|---|---|
flags ^ 00000001 | 00000000 | 00000001 | LED bit toggled ON |
flags ^ 00000100 | 00000001 | 00000101 | Log bit toggled ON |
same mask again | 00000101 | 00000000 | Both bits toggled back OFF |
Common Mistakes with ^
- Thinking
^means exponent (power). - Toggling wrong bit due to incorrect mask value.
- Using XOR in boolean if-conditions when logical operators were intended.
- Forgetting that applying same XOR mask twice restores original bits.
Best Practices for ^
- Use clear mask names like
LED_TOGGLE_MASK. - Keep XOR expressions simple and isolate bit checks in separate variables.
- Print binary values to verify which bits toggled.
- Document which bit each mask controls in comments/lesson notes.
Try it now
Open the simulator workspace and observe XOR bit toggling in each loop cycle.