The bitwise NOT operator (
~) inverts the bits of its operand.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
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:
9 (base 10) = 00000000000000000000000000001001 (base 2) -------------------------------- ~9 (base 10) = 11111111111111111111111111110110 (base 2) = -10 (base 10)
Bitwise NOTing any number
-(x + 1). For example,
Note that due to using 32-bit representation for numbers both
~4294967295 (232-1) results in
Using bitwise NOT
~0; // -1 ~-1; // 0 ~1; // -2
The definition of 'Unary NOT expression' in that specification.
|Bitwise NOT (||Chrome Full support 1||Edge Full support 12||Firefox Full support 1||IE Full support 3||Opera Full support 3||Safari Full support 1||WebView Android Full support 1||Chrome Android Full support 18||Firefox Android Full support 4||Opera Android Full support 10.1||Safari iOS Full support 1||Samsung Internet Android Full support 1.0||nodejs Full support 0.1.100|
- Full support
- Full support