+ 3

Bitwise complement operator

Can anyone explain why ~4 equals to -5, and what's happening under the hood? Shouldn't it be a positive integer if every bit is flipped? Any answer is appreciated 👍 https://code.sololearn.com/cWToKRtCEoW1/?ref=app

22nd Sep 2021, 1:52 PM
Tim
Tim - avatar
4 odpowiedzi
+ 4
Time is Money And in which language that ~ work that way? do you have a resource to backup your statement to be shared here, for people to learn?
22nd Sep 2021, 2:03 PM
Ipang
+ 4
Time is Money, Please note that Nick here had tagged C# as the language relevant to the question. The link you provided is in regard to Javascript. Also, it is ccovering the use of ~~ (double tilde symbol) if you read it more carefully, and see the provided examples below the paragraph mentioning that statement. Quoted from the link: "However, the most practical way of utilizing the power of this operator is to use it as a replacement for Math.floor() function as *double bitwise NOT* performs the same operation a lot quicker."
22nd Sep 2021, 2:31 PM
Ipang
0
Sorry for the wrong information ~ is the bitwise NOT operator Bitwise NOT changes each bit to its opposite: 0 becomes 1, and 1 becomes 0. In your case 3 --> 0000000000000011 Covert 0 to 1 and 1 to 0 1111111111111100 Which is equals to -4
22nd Sep 2021, 2:36 PM
Pariket Thakur
Pariket Thakur - avatar