The Ones Complement Operator

The ones complement operator is a unary operator, and its effect is to simply “flip” the bits of its operand. Each bit of the operand that is a 1 is changed to a 0, and each bit that is a 0 is changed to a 1. The truth table is provided here simply for the sake of completeness.

b1   ~b1————————0     11     0

If w1 is a short int that is 16 bits long and is set equal to hexadecimal a52f, taking the ones complement of this value produces a result of hexadecimal 5ad0:

 w1   1010  0101  0010  1111    0xa52f~w1   0101  1010  1101  0000    0x5ad0

The ones complement operator proves useful when you don’t know the precise bit size of the quantity that you are dealing with in an operation, and ...

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.