Int x=~10 is given so ~(1010)=0101=5. So why answer is -11

Why it is -11 when we do it on paper it should be 5?

hello @pd.pankajdahiya

~ 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.
similary for ~10 is -11

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.