Double Tilde ~~ will perform bitwise NOT operation twice.
The following example illustrates use of the bitwise NOT (~~) operator on decimal numbers.
To keep the example simple, decimal number 3.5 will be used, cause of it's simple representation in binary format.
let number = 3.5;
let complement = ~number;
Result of the complement number equals to -4;
| Expression | Binary value | Decimal value |
|---|---|---|
| 3 | 00000000 00000000 00000000 00000011 | 3 |
| ~~3 | 00000000 00000000 00000000 00000011 | 3 |
| 3.5 | 00000000 00000011.1 | 3.5 |
| ~~3.5 | 00000000 00000011 | 3 |
To simplify this, we can think of it as functions f2(n) = -(-(n+1) + 1) and g2(n) = -(-(integer(n)+1) + 1).
f2(n) will leave the integer number as it is.
let a = ~~-2; // a is now -2
let b = ~~-1; // b is now -1
let c = ~~0; // c is now 0
let d = ~~1; // d is now 1
let e = ~~2; // e is now 2
g2(n) will essentially round positive numbers down and negative numbers up.
let a = ~~-2.5; // a is now -2
let b = ~~-1.5; // b is now -1
let c = ~~0.5; // c is now 0
let d = ~~1.5; // d is now 1
let e = ~~2.5; // e is now 2