javascript / latest / operators / bitwise_not.html /

Bitwise NOT (~)

The bitwise NOT operator (~) inverts the bits of its operand. Like other bitwise operators, it converts the operand to a 32-bit signed integer

Try it

Syntax

~a

Description

The operand is converted to a 32-bit signed integer and expressed as 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 signed integer:

Before: 11100110111110100000000000000110000000000001
After:              10100000000000000110000000000001

Each bit in the operand is inverted in the result.

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)

The 32-bit signed integer operand is inverted according to two's complement. That is, the presence of the most significant bit is used to express negative integers.

Bitwise NOTing any number x yields -(x + 1). For example, ~-5 yields 4.

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

Examples

Using bitwise NOT

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

Specifications

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet Deno Node.js
Bitwise_NOT
1
12
1
3
3
1
1
18
4
10.1
1
1.0
1.0
0.10.0

See also

© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT