CBitwise OperatorsBitwise XOR

Understanding the Bitwise XOR Operator (^) in C: A Beginner’s Guide

The bitwise XOR operator (^) in C is a powerful tool for manipulating binary data. XOR stands for “Exclusive OR,” and it is widely used in cryptography, toggling bits, and error detection mechanisms. Understanding how XOR works is essential for low-level programming tasks and bitwise operations.

In this blog, we’ll explore the bitwise XOR operator in detail, covering syntax, examples, edge cases, and practical applications.


1. What is the Bitwise XOR Operator (^)?

The bitwise XOR operator performs a bit-by-bit comparison between two numbers. For each corresponding pair of bits:

  • If the bits are different, the result is 1.
  • If the bits are the same, the result is 0.

Syntax:

result = operand1 ^ operand2;

Bitwise XOR Truth Table:

Bit 1Bit 2Result (Bit 1 ^ Bit 2)
000
011
101
110

2. Basic Example

Let’s start with a simple example to understand how the bitwise XOR operator works with binary numbers.

main.c
#include <stdio.h>
 
int main() {
    int a = 5;  // Binary: 0101
    int b = 3;  // Binary: 0011
 
    int result = a ^ b;  // Binary: 0101 ^ 0011 = 0110 (Decimal 6)
 
    printf("Result of %d ^ %d = %d\n", a, b, result);
    return 0;
}

Output:

Result of 5 ^ 3 = 6

Explanation:

  • Binary of 5: 0101
  • Binary of 3: 0011
  • Bitwise XOR: 0110 (Decimal 6)

3. Practical Applications of Bitwise XOR

1. Toggling Specific Bits

The XOR operator can toggle (invert) specific bits in a number without affecting other bits.

main.c
#include <stdio.h>
 
int main() {
    int number = 10;       // Binary: 1010
    int mask = 5;          // Binary: 0101
 
    int result = number ^ mask;  // Toggles bits where the mask has 1s
 
    printf("Result after toggling bits: %d\n", result);
    return 0;
}

Output:

Result after toggling bits: 15

Explanation:

  • 10: 1010
  • 5: 0101
  • XOR: 1111 (Decimal 15)

2. Swapping Two Numbers Without a Temporary Variable

One of the most famous applications of XOR is swapping two numbers without using a temporary variable.

main.c
#include <stdio.h>
 
int main() {
    int x = 3, y = 5;
 
    x = x ^ y;
    y = x ^ y;
    x = x ^ y;
 
    printf("After swapping: x = %d, y = %d\n", x, y);
    return 0;
}

Output:

After swapping: x = 5, y = 3

Explanation:

  1. x = x ^ y: Stores the XOR of x and y in x.
  2. y = x ^ y: Since x now holds x ^ y, this operation gives y = (x ^ y) ^ y = x.
  3. x = x ^ y: Now x = (x ^ y) ^ x = y.

This method is efficient and avoids the need for extra storage.


3. Checking if Two Numbers are Equal

If a ^ b equals zero, then a and b are identical.

main.c
#include <stdio.h>
 
int main() {
    int a = 10, b = 10;
 
    if ((a ^ b) == 0) {
        printf("Numbers are equal.\n");
    } else {
        printf("Numbers are not equal.\n");
    }
    return 0;
}

Output:

Numbers are equal.

4. Edge Cases and Considerations

1. XOR with Zero

Any number XORed with 0 remains unchanged:

main.c
#include <stdio.h>
 
int main() {
    int a = 7;
    printf("%d ^ 0 = %d\n", a, a ^ 0);
    return 0;
}

Output:

7 ^ 0 = 7

2. XOR with Itself

Any number XORed with itself results in 0:

main.c
#include <stdio.h>
 
int main() {
    int a = 12;
    printf("%d ^ %d = %d\n", a, a, a ^ a);
    return 0;
}

Output:

12 ^ 12 = 0

3. XOR is Commutative and Associative

The order in which you XOR the numbers doesn’t matter.

  • Commutative: a ^ b = b ^ a
  • Associative: (a ^ b) ^ c = a ^ (b ^ c)

5. Operator Precedence and Associativity

The bitwise XOR (^) operator has lower precedence than arithmetic and relational operators but higher precedence than the logical AND (&&) and OR (||) operators.

Precedence Example:

main.c
#include <stdio.h>
 
int main() {
    int a = 5, b = 3, c = 2;
 
    int result = a ^ b + c;  // Evaluated as a ^ (b + c)
 
    printf("Result: %d\n", result);
    return 0;
}

Output:

Result: 4

Explanation: The addition (b + c) is evaluated first, then XOR with a.


Summary

  • The bitwise XOR operator (^) performs an exclusive OR operation on each pair of corresponding bits in two operands.
  • It is useful for toggling bits, swapping values without a temporary variable, and checking equality.
  • XOR’s properties, such as being commutative and associative, make it a versatile tool in various programming scenarios.

Mastering the bitwise XOR operator equips you with a powerful tool for low-level bit manipulation and efficient coding techniques!