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:

    unsigned char e = a ^ b;

    printf("Hex: %x ^ %x = %x\n", a, b, e);
    printf("Decimal: %d ^ %d = %d\n", a, b, e);

    return 0;
}

When you run it you will see:

Hex: 3c ^ a9 = 95
Decimal: 60 ^ 169 = 149

This operator ...

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.