---
description: 🚧 under construction -> code example
---

# bitwise not (\~)

[JS](../../../..) ⟩ [statement](../../../statement) ⟩ [expression](../../../statement/expr) ⟩ [operator](../..) ⟩ [arithmetic](..) ⟩ [bitwise]() ⟩ 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: [2s-complement](2s-complement "mention")<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](../../table-of-operators "mention")
{% 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: [2s-complement](2s-complement "mention")
{% 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](../../../../val/prim/null "mention") <mark style="color:yellow;">**/**</mark> [undefined](../../../../val/prim/undefined "mention") <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](../../../../val/prim/num/special/nan "mention") <mark style="color:yellow;">**/**</mark> [infinity](../../../../val/prim/num/special/infinity "mention")
* <mark style="color:yellow;">**non-numeric**</mark> [str](../../../../val/prim/str "mention") <mark style="color:yellow;">**/**</mark> <mark style="color:yellow;">**non-numeric**</mark> [arr](../../../../val/builtin/arr "mention")&#x20;
* <mark style="color:yellow;">**other**</mark> [obj](../../../../val/obj "mention") <mark style="color:yellow;">**(**</mark><mark style="color:red;">**except**</mark> [date](../../../../val/builtin/date "mention")<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](../../../../val/prim/bigint "mention").
* :x: <mark style="color:red;">**can't**</mark>**&#x20;**<mark style="color:yellow;">**be used**</mark> with [symbol](../../../../val/prim/symbol "mention"):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 [num](../../../../val/prim/num "mention") / [bigint](../../../../val/prim/bigint "mention").
{% endhint %}
{% endtab %}

{% tab title="👥 相關" %}
* :star2: [2s-complement](2s-complement "mention")
* :point\_right: [double-tilde](not/double-tilde "mention") - 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](../../../../../master/ref/javascript-the-definitive-guide "mention") ⟩ 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 %}
