Javascript 波浪号和二进制补码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12337360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:58:10  来源:igfitidea点击:

Javascript Tilde & Two's complement

javascriptbit-manipulation

提问by Royi Namir

Two's complement method- generates -(x + 1).

二进制补码方法- 生成-(x + 1).

for example when JavaScript encounters the Tilde he uses this method:

例如,当 JavaScript 遇到波浪号时,他会使用以下方法:

~5 = -(5+1) = -6.

Fine - lets go deeper.

很好 - 让我们更深入。

Now lets talk about the Two's complement method.

现在让我们谈谈二进制补码方法。

5        = 0000 0101
Flip     = 1111 1010
add one  = 1111 1011

so 1111 1011is -5.

所以1111 1011-5

how ?

如何 ?

again : flip :

再次:翻转:

0000 0100 

add one :

添加一个:

0000 0101

And so it was -5.

原来如此-5

So how does this settle with ~5=-6?

那么这是如何解决的 ~5=-6呢?

where this -6came from ?

-6是从哪里来的?

回答by Aleksander Blomsk?ld

First of all, you need to realize that ~is the bitwise flip operator, which is not the same as the negate operator -. ~does only do the bitwise flipping, but the negate operator -does bitwise flipping and add one (for integers).

首先,您需要意识到它~是按位翻转运算符,它与否定运算符不同-~只执行按位翻转,但否定运算符-执行按位翻转并加一(对于整数)。

As you've explained, if yo want to go from a postive number nto -nusing the two complement method you bitwise flip/not n and add 1. ~n is just the bit-wise not meaning that ~n=-n-1.

正如你所解释的,如果你想从一个正数n-n使用两个补码方法你按位翻转/不是 n 并加 1。 ~n 只是按位而不意味着~n=-n-1.

For instance:

例如:

5               = 0000 0101
Flipped (~5)    = 1111 1010

So, which number does 1111 1010represent? Since the first digit is a 1 we know it's a negative value. To find which value, do

那么,哪个数字1111 1010代表呢?由于第一个数字是 1,我们知道它是一个负值。要找到哪个值,请执行

-(flip(1111 1010) + 1) =
-(0000 0101 + 1)
-(0000 0110) =
-6

回答by Andreas Grapentin

~5 = -(5 + 1) = -6

~5 = -(5 + 1) = -6

so far so good. However, ~is not the two's complement, it's the binary inversion operator.

到目前为止,一切都很好。然而,~不是二进制的补码,而是二元求逆运算符。

5     = 0000 0101
flip  : 1111 1010

which is -6

这是-6

does that make it clear?

这样说清楚了吗?

回答by xdazz

~is the Bitwise NOT operator (only inverts the bits of its operand).

~是按位非运算符(仅反转其操作数的位)。

For a positive number n,

对于正数n

~n + 1 = -n

回答by Drazen Bjelovuk

Two's complement method- generates -(x + 1).

二进制补码方法- 生成-(x + 1).

Simply put, two's complement does not generate -(x + 1). One's complement does (i.e., ~ / bitwise NOT / flipping the bits).

简单地说,二进制补码不会生成-(x + 1)。一个的补码(即,〜/按位非/翻转位)。

Two's compliment (flip the bits, add 1) is the (-0averse) operation/encoding we use to express a negative number in pure bits (and derive it therefrom). Two's complement of xwill generate -x.

二进制补(翻转比特,添加1)是(-0厌恶)操作/编码,我们使用表达在纯位的负数(和导出它从其)。的补码x将生成-x

~5is nothing more than flipping the bits 0000 0101to 1111 1010.

~5只不过是将位翻转0000 01011111 1010

To determine the value of 1111 1010, we flip back to 0000 0101and add 1: 0000 0110 (-6).

为了确定 的值1111 1010,我们返回0000 0101并添加 1: 0000 0110 (-6)

回答by Naveenk

Tild(~) -

波浪号(~) -

it is just flip(n). I.e. ~5 = flip(5). In java script numbers are always 64 bit signed. let us take Just 8 bit for reference,

它只是翻转(n)。即〜5 =翻转(5)。在 java 脚本中,数字总是 64 位有符号的。让我们只取 8 位作为参考,

 5==> 0000 0101 
~5 ==> filp(0000 0101)
~5 ==> 1111 1010 ==> -6 

2' complement -

2' 补 -

It's filp(n) + 1.

它是 filp(n) + 1。

5 ==> 0000 0101
2's complement of 5 ==> flip(0000 0101) + 0000 0001
2's complement of 5 ==> 1111 1010 + 000 0001
2's complement of 5 ==> 1111 1011