Operator

suggest change

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Tilde:
* Tilde
* Operator

Table Of Contents
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde