在 javascript 中,波浪号运算符是按位非或补码,那么为什么下面的运算符不能按预期工作?
var x = true;
var tildeX = ~x;
var exclX = !x;
tildeX == exclX; // returns false
最佳答案
原因是因为true
等价于1
,当你对数字进行按位非(
你得到 ~
) 1-2
。当与 NOT 运算符 (!
) 结合时,它会产生 false(因为当与 NOT 运算符结合时,唯一会产生 true 的数字是 0
)
Here is some information that you might find interesting
引用自链接:
Bitwise NOTing any number x yields -(x + 1)
关于javascript - 为什么 !(~true) 不是真的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19514227/