Other bitwise operators

For completeness, here are the other bitwise operators. These are less commonly used in Objective-C but good to know.

Exclusive-OR

You can exclusive-or (XOR) two bytes together to create a third. A bit in the third byte is 1 if exactly one of the corresponding bits in the input bytes is 1.

Figure 38.5  Two bytes bitwise-XORed together

Two bytes bitwise-XORed together

This is done with the ^ operator. Add to main.c:

 ​ ​ ​ ​u​n​s​i​g​n​e​d​ ​c​h​a​r​ ​e​ ​=​ ​a​ ​^​ ​b​;​ ​ ​ ​ ​p​r​i​n​t​f​(​"​H​e​x​:​ ​%​x​ ​^​ ​%​x​ ​=​ ​%​x​\​n​"​,​ ​a​,​ ​b​,​ ​e​)​;​ ​ ​ ​ ​p​r​i​n​t​f​(​"​D​e​c​i​m​a​l​:​ ​%​d​ ​^​ ​%​d​ ​=​ ​%​d​\​n​"​,​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide 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.