Why is it that ~2 is equal to -3? How does ~
operator work?
19 Answers
Remember that negative numbers are stored as the two's complement of the positive counterpart. As an example, here's the representation of -2 in two's complement: (8 bits)
1111 1110
The way you get this is by taking the binary representation of a number, taking its complement (inverting all the bits) and adding one. Two starts as 0000 0010, and by inverting the bits we get 1111 1101. Adding one gets us the result above. The first bit is the sign bit, implying a negative.
So let's take a look at how we get ~2 = -3:
Here's two again:
0000 0010
Simply flip all the bits and we get:
1111 1101
Well, what's -3 look like in two's complement? Start with positive 3: 0000 0011, flip all the bits to 1111 1100, and add one to become negative value (-3), 1111 1101.
So if you simply invert the bits in 2, you get the two's complement representation of -3.
The complement operator (~) JUST FLIPS BITS. It is up to the machine to interpret these bits.
-
49One other thing maybe to mention is that the flip is called 1s complement, before adding the 1. – Chris S Apr 26 '09 at 19:31
-
3It might helps others who are not aware of One's Complement and Two's Complement. Read about them here. http://en.wikipedia.org/wiki/Ones%27_complement http://en.wikipedia.org/wiki/Two%27s_complement – Sai Dec 21 '14 at 03:40
-
1Isn't that the bitwise NOT operator? – Braden Best Jan 20 '15 at 07:14
-
3How does the machine know it is getting a two complement negative number instead of a higher positive number? Is it because of the type system of the respective language indicating that the type is a signed int versus unsigned? – GL2014 Feb 20 '16 at 22:05
-
@GL2014 I think you answered your own question there. In my understanding, it's how the machine was designed to work on in the first place. – geekidharsh Jul 29 '18 at 19:45
-
Just one question : How can we tell for example that `1111 1101` gives us -3 and not 253. How do we know it's supposed to be negative when just looking at the binary representation ? – Ellone Nov 28 '18 at 09:19
-
@Ellone your question was addressed in the answer: "The first bit is the sign bit, implying a negative." – mathematrucker Apr 11 '19 at 14:01
-
@BradenBest it is often referred to as that, yes. It provides the inverse of the value - i.e. any bits that are NOT value 1 in the operand will be value 1 in the result, the same is true for bits that are NOT value 0 in the operand becoming value 0 in the result. – IAmJersh Apr 23 '19 at 12:14
-
Unfortunately this answer didn't clarify things for me. Only now that I understand what's going on, I see what you meant: "`~2 == 0b..101`, `-3 == 0b..101`, therefore `~2 == -3`". It's best to explain things as they happen in real life. `~2 == 0b..101`. It's a negative number (`MSB == 1`). To output a negative number, `ruby` prints `-`, then two's complement of the value. For more details, check out [my answer](https://stackoverflow.com/a/58699443/52499) – x-yuri Nov 04 '19 at 21:45
-
This answer/explanation is very confusing. It leads people to believe that the ~ is taking the two's complement rather than the ones' complement. It makes much more sense to start by saying the ~ operator simply flips the bits (such a simple point to start!) and then state the effect this has in a processor (or number system) that uses two's complement. – user1596274 Apr 21 '20 at 10:08
-
Is there any use of Unary operations in real world programming? – Jdeep Jul 17 '20 at 15:54
~
flips the bits in the value.
Why ~2
is -3
has to do with how numbers are represented bitwise. Numbers are represented as two's complement.
So, 2 is the binary value
00000010
And ~2 flips the bits so the value is now:
11111101
Which, is the binary representation of -3.

- 97,193
- 102
- 206
- 364

- 161,458
- 45
- 265
- 341
-
2
-
12
-
What is its use in real world programming ? Does it has applications in competitive programming? – Jdeep Jul 17 '20 at 15:55
-
@driis, `System.out.println((byte)(~2));` still outputs -3 instead of 253, may you explain why? – zipper Jan 16 '21 at 01:29
-
As others mentioned ~
just flipped bits (changes one to zero and zero to one) and since two's complement is used you get the result you saw.
One thing to add is why two's complement is used, this is so that the operations on negative numbers will be the same as on positive numbers. Think of -3
as the number to which 3
should be added in order to get zero and you'll see that this number is 1101
, remember that binary addition is just like elementary school (decimal) addition only you carry one when you get to two rather than 10.
1101 +
0011 // 3
=
10000
=
0000 // lose carry bit because integers have a constant number of bits.
Therefore 1101
is -3
, flip the bits you get 0010
which is two.

- 110,860
- 49
- 189
- 262
This operation is a complement, not a negation.
Consider that ~0 = -1, and work from there.
The algorithm for negation is, "complement, increment".
Did you know? There is also "one's complement" where the inverse numbers are symmetrical, and it has both a 0 and a -0.

- 10,352
- 6
- 33
- 43
I know the answer for this question is posted a long back, but I wanted to share my answer for the same.
For finding the one’s complement of a number, first find its binary equivalent. Here, decimal number 2
is represented as 0000 0010
in binary form. Now taking its one’s complement by inverting (flipping all 1’s into 0’s and all 0’s into 1’s) all the digits of its binary representation, which will result in:
0000 0010 → 1111 1101
This is the one’s complement of the decimal number 2. And since the first bit, i.e., the sign bit is 1 in the binary number, it means that the sign is negative for the number it stored. (here, the number referred to is not 2 but the one’s complement of 2).
Now, since the numbers are stored as 2’s complement (taking the one’s complement of a number plus one), so to display this binary number, 1111 1101
, into decimal, first we need to find its 2’s complement, which will be:
1111 1101 → 0000 0010 + 1 → 0000 0011
This is the 2’s complement. The decimal representation of the binary number, 0000 0011
, is 3
. And, since the sign bit was one as mentioned above, so the resulting answer is -3
.
Hint: If you read this procedure carefully, then you would have observed that the result for the one’s complement operator is actually, the number (operand - on which this operator is applied) plus one with a negative sign. You can try this with other numbers too.

- 1,803
- 2
- 24
- 36
-
Why is it adding twice? I'm seeing `add, flip, add`. `0010` -> `0011` -> `1100` -> `1101` – Braden Best Jan 20 '15 at 07:45
-
1It's flip, flip, add. First flip for 1's complement. And since, it is stored in 2's complement in the system, when you need to display the number, it will show the 2's complement of the number stored (i.e., second flip and add). – Himanshu Aggarwal Jan 21 '15 at 14:45
-
-
Yes it will be 2 only. But since when the bits are stored in memory the most significant bit was 1 which will make the number negative later on as explained in the answer above. – Himanshu Aggarwal Jan 22 '15 at 07:40
-
1From what you're describing and everything I've researched, this is not a two's complement, but a "regular" complement, or a bitwise NOT. In logic, `NOT 0 = 1` and `NOT 1 = 0`. In a four-bit system, `NOT 0011` (3) = `1100` (12 unsigned, -4 signed). From what I understand, two's complement is defined as `(NOT n) + 1`, and is used to find the negative counterpart of a number regardless of the number of bits. Thus, `2c(5) = -5`. See, now it makes perfect sense. Just as long as you call this operation what it is: a bitwise NOT. – Braden Best Feb 07 '15 at 17:51
-
Case in point: [1](http://pastebin.com/1nXgd4aQ), [2](http://pastebin.com/smWNtTHN) – Braden Best Feb 07 '15 at 18:29
-
This is the only answer (except [mine](https://stackoverflow.com/a/58699443/52499) :)) that explain things in a non-counterintuitive manner. Although the last comment of the author is confusing. @BradenBest You might want to check out my answer as well. Hopefully, that'll answer your questions. But to brief you, `~` is bitwise NOT (or complement). When `ruby` outputs a negative value, it prints `-`, then two's complement (`~x + 1`) of the value. Therefore, `-(~~x + 1)`. It's flip, flip, add. And minus sign. – x-yuri Nov 04 '19 at 22:06
int a=4; System.out.println(~a); Result would be :-5
'~' of any integer in java represents 1's complement of the no. for example i am taking ~4,which means in binary representation 0100. first , length of an integer is four bytes,i.e 4*8(8 bits for 1 byte)=32. So in system memory 4 is represented as 0000 0000 0000 0000 0000 0000 0000 0100 now ~ operator will perform 1's complement on the above binary no
i.e 1111 1111 1111 1111 1111 1111 1111 1011->1's complement the most significant bit represents sign of the no(either - or +) if it is 1 then sign is '-' if it is 0 then sign is '+' as per this our result is a negative number, in java the negative numbers are stored in 2's complement form, the acquired result we have to convert into 2's complement( first perform 1's complement and just add 1 to 1's complement). all the one will become zeros,except most significant bit 1(which is our sign representation of the number,that means for remaining 31 bits 1111 1111 1111 1111 1111 1111 1111 1011 (acquired result of ~ operator) 1000 0000 0000 0000 0000 0000 0000 0100 (1's complement)
1 (2's complement)
1000 0000 0000 0000 0000 0000 0000 0101 now the result is -5 check out this link for the video <[Bit wise operators in java] https://youtu.be/w4pJ4cGWe9Y

- 61
- 1
- 3
Simply ...........
As 2's complement of any number we can calculate by inverting all 1s to 0's and vice-versa than we add 1 to it..
Here N= ~N produce results -(N+1) always. Because system store data in form of 2's complement which means it stores ~N like this.
~N = -(~(~N)+1) =-(N+1).
For example::
N = 10 = 1010
Than ~N = 0101
so ~(~N) = 1010
so ~(~N) +1 = 1011
Now point is from where Minus comes. My opinion is suppose we have 32 bit register which means 2^31 -1 bit involved in operation and to rest one bit which change in earlier computation(complement) stored as sign bit which is 1 usually. And we get result as ~10 = -11.
~(-11) =10 ;
The above is true if printf("%d",~0); we get result: -1;
But printf("%u",~0) than result: 4294967295 on 32 bit machine.

- 31
- 3
The Bitwise complement operator(~) is a unary operator.
It works as per the following methods
First it converts the given decimal number to its corresponding binary value.That is in case of 2 it first convert 2 to 0000 0010 (to 8 bit binary number).
Then it converts all the 1 in the number to 0,and all the zeros to 1;then the number will become 1111 1101.
that is the 2's complement representation of -3.
In order to find the unsigned value using complement,i.e. simply to convert 1111 1101 to decimal (=4294967293) we can simply use the %u during printing.

- 1,092
- 2
- 18
- 27
It's easy:
Before starting please remember that
1 Positive numbers are represented directly into the memory.
2. Whereas, negative numbers are stored in the form of 2's compliment.
3. If MSB(Most Significant bit) is 1 then the number is negative otherwise number is
positive.
You are finding ~2:
Step:1 Represent 2 in a binary format
We will get, 0000 0010
Step:2 Now we have to find ~2(means 1's compliment of 2)
1's compliment
0000 0010 =================> 1111 1101
So, ~2 === 1111 1101, Here MSB(Most significant Bit) is 1(means negative value). So,
In memory it will be represented as 2's compliment(To find 2's compliment first we
have to find 1's compliment and then add 1 to it.)
Step3: Finding 2's compliment of ~2 i.e 1111 1101
1's compliment Adding 1 to it
1111 1101 =====================> 0000 0010 =================> 0000 0010
+ 1
---------
0000 0011
So, 2's compliment of 1111 1101, is 0000 0011
Step4: Converting back to decimal format.
binary format
0000 0011 ==============> 3
In step2: we have seen that the number is negative number so the final answer would
be -3
So, ~2 === -3

- 386
- 3
- 7
Simply speaking, ~
is to find the symmetric value (to -0.5).
~a
and a
should be symmetric to the mirror in the middle of 0 and -1.
-5,-4,-3,-2,-1 | 0, 1, 2, 3, 4
~0 == -1
~1 == -2
~2 == -3
~3 == -4
The reason for this is due to how computers represent negative values.
Say, if positive value uses 1
to count, negative value uses 0
.
1111 1111 == -1
1111 1110 == -2; // add one more '0' to '1111 1111'
1111 1101 == -3; // add one more '0' to '1111 1110'
Finally, ~i == -(i+1)
.

- 6,457
- 2
- 29
- 39

- 31
- 2
I think for most people the confusion part comes from the difference between decimal number and signed binary number, so lets clarify it first:
for human decimal world: 01 means 1, -01 means -1, for computer's binary world: 101 means 5 if it is unsigned. 101 means (-4 + 1) if is signed while the signed digit is at position x. | x
so 2's flipped bit = ~2 = ~(010) = 101 = -4 + 1 = -3 the confusion comes from mixing up the signed result(101=-3) and the unsinged result(101=5)

- 31
- 1
Basically action is a complement not a negation .
Here x= ~x produce results -(x+1) always .
x = ~2
-(2+1)
-3

- 171
- 2
- 4
tl;dr ~
flips the bits. As a result the sign changes. ~2
is a negative number (0b..101
). To output a negative number ruby
prints -
, then two's complement of ~2
: -(~~2 + 1) == -(2 + 1) == 3
. Positive numbers are output as is.
There's an internal value, and its string representation. For positive integers, they basically coincide:
irb(main):001:0> '%i' % 2
=> "2"
irb(main):002:0> 2
=> 2
The latter being equivalent to:
irb(main):003:0> 2.to_s
"2"
~
flips the bits of the internal value. 2
is 0b010
. ~2
is 0b..101
. Two dots (..
) represent an infinite number of 1
's. Since the most significant bit (MSB) of the result is 1
, the result is a negative number ((~2).negative? == true
). To output a negative number ruby
prints -
, then two's complement of the internal value. Two's complement is calculated by flipping the bits, then adding 1
. Two's complement of 0b..101
is 3
. As such:
irb(main):005:0> '%b' % 2
=> "10"
irb(main):006:0> '%b' % ~2
=> "..101"
irb(main):007:0> ~2
=> -3
To sum it up, it flips the bits, which changes the sign. To output a negative number it prints -
, then ~~2 + 1
(~~2 == 2
).
The reason why ruby
outputs negative numbers like so, is because it treats the stored value as a two's complement of the absolute value. In other words, what's stored is 0b..101
. It's a negative number, and as such it's a two's complement of some value x
. To find x
, it does two's complement of 0b..101
. Which is two's complement of two's complement of x
. Which is x
(e.g ~(~2 + 1) + 1 == 2
).
In case you apply ~
to a negative number, it just flips the bits (which nevertheless changes the sign):
irb(main):008:0> '%b' % -3
=> "..101"
irb(main):009:0> '%b' % ~-3
=> "10"
irb(main):010:0> ~-3
=> 2
What is more confusing is that ~0xffffff00 != 0xff
(or any other value with MSB equal to 1
). Let's simplify it a bit: ~0xf0 != 0x0f
. That's because it treats 0xf0
as a positive number. Which actually makes sense. So, ~0xf0 == 0x..f0f
. The result is a negative number. Two's complement of 0x..f0f
is 0xf1
. So:
irb(main):011:0> '%x' % ~0xf0
=> "..f0f"
irb(main):012:0> (~0xf0).to_s(16)
=> "-f1"
In case you're not going to apply bitwise operators to the result, you can consider ~
as a -x - 1
operator:
irb(main):018:0> -2 - 1
=> -3
irb(main):019:0> --3 - 1
=> 2
But that is arguably of not much use.
An example Let's say you're given a 8-bit (for simplicity) netmask, and you want to calculate the number of 0
's. You can calculate them by flipping the bits and calling bit_length
(0x0f.bit_length == 4
). But ~0xf0 == 0x..f0f
, so we've got to cut off the unneeded bits:
irb(main):014:0> '%x' % (~0xf0 & 0xff)
=> "f"
irb(main):015:0> (~0xf0 & 0xff).bit_length
=> 4
Or you can use the XOR operator (^
):
irb(main):016:0> i = 0xf0
irb(main):017:0> '%x' % i ^ ((1 << i.bit_length) - 1)
=> "f"

- 16,722
- 15
- 114
- 161
here, 2 in binary(8 bit) is 00000010 and its 1's complement is 11111101, subtract 1 from that 1's complement we get 11111101-1 = 11111100, here the sign is - as 8th character (from R to L) is 1 find 1's complement of that no. i.e. 00000011 = 3 and the sign is negative that's why we get -3 here.

- 39
- 2
First we have to split the given digit into its binary digits and then reverse it by adding at the last binary digit.After this execution we have to give opposite sign to the previous digit that which we are finding the complent ~2=-3 Explanation: 2s binary form is 00000010 changes to 11111101 this is ones complement ,then complented 00000010+1=00000011 which is the binary form of three and with -sign I.e,-3

- 11
- 1
The bit-wise operator is a unary operator which works on sign and magnitude method as per my experience and knowledge.
For example ~2 would result in -3.
This is because the bit-wise operator would first represent the number in sign and magnitude which is 0000 0010 (8 bit operator) where the MSB is the sign bit.
Then later it would take the negative number of 2 which is -2.
-2 is represented as 1000 0010 (8 bit operator) in sign and magnitude.
Later it adds a 1 to the LSB (1000 0010 + 1) which gives you 1000 0011.
Which is -3.
Javascript tilde (~) coerces a given value to the one's complement--all bits are inverted. That's all tilde does. It's not sign opinionated. It neither adds nor subtracts any quantity.
0 -> 1
1 -> 0
...in every bit position [0...integer nbr of bits - 1]
On standard desktop processors using high-level languages like JavaScript, BASE10 signed arithmetic is the most common, but keep in mind, it's not the only kind. Bits at the CPU level are subject to interpretation based on a number of factors. At the 'code' level, in this case JavaScript, they are interpreted as a 32-bit signed integer by definition (let's leave floats out of this). Think of it as quantum, those 32-bits represent many possible values all at once. It depends entirely on the converting lens you view them through.
JavaScript Tilde operation (1's complement)
BASE2 lens
~0001 -> 1110 - end result of ~ bitwise operation
BASE10 Signed lens (typical JS implementation)
~1 -> -2
BASE10 Unsigned lens
~1 -> 14
All of the above are true at the same time.

- 3,021
- 1
- 14
- 27
Here is a way it can be explained :
let's take the case of why ~2 = -3,(explained using the 8-bit system for simplicity)
1)we've 2 ---> 00000010
2)we can get ~2 ---> 11111101 #by simply switching the bits.
[but the common mistake is that some try to convert the binary value of ~2 obtained directly to decimal(base 10) numbers, in this case, it's 253. This is not how we find complements.]
3)now we find a binary number which on adding to the binary value of 2~ gives 0 (00000000) as the result. In this case, it is 00000011 (which is 3), since if we add 00000011 to 11111101 that we have, we get 100000000, but since we are using the 8-bit system and the 1 is in the 9th position, it is ignored completely, so we end up with 00000000.
4)From point (3) we can say ~2+3 = 0, and hence we can say, ~2 = -3
Note: The value of -3 is simply 11111101 and can be explained in the same way.

- 127
- 8
This operator comes into bitwise operators means those operators which works with bits these operators can manipulate the bits. There are different type of bitwise operators :
- Bitwise AND( & )
- Bitwise OR ( | )
- Bitwise XOR (^)
- Bitwise compliment (~)
- Bitwise leftshift (<<)
- Bitwise rightshift (>>)
The "~" operator in many programming languages is also called as the bitwise NOT operator. It performs a bitwise inversion on the binary representation of a number. In most programming languages, including Python, integers are represented using a fixed number of bits, typically 32 or 64. I am just using example of 8 bit (for fast and better understanding).
The binary representation of 2 is "00000010" (8 bits), and the bitwise NOT of 2 would invert each bit, resulting in "11111101". This binary representation, when interpreted as a signed 8-bit integer, is equal to -3. [ Here 8-bit are only used only for example you must use 32 or 64 bits].
Binary representation of 2 in 32 bits: 00000000000000000000000000000010 And after using ~ operator 11111111111111111111111111111101
The reason for this is due to the two's complement representation of signed integers. In this representation, the most significant bit (the leftmost bit) represents the sign of the number. If it is 0, the number is positive(+) and if it is 1, the number is negative(-).
When you perform a bitwise NOT on a positive number, it flips all the bits, including the sign bit, resulting in a negative number in two's complement representation.
It's important to note that the actual result may vary depending on the programming language and the number of bits used to represent integers. Which example I provided assumes a 8-bit signed integer representation commonly used in many programming languages for a basic understanding.

- 1
- 1
-
Could you please edit this for clarity? I am having trouble parsing it. – tchrist Jun 26 '23 at 02:32
-
Welcome to Stack Overflow! Your answer appears likely to have been written (entirely or partially) by AI (e.g., ChatGPT). A heads-up that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. We do hope you'll continue to be a part of our community and contribute with *your own*, quality posts in the future. Thanks! – NotTheDr01ds Jul 05 '23 at 13:22
-
**Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. – NotTheDr01ds Jul 05 '23 at 13:23
-
1This is not generated by ChatGPT, This is creation of my mind this is my mind solution, you know nothing you are just guessing and I think this is your work on this platform. Do you know who I am, completed BTech in CSE from IIT Delhi,India. – Mark Jul 08 '23 at 15:35