心血来潮地跑到Upworks作了个JavaScript Test,结果固然是惨不忍睹,发现本身对很多JavaScript的基础知识的理解是模模糊糊,甚至是错的。javascript
好比这题:html
~-(2+"2")
java
这个表达式的值是21,我脑补了很久也得不到这个答案,这才发现,我彻底不理解Bitwise NOT操做符。python
不懂就补喽。程序员
这回还真是“补”,回想起大学那会儿,学到“补码”时,脑子里就是一团雾,原来那团迷雾到今天也没散,真他么黏糊。this
读到 Why is ~5 === -6 in JavaScript?:code
It does indeed perform a bit-wise NOT, the negative number is in two's complement. So the value 1010 is -6.Two's complement basically works by the very left-most bit signifies a negative number and is taken as a negative value. All other 1 bits are added to this number. For example:orm
1010 => (-8 +0 +2 +0) => -6
1111 => (-8 +4 +2 +1) => -1
htm
又琢磨了一下子,才搞明白为啥补码这么反直觉。blog
“二补码”只能脑补,或者用代码打印脑补的内容。
在JavaScript里,若是用number.toString(2)
,结果是这样:
Decimal: 5 | Binary: 00000000000000000000000000000101 Decimal: 4 | Binary: 00000000000000000000000000000100 Decimal: 3 | Binary: 00000000000000000000000000000011 Decimal: 2 | Binary: 00000000000000000000000000000010 Decimal: 1 | Binary: 00000000000000000000000000000001 Decimal: 0 | Binary: 00000000000000000000000000000000 Decimal: -0 | Binary: 00000000000000000000000000000000 Decimal: -1 | Binary: 000000000000000000000000000000-1 Decimal: -2 | Binary: 00000000000000000000000000000-10 Decimal: -3 | Binary: 00000000000000000000000000000-11 Decimal: -4 | Binary: 0000000000000000000000000000-100 Decimal: -5 | Binary: 0000000000000000000000000000-101
这个结果符合直觉,但加法器的实现只会使用二补码,下面是加法器实际使用的“二补码”:
Decimal: 5 | Binary: 00000000000000000000000000000101 Decimal: 4 | Binary: 00000000000000000000000000000100 Decimal: 3 | Binary: 00000000000000000000000000000011 Decimal: 2 | Binary: 00000000000000000000000000000010 Decimal: 1 | Binary: 00000000000000000000000000000001 Decimal: 0 | Binary: 00000000000000000000000000000000 Decimal: -0 | Binary: 00000000000000000000000000000000 Decimal: -1 | Binary: 11111111111111111111111111111111 Decimal: -2 | Binary: 11111111111111111111111111111110 Decimal: -3 | Binary: 11111111111111111111111111111101 Decimal: -4 | Binary: 11111111111111111111111111111100 Decimal: -5 | Binary: 11111111111111111111111111111011
理解了二补码,再来看-22怎么被补成21的:
Decimal: -22 | Binary: 11111111111111111111111111101010 -> Decimal: 21 | Binary: 00000000000000000000000000010101
Python对此解释得更直接:
~ x
Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1.
This is the same as -x - 1.
我连补码都没闹明白,居然过了关还Score top 30%,可见其余人都没做弊,我真是无耻的程序员。
写了半天,一言以蔽之:
~x 至关于调用
function twosComplement(x){ return 0 -x - 1; }
参考连接: