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 its use can help make a program ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access