Bitwise complement operator | Sololearn: Learn to code for FREE!

+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

9/22/2021 1:52:19 PM

Tim

4 Answers

New Answer

+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?

+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."

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