Bytes for Beginners (Part V.iv): XOR (^)



Swift Advanced Operators

No iPad Pro today, so I decided to cheer you all up with a third bytes for beginners post instead. Hopefully it will keep you occupied while you wait for Yosemite to appear in your updates on the App Store.

XOR (^)

In this example, the blue and green rows are independent of one another while the grey row is the resulting XOR. A XOR is an 'exclusive' OR, or to put it simply an OR that returns a 0 bit where both numbers have either a 0 or a 1 in the same position as one another. But returns a 1 if only one of the numbers contains a 1 in that bit position. This is demonstrated in the interactive example below.

0b 0 0 0 0 0 0 0 0 Val
0 0 0 0 0 0 0 0 0
0b 1 1 1 1 1 1 1 1 Val
128 64 32 16 8 4 2 1 255

Result

0b 1 1 1 1 1 1 1 1 Val
128 64 32 16 8 4 2 1 255

You can change the numbers by clicking on the blue or green squares. The blue and green rows display 8-bit binary numbers. The grey row is the resulting XOR.

What's next?

Bit shifting coming soon.

Endorse on Coderwall