how is the value of ~10 calculated?
How is the value of ~10 calculated
hello @be10046.19
The bitwise operator ~ (pronounced as tilde) is a complement operator. It takes one bit operand and returns its complement. If the operand is 1, it returns 0, and if it is 0, it returns 1
For example if a=60 (0011 1100 in binary) its complement is -61 (-0011 1101) stored in 2’s complement
similarly ~10 will be -11
means we add 1 to the binary representation of the number and change the sign?
this is what we do only?
yeah u can get answer like that.
this is what actually happens
but answer will always be -(ans+1)
if this not clears ur doubt then refer this .
~ is a 1’s complement operator.
7 ==> 00000000000000000000000000000111
~7==>111111111111111111111111111111111111111111000 which is equal to -8.
How -8 will be stored in computer memory ?
Negative numbers are stored in the form of 2’s complement.
2’s complement can be calculated by adding +1 to 1’s complement of a number.
8 ==> (00000000000000000000000000001000)
1’s complement of 8 ==> (11111111111111111111111111110111)
Add +1 11111111111111111111111111110111 + 1 ==>11111111111111111111111111111000 (-8)
So ~7 binary representation is equals to -8 binary representation.
So ~7 is -8.