Integer
suggest changeThe bitwise NOT (~) operator flips the bits of a 32-bit integer.
let number = 3;
let complement = ~number;
complement is -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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents