JavaScript ~ Integer


Example

The following example illustrates use of the bitwise NOT (~) operator on integer numbers.

let number = 3;
let complement = ~number;

Result of the complement number equals to -4;

ExpressionBinary valueDecimal value
300000000 00000000 00000000 000000113
~311111111 11111111 11111111 11111100-4

To simplify this, we can think of it as function f(n) = -(n+1).

let a = ~-2; // a is now 1
let b = ~-1; // b is now 0
let c = ~0;  // c is now -1
let d = ~1;  // d is now -2
let e = ~2;  // e is now -3