心血来潮地跑到Upworks做了个JavaScript Test,结果当然是惨不忍睹,发现自己对不少JavaScript的基础知识的理解是模模糊糊,甚至是错的。

比如这题:

~-(2+"2")

这个表达式的值是21,我脑补了好久也得不到这个答案,这才发现,我完全不理解Bitwise NOT操作符。

不懂就补喽。

这回还真是“补”,回想起大学那会儿,学到“补码”时,脑子里就是一团雾,原来那团迷雾到今天也没散,真他么黏糊。

读到 Why is ~5 === -6 in JavaScript?:

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:

1010 => (-8 +0 +2 +0) => -6
1111 => (-8 +4 +2 +1) => -1

又琢磨了一会儿,才搞明白为啥补码这么反直觉。

“二补码”只能脑补,或者用代码打印脑补的内容。

在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;
}

参考链接:


IMDZ
24 声望2 粉丝

腰身壮 胆气豪 常练武 勤操劳