Negetion using bitwise operator

why there is ~0 is coming as -1.

Hello Ankit, see if we want to express a no. in the form of bits we use 0-1 nos. to denote it.
And in that we consider the leftmost bit is sign bit so if there is positive no. then we say the sign bit is 0 else 1 for the negative nos.
Now if you know about how to find out the negative of a no. is 2’s compliment of a no.
It means flip all the bits of the given no. and then add one to it.
And by flipping I mean negation of a no. and then add one to it.
So overall it will be like negative of any positive no. x is ~x+1 so we can say that ~x = x-1
So for ~0 = 0-1
Similarly for ~2 = 2-1 = 1
~5 = 5-1 = 4
I hope it is clear to you. In case it is clear to you pls mark it as resolve and provide the rating as well as feedback so that we can improve ourselves.
In case there is still some confusion pls let me know, I will surely try to help you out.
Thanks :slight_smile:
Happy Coding !!

you are wrong man
~0=-1
~1=-2
~2=-3
…and so on

Yeah you are right, oops sorry for that.
I need to write -x = ~x + 1
so that ~x = -x-1

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.