0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Use Bitwise XOR in Embedded C: Syntax and Examples

In Embedded C, use the ^ operator to perform a bitwise XOR between two integers. It compares each bit of the operands and returns 1 if the bits differ, otherwise 0. For example, result = a ^ b; computes the XOR of a and b.
📐

Syntax

The bitwise XOR operator in Embedded C is ^. It takes two integer operands and compares their bits one by one.

  • Operand 1: First integer value.
  • Operand 2: Second integer value.
  • Result: A new integer where each bit is 1 if the corresponding bits of operands differ, else 0.
c
result = operand1 ^ operand2;
💻

Example

This example shows how to use bitwise XOR to toggle bits and combine values in Embedded C.

c
#include <stdio.h>

int main() {
    unsigned char a = 0b10101010;  // 170 in decimal
    unsigned char b = 0b11001100;  // 204 in decimal
    unsigned char result = a ^ b;  // XOR operation

    printf("a = 0b%08X (decimal %d)\n", a, a);
    printf("b = 0b%08X (decimal %d)\n", b, b);
    printf("a ^ b = 0b%08X (decimal %d)\n", result, result);

    // Toggle bits example
    unsigned char toggle_mask = 0b00001111;
    unsigned char toggled = a ^ toggle_mask;
    printf("Toggle lower 4 bits of a: 0b%08X (decimal %d)\n", toggled, toggled);

    return 0;
}
Output
a = 0b10101010 (decimal 170) b = 0b11001100 (decimal 204) a ^ b = 0b01100110 (decimal 102) Toggle lower 4 bits of a: 0b10100101 (decimal 165)
⚠️

Common Pitfalls

Common mistakes when using bitwise XOR in Embedded C include:

  • Using ^ instead of logical XOR (!= for booleans).
  • Applying XOR on signed integers without considering sign bits.
  • Confusing XOR with OR (|) or AND (&) operators.
  • Forgetting that XOR with the same value twice restores the original value.
c
#include <stdio.h>

int main() {
    int x = 5;  // 0101 in binary
    int y = 3;  // 0011 in binary

    // Wrong: Using logical XOR (^) on booleans or expecting logical behavior
    // int wrong = x ^ y; // This is bitwise XOR, not logical XOR

    // Correct: Use ^ for bitwise XOR
    int correct = x ^ y;
    printf("Bitwise XOR of %d and %d is %d\n", x, y, correct);

    // Demonstrate XOR property
    int original = 10;
    int key = 7;
    int encrypted = original ^ key;
    int decrypted = encrypted ^ key;  // XOR again with same key
    printf("Original: %d, Encrypted: %d, Decrypted: %d\n", original, encrypted, decrypted);

    return 0;
}
Output
Bitwise XOR of 5 and 3 is 6 Original: 10, Encrypted: 13, Decrypted: 10
📊

Quick Reference

OperatorDescriptionExampleResult
^Bitwise XOR5 ^ 36 (0101 ^ 0011 = 0110)
a ^ aXOR with same value7 ^ 70 (all bits cancel out)
a ^ 0XOR with zero9 ^ 09 (value unchanged)
Toggle bitsXOR with mask toggles bitsa ^ maskBits flipped where mask is 1

Key Takeaways

Use the ^ operator to perform bitwise XOR between two integers in Embedded C.
XOR returns 1 for bits that differ and 0 for bits that are the same.
XORing a value twice with the same number restores the original value.
Avoid confusing bitwise XOR (^) with logical operators.
XOR is useful for toggling bits and simple encryption in embedded systems.