JavaScript的波浪号和放大器;补 [英] Javascript Tilde & Two's complement
问题描述
双补法 - 生成 - (X + 1 )
。
例如JavaScript时遇到波浪线,他用这个方法:
for example when JavaScript encounters the Tilde he uses this method:
~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
所以 1111 1011
是 -5
。
怎么样?
再次:翻转:
0000 0100
添加一个:
0000 0101
因此,这是 -5
。
那么,这怎样解决与〜5 = -6
?
这哪里 -6
是从哪里来的?
推荐答案
首先,你需要认识到,〜
是按位翻转运营商,这是不同为否定经营者 -
。 〜
不只做按位翻转,但否定经营者 -
不按位翻转和添加一个(整数)。
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).
正如你所解释的,如果哟希望从阳性数去 N
到 -n
使用两者相辅相成,方法,你按位翻转/不是n,并添加1〜N仅仅是逐位并不意味着〜N = -n-1
。
As you've explained, if yo want to go from a postive number n
to -n
using the two complement method you bitwise flip/not n and add 1. ~n is just the bit-wise not meaning that ~n=-n-1
.
例如:
5 = 0000 0101
Flipped (~5) = 1111 1010
那么,哪个号码呢 1111 1010
重新present?由于第一个数字是1,我们知道这是一个负值。为了找到其中的价值,做
So, which number does 1111 1010
represent? Since the first digit is a 1 we know it's a negative value. To find which value, do
-(flip(1111 1010) + 1) =
-(0000 0101 + 1)
-(0000 0110) =
-6
这篇关于JavaScript的波浪号和放大器;补的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!