• Home
  • 3.17 Bitwise XOR

3.17 Bitwise XOR

View Categories

3.17 Bitwise XOR

< 1 min read

Table of Contents

The bitwise XOR operator ^ compares each corresponding bit of two integer operands. A result bit is 1 when the corresponding bits are different, and 0 when they are the same.

#include <stdio.h>

int main()
{
    int a = 12;
    int b = 10;

    printf("a ^ b = %d\n", a ^ b);

    return 0;
}

Example Output #

a ^ b = 6

Explanation #

The values are represented in binary as:

12 = 1100
10 = 1010
     ----
     0110

Each pair of corresponding bits is compared:

a b a ^ b
0 0 0
0 1 1
1 0 1
1 1 0

Therefore, 1100 ^ 1010 produces 0110, which is decimal 6.

The bitwise XOR operator produces 1 when the corresponding bits are different.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *