cout<<(~5);
how the output of this becomes -6?
5=101 after flipping it is 010 which is 2
I am not able to understand kindly help
Please explain
Not operator in c++
@sagar_aggarwal
consider this example:-
int a = 103; // binary: 0000000001100111
int b = ~a; // binary: 1111111110011000 = -104
You might be surprised to see a negative number like -104 as the result of this operation. This is because the highest bit in an int variable is the so-called sign bit. If the highest bit is 1, the number is interpreted as negative
This encoding of positive and negative numbers is referred to as two’s complement. For more information, see the Wikipedia article on two’s complement.
why are you considering so many 0’s before 1100111
@sagar_aggarwal
There will be many more zeroes before
because a is integer, which is 4bytes i.e. 32 bits. So the binary representation of int variable will be of 32 bits. So there will be preceeding zeros. This is how data is stored in memory
1 Like