The Bitwise Exclusive-OR Operator

The bitwise exclusive-OR operator, which is often called the XOR operator, works as follows: For corresponding bits of the two operands, if either bit is a 1 (but not both bits), the corresponding bit of the result is a 1; otherwise, it is a 0. The truth table for this operator is as shown.

b1      b2    b1 ^ b2———————————————————————0       0        00       1        11       0        11       1        0

If w1 and w2 were set equal to hexadecimal 5e and d6, respectively, the result of w1 exclusive-ORed with w2 would be hexadecimal e8, as illustrated:

w1    0000 0000 0101  1110     0x5ew2    0000 0000 1011  0110   ^ 0xd6————————————————————————————————————      0000 0000 1110  1000     0xe8 ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.