0

# Why output is -1

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

4 Answers

+ 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.

+ 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)).

+ 2

Hatsy Rei Isn't ~2 = -3? Under which circumstances does ~N not equal -(N+1)?

0

Hatsy rei why the output is -1 and can you give the example for ! And ~