javascript
JavaScript Bitwise NOT Operator
心血來潮地跑到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: 00000000000000000000000000010101Python對此解釋得更直接:
~ xReturns 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; }參考鏈接:
- Why is ~5 === -6 in JavaScript?
- Why does bitwise “not 1” equal -2?
- MDN Bitwise operators
- Python's bitwise operators.
- Two's Complement
總結
以上是生活随笔為你收集整理的JavaScript Bitwise NOT Operator的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 17.Node.js 回调函数--异步编
- 下一篇: C语言函数中的参数有const的问题