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;
Expression | Binary value | Decimal value |
---|---|---|
3 | 00000000 00000000 00000000 00000011 | 3 |
~3 | 11111111 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