~ (Bitwise not)
From Microduino Wiki
- Bitwise not (~)
Bitwise not is ~ in C+ +. Different from &(bitwise and) and |(bitwise or), bitwise not is on the right of an operand. Bitwise not changes the operand to its “opposite”:0 changes into 1, and 1 changes into 0. For example :
0 1 operand1 ---------- 1 0 ~ operand1 int a = 103; // Binary: 0000000001100111 int b = ~a; // Binary: 1111111110011000 = -104
You may see the result is a surprised number such as -104. This is because the highest bit of the integer variable is so-called sign bit.
If the highest bit is 1, this number is negative, and the coding of this positive and negative is called complement.
By the way, what is interesting is that you should pay attention to that for any integer operand x, 〜X is equal to -X-1.
Sometimes, implementing bitwise operation to integer operands with signs may cause some unnecessary accidents.