O'Reilly logo

Objective-C Programming: The Big Nerd Ranch Guide by Mikey Ward, Aaron Hillegass

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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​"​,​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required