Bit XOR operation - C Operator

C examples for Operator:Bit Operator

Introduction

The bitwise XOR operator, ^, produces a 1 if both bits are different, and 0 if they're the same.

Demo Code

#include <stdio.h> 

int main(void) { 
    /* ww w  .j a va 2s  .c  o m*/
    int x = 13;
    int y = 6;
    int z = x ^ y;                     // Exclusive OR the bits of x and y
    
    printf("%d",z);
    return 0; 
} 

Result

This results in z containing the value 11 (binary 1011), because the bits combine as follows:

x          0       0       0      0      1       1       0       1
y          0       0       0      0      0       1       1       0
x ^ y      0       0       0      0      1       0       1       1

Related Tutorials