Form

<bitwise-exclusive-or-operator>
^

Semantics

Domains

Bitwise Exclusive Or
int × int → int
long × long → long
unsigned int × unsigned int → unsigned int
unsigned long × unsigned long → unsigned long

Properties

Name
Bitwise Exclusive Or
Verb Form
exclusive or
Commutativity
Bitwise Exclusive Or is commutative
Associativity
Bitwise Exclusive Or is mathematically associative
Bitwise Exclusive Or is syntactically associative left to right.
Side Effect
Bitwise Exclusive Or has no side effects

Remarks

Bitwise OR
Operands Result
0 0 0
0 1 1
1 0 1
1 1 0

Example

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void Display (short A, short B);
void ToBinary (char * Display, short N);

short main () {
  time_t T;
  srand (time (&T));
  Display (2, 7);
  Display (15, 22);
  Display (rand() - 16768, rand() - 16768);
  Display (rand() - 16768, rand() - 16768);
  return 0;
}

void Display (short I, short J) {
  char Binary [17];
  short K;
  K = I ^ J;
  ToBinary (Binary, I);
  printf ("   %6i  %s\n", I, Binary);
  ToBinary (Binary, J);
  printf ("^  %6i  %s\n", J, Binary);
  printf ("   ------  ----------------\n");
  ToBinary (Binary, K);
  printf ("   %6i  %s\n", K, Binary);
  printf ("   ======  ================\n\n");
}

void ToBinary (char * Display, short N) {
  short I;
  I = 15;
  while (I >= 0) {
    Display [I] = (N & 1) + '0';
    N >>= 1;
    --I;
  }
  Display [16] = 0;
}

Output

        2  0000000000000010
^       7  0000000000000111
   ------  ----------------
        5  0000000000000101
   ======  ================

       15  0000000000001111
^      22  0000000000010110
   ------  ----------------
       25  0000000000011001
   ======  ================

      113  0000000001110001
^    8812  0010001001101100
   ------  ----------------
     8733  0010001000011101
   ======  ================

    -3562  1111001000010110
^  -12486  1100111100111010
   ------  ----------------
    15660  0011110100101100
   ======  ================

See Also

Operator Summary   <operator>   Summary
BitwiseAnd Operator   <bitwise-and-operator>   &
BitwiseOr Operator   <bitwise-or-operator>   |

Valid XHTML 1.0 Transitional

Valid CSS

Site Icon Copyright © 1999-2006, jhyoung, revised 2/18/2007