Why is it that ~2 is equal to -3? How does ~
operator work?
相关问题
- Should “operator !=” always be implemented via “op
- How to define operations of an STFP Operator on Ai
- Regular Expressions: how to interpret *?
- Logical vs. bitwise operator AND
- What is the pre-Ruby2.3 equivalent to the safe nav
相关文章
- What does it means in C# : using -= operator by ev
- C# generics class operators not working
- Construction an logical expression which will coun
- Difference between “new” operator and “new” functi
- ~~ Operator In Postgres
- Rounded division by power of 2
- How do I perform explicit operation casting from r
- What is the ..= (dot dot equals) operator in Rust?
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)
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:
Simply flip all the bits and we get:
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.
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
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.
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.
All of the above are true at the same time.
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 which3
should be added in order to get zero and you'll see that this number is1101
, remember that binary addition is just like elementary school (decimal) addition only you carry one when you get to two rather than 10.Therefore
1101
is-3
, flip the bits you get0010
which is two.~
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
And ~2 flips the bits so the value is now:
Which, is the binary representation of -3.