> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/not.md).

# bitwise not (\~)

[JS](/web/js.md) ⟩ [statement](/web/js/grammar/statement.md) ⟩ [expression](/web/js/grammar/statement/expr.md) ⟩ [operator](/web/js/grammar/op.md) ⟩ [arithmetic](/web/js/grammar/op/arithmetic.md) ⟩ [bitwise](/web/js/grammar/op/arithmetic/bitwise.md) ⟩ not

{% hint style="success" %} <mark style="color:yellow;">**(**</mark>converts the operand to a <mark style="color:blue;">**32**</mark><mark style="color:red;">**-bit**</mark>**&#x20;**<mark style="color:yellow;">**signed integer)**</mark>&#x20;

<mark style="color:yellow;">**inverts**</mark> the <mark style="color:yellow;">**bits**</mark> of its operand. <mark style="color:yellow;">**(**</mark><mark style="color:orange;">**definition**</mark>： <mark style="color:yellow;">**`~x = -x - 1`**</mark> , :point\_right: [2's complement](/web/js/grammar/op/arithmetic/bitwise/2s-complement.md)<mark style="color:yellow;">**)**</mark>

```javascript
// ⭐ `~x` === -x - 1 (if x integer, truncate decimal if not)
~(3),       // -4    (-3 - 1)
~(-5),      //  4    ( 5 - 1)

~(3.4),     // -4    ( 3.4 ->  3 -> -3 - 1 = -4)
~(-5.5),    //  4    (-5.5 -> -5 ->  5 - 1 =  4)

// ⭐ `~~x`: nearest integer towards 0.
~~(7.8),    //  7
~~(-3.4),   // -3
~~(6),      //  6 (⭐ `~~x = x`, if x integer)
```

:star2: [table of operators](/web/js/grammar/op/table-of-operators.md)
{% endhint %}

{% tabs %}
{% tab title="🗺️ 圖表" %}

* replit： [bitwise not (\~) (v.2)](https://replit.com/@pegasusroe/bitwise-not-v2#index.js)

```javascript
┌── (+x !== ~~x)
│
│  x           type        +x              ~~x          ~x      ⭐   
────────────────────────────────────────────────────────────────────
   null        Null                    0            0            -1
⭐ undefined   Undefined             NaN            0            -1
────────────────────────────────────────────────────────────────────
   true        Boolean                 1            1            -2
   false       Boolean                 0            0            -1
────────────────────────────────────────────────────────────────────
   ''          String                  0            0            -1
   '23'        String                 23           23           -24
⭐ 'yes'       String                NaN            0            -1
────────────────────────────────────────────────────────────────────
   1           Number                  1            1            -2
   0           Number                  0            0            -1
⭐ NaN         Number                NaN            0            -1
⭐ Infinity    Number           Infinity            0            -1
⭐ -Infinity   Number          -Infinity            0            -1
────────────────────────────────────────────────────────────────────
⛔ 40n         BigInt          TypeError           40           -41
⛔ Symbol()    Symbol          TypeError    TypeError     TypeError
────────────────────────────────────────────────────────────────────
⭐ ({a:1})     Object                NaN            0            -1
⭐ ({})        Object                NaN            0            -1
────────────────────────────────────────────────────────────────────
   []          Array                   0            0            -1
⭐ [4.5]       Array                 4.5            4            -5
⭐ ['-7.8']    Array                -7.8           -7             6
⭐ [1,2,3]     Array                 NaN            0            -1
────────────────────────────────────────────────────────────────────
⭐ /regex/     RegExp                NaN            0            -1
⭐ new Date    Date        1667543819273   1096508425   -1096508426
⭐ ()=>{}      Function              NaN            0            -1
────────────────────────────────────────────────────────────────────
```

{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="info" %}
for <mark style="color:blue;">**n**</mark><mark style="color:yellow;">**-bit**</mark> <mark style="color:red;">**signed**</mark>**&#x20;**<mark style="color:yellow;">**integers**</mark>,&#x20;

* <mark style="color:purple;">**`-x`**</mark> = <mark style="color:yellow;">**`~x + 1`**</mark>.
* <mark style="color:yellow;">**`~x`**</mark> = <mark style="color:purple;">**`-x`**</mark><mark style="color:yellow;">**`- 1`**</mark>.
* <mark style="color:yellow;">**`~(x`**</mark>± <mark style="color:red;">**M**</mark><mark style="color:yellow;">**`)`**</mark> ≡ <mark style="color:yellow;">**`~x`**</mark> (mod <mark style="color:red;">**M**</mark>), where $$M=2^n$$

&#x20;:point\_right: [2's complement](/web/js/grammar/op/arithmetic/bitwise/2s-complement.md)
{% endhint %}

{% hint style="warning" %} <mark style="color:yellow;">**the following**</mark>**&#x20;**<mark style="color:red;">**all**</mark>**&#x20;**<mark style="color:yellow;">**converted to**</mark>**&#x20;**<mark style="color:red;">**0**</mark> before applying <mark style="color:purple;">**bitwise not (\~)**</mark> operator.

* [null](/web/js/val/prim/null.md) <mark style="color:yellow;">**/**</mark> [undefined](/web/js/val/prim/undefined.md) <mark style="color:yellow;">**/**</mark> <mark style="color:red;">**false**</mark> <mark style="color:yellow;">**/**</mark> <mark style="color:orange;">**`""`**</mark> (<mark style="color:yellow;">**empty string**</mark>) <mark style="color:yellow;">**/**</mark> [NaN](/web/js/val/prim/num/special/nan.md) <mark style="color:yellow;">**/**</mark> [Infinity](/web/js/val/prim/num/special/infinity.md)
* <mark style="color:yellow;">**non-numeric**</mark> [String](/web/js/val/prim/str.md) <mark style="color:yellow;">**/**</mark> <mark style="color:yellow;">**non-numeric**</mark> [Array](/web/js/val/builtin/arr.md)&#x20;
* <mark style="color:yellow;">**other**</mark> [object](/web/js/val/obj.md) <mark style="color:yellow;">**(**</mark><mark style="color:red;">**except**</mark> [Date](/web/js/val/builtin/date.md)<mark style="color:yellow;">**)**</mark>
  {% endhint %}

{% hint style="warning" %} <mark style="color:purple;">**bitwise not (\~)**</mark>

* :white\_check\_mark: <mark style="color:green;">**can**</mark>**&#x20;**<mark style="color:yellow;">**be used**</mark> with [BigInt](/web/js/val/prim/bigint.md).
* :x: <mark style="color:red;">**can't**</mark>**&#x20;**<mark style="color:yellow;">**be used**</mark> with [Symbol](/web/js/val/prim/symbol.md):exclamation:
  {% endhint %}

{% hint style="info" %}
all <mark style="color:purple;">**bitwise operators**</mark> (<mark style="color:red;">**except**</mark>**&#x20;**<mark style="color:purple;">**>>>**</mark>) <mark style="color:green;">**can**</mark>**&#x20;**<mark style="color:yellow;">**be used**</mark> with [Number](/web/js/val/prim/num.md) / [BigInt](/web/js/val/prim/bigint.md).
{% endhint %}
{% endtab %}

{% tab title="👥 相關" %}

* :star2: [2's complement](/web/js/grammar/op/arithmetic/bitwise/2s-complement.md)
* :point\_right: [double tilde (\~\~)](/web/js/grammar/op/arithmetic/bitwise/not/double-tilde.md) - nearest integer towards 0.
  {% endtab %}

{% tab title="💈範例" %}

* replit： [bitwise not (\~)](https://replit.com/@pegasusroe/bitwise-not-v2#index.js)

```javascript
// ⭐ test ~x
const M = 2 ** 32;        // 4294967296
const m = M/8;            //  536870912

M,           // 4294967296
m,           //  536870912    (m = M/8)

// ⭐ overflow: 3m + 2m ≡ -3m (mod M)
~~(5*m),     // -1610612736    (overflow)❗
-3*m,        // -1610612736

// ⭐ underflow: -5m ≡ 3m (mod M)
~~(-5*m),    //  1610612736    (underflow)❗
3*m,         //  1610612736
-5*m,        // -2684354560

// ⭐ ~(x ± M) = ~x
~1234,          // -1235
~(1234 - M),    // -1235
~(1234 + M),    // -1235

'----------',

// ⭐ `~x` === -x - 1 (truncate decimal if needed)
~(3),       // -4     (-3 - 1)
~(-5),      //  4     ( 5 - 1)

~(3.4),     // -4     ( 3.4 ->  3 -> -3 - 1 = -4)
~(-5.5),    //  4     (-5.5 -> -5 ->  5 - 1 =  4)

// ⭐ `~~x`: 
~~(7.8),    //  7     (⭐ nearest integer towards 0)
~~(-3.4),   // -3
~~(6),      //  6     (⭐ `~~x = x`, if x integer)
```

{% endtab %}

{% tab title="📗 參考" %}

* [ ] [JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) ⟩ 4.8.3 Bitwise Operators
* [ ] Cornell ⟩ [Two's Complement](https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html)
  {% endtab %}

{% tab title="📘 手冊" %}

* [Bitwise NOT (\~)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/not.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
