0

Why output is -1

Void main() { Int i=0; Printf("%d",~i); } And what is the difference between ! And ~

22nd Feb 2019, 4:44 AM
jeevan
jeevan - avatar
4 ответов
+ 6
~ operator is the bitwise NOT. It flips the bits of a value to their complement counterpart. E.g. NOT (0010) = (1101) ! is the boolean NOT operator. The difference with ~ is that ! is used to negate boolean values instead of bits.
22nd Feb 2019, 5:10 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Diego Yeah, I edited my answer there. I forgotten that C++ uses two's complement. To your question, it could be the case where we do ~ on an unsigned integer, in which it depends on the number of bits used to represent the unsigned int (or is it? If we ~ an unsigned int of value 2, it could still be interpreted as -3 but fitted into the unsigned int variable to give a larger number - Seems to be the case, so technically ~N is still -(N+1)).
22nd Feb 2019, 5:15 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Hatsy Rei Isn't ~2 = -3? Under which circumstances does ~N not equal -(N+1)?
22nd Feb 2019, 5:12 AM
Diego
Diego - avatar
0
Hatsy rei why the output is -1 and can you give the example for ! And ~
22nd Feb 2019, 6:14 AM
jeevan
jeevan - avatar