JavaScript的位运算符的问题 [英] javascript bitwise operator question
本文介绍了JavaScript的位运算符的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Javascript中,当我做到这一点。
VAR NUM = 1;〜NUM == -2
为什么〜NUM不等于0
在二进制1被存储为1 ...因此不是1应该是0
的或者存储像0001这样不是0001是1110 的
我想我失去了一些东西...有人可以清除此了。
块引用>解决方案让我们假设一个JavaScript号码是8位宽(其中它不是):
然后
1 = 0000 0001B
和
〜1 = 1111 1110B
这是
的二进制重新presentation-2
0000 0010B = 2
0000 0001B = 1
0000 0000B = 0
1111 1111B = -1
1111 1110B = -2In Javascript when I do this
var num = 1; ~ num == -2
why does ~num not equal 0
in binary 1 is stored as 1 ... thus not 1 should be 0
or it is stored like 0001 thus not 0001 would be 1110
I think I am missing something... can someone clear this up
解决方案Look up Two's complement for signed binary numbers
Lets assume that a javascript Number is 8 bits wide (which its not):
then
1 = 0000 0001b
and
~1 = 1111 1110b
Which is the binary representation of
-2
0000 0010b = 2 0000 0001b = 1 0000 0000b = 0 1111 1111b = -1 1111 1110b = -2
这篇关于JavaScript的位运算符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文