+ 1
What is the concept of "Complement(~)" in C?
Example : (~13) --> (-14)
3 ответов
+ 3
The bitwise complement operator (~) inverts the state of every bit of a numerical value.
For example, 13 (base 10) = 0000'0000'0000'0000'0000'0000'0000'1101 (base 2)
(if int is 4 bytes on your system).
Now, ~13 = 1111'1111'1111'1111'1111'1111'1111'0010.
Now all numbers are represented by two's complement form. This means that if the most significant or the leftmost bit is 1, the number is negative. Otherwise it is positive. The base 10 value is then calculated from the two's complement form by inverting the bits again, converting to base 10, adding the sign based on the leftmost bit in the two's complement form, and subtracting 1 from the total.
So ~13 becomes -13-1 = -14.
Similarly, if the number was -6, ~(-6) will be 6-1 = 5.
+ 2
Reverse bits. zeros to ones and viceversa
+ 2
Thanks for your answers.