How does the bitwise complement operator (~ tilde)

2018-12-31 19:56发布

Why is it that ~2 is equal to -3? How does ~ operator work?

12条回答
闭嘴吧你
2楼-- · 2018-12-31 20:15

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.

查看更多
公子世无双
3楼-- · 2018-12-31 20:15

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

查看更多
人气声优
4楼-- · 2018-12-31 20:17

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.

查看更多
牵手、夕阳
5楼-- · 2018-12-31 20:28

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.

查看更多
千与千寻千般痛.
6楼-- · 2018-12-31 20: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.

查看更多
大哥的爱人
7楼-- · 2018-12-31 20:34

~ 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.

查看更多
登录 后发表回答