Bitwise NOT (~)

在此页

The bitwise NOT operator ( ``` ~ ``` ) inverts the bits of its operand.

句法

````~a`
```

描述

The operands are converted to 32-bit integers and expressed by a series of bits (zeroes and ones). Numbers with more than 32 bits get their most significant bits discarded. For example, the following integer with more than 32 bits will be converted to a 32 bit integer:

```Before: 11100110111110100000000000000110000000000001
After:              10100000000000000110000000000001
```

Each bit in the first operand is paired with the corresponding bit in the second operand: first bit to first bit , second bit to second bit , and so on.

The operator is applied to each pair of bits, and the result is constructed bitwise.

The truth table for the ``` NOT ``` operation is:

a NOT a
0 1
1 0
``` 9 (base 10) = 00000000000000000000000000001001 (base 2)
--------------------------------
~9 (base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)
```

Bitwise NOTing any number ``` x ``` yields ``` -(x + 1) ``` 。例如， ``` ~-5 ``` yields ``` 4 ``` .

Note that due to using 32-bit representation for numbers both ``` ~-1 ``` and ``` ~4294967295 ``` (2 32 -1) results in ``` 0 ``` .

范例

Using bitwise NOT

```~0;  // -1
~-1; // 0
~1;  // -2
```

规范

ECMAScript (ECMA-262)
The definition of 'Unary NOT expression' in that specification.

浏览器兼容性

 Desktop Mobile Server Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Chrome 1 Edge 12 Firefox 1 IE 3 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100

另请参阅

1. JavaScript
2. 教程：
3. 完整初学者
4. JavaScript 指南
5. 中间体
6. 高级
7. 参考：
8. 内置对象
9. 表达式 & 运算符
10. 语句 & 声明
11. 函数
12. 错误
13. 杂项