+ 1

What is the concept of "Complement(~)" in C?

Example : (~13) --> (-14)

11th Jul 2019, 3:32 AM
Lakshan Chathuranga
Lakshan Chathuranga - avatar
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.
11th Jul 2019, 3:53 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
Reverse bits. zeros to ones and viceversa
11th Jul 2019, 10:50 AM
Bebida Roja
Bebida Roja - avatar
+ 2
Thanks for your answers.
13th Jul 2019, 1:57 PM
Lakshan Chathuranga
Lakshan Chathuranga - avatar