Operator
suggest change~~ applies bitwise NOT twice. For a Number, that is the ToInt32 conversion: the operand is truncated toward zero and wrapped into the signed 32-bit range. It is not a view of the IEEE 754 bits.
A single ~ is different. ~n is -(ToInt32(n) + 1).
let number = 3.5;
let complement = ~number; // -4
let truncated = ~~number; // 3
For an integer already in that 32-bit range, ~~n is n, which is -(-(n + 1) + 1). For other numbers, truncation toward zero comes first.
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) truncates toward zero: the fractional part is dropped, and the sign stays.
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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents