โbitwise not (~)
๐ง under construction -> code example
JS โฉ statement โฉ expression โฉ operator โฉ arithmetic โฉ bitwise โฉ not
(converts the operand to a 32-bit signed integer)
inverts the bits of its operand. (definition๏ผ ~x = -x - 1
, ๐ 2's complement)
// โญ `~x` === -x - 1 (if x integer, truncate decimal if not)
~(3), // -4 (-3 - 1)
~(-5), // 4 ( 5 - 1)
~(3.4), // -4 ( 3.4 -> 3 -> -3 - 1 = -4)
~(-5.5), // 4 (-5.5 -> -5 -> 5 - 1 = 4)
// โญ `~~x`: nearest integer towards 0.
~~(7.8), // 7
~~(-3.4), // -3
~~(6), // 6 (โญ `~~x = x`, if x integer)
๐ table of operators
replit๏ผ bitwise not (~) (v.2)
โโโ (+x !== ~~x)
โ
โ x type +x ~~x ~x โญ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
null Null 0 0 -1
โญ undefined Undefined NaN 0 -1
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
true Boolean 1 1 -2
false Boolean 0 0 -1
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
'' String 0 0 -1
'23' String 23 23 -24
โญ 'yes' String NaN 0 -1
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
1 Number 1 1 -2
0 Number 0 0 -1
โญ NaN Number NaN 0 -1
โญ Infinity Number Infinity 0 -1
โญ -Infinity Number -Infinity 0 -1
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 40n BigInt TypeError 40 -41
โ Symbol() Symbol TypeError TypeError TypeError
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ ({a:1}) Object NaN 0 -1
โญ ({}) Object NaN 0 -1
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
[] Array 0 0 -1
โญ [4.5] Array 4.5 4 -5
โญ ['-7.8'] Array -7.8 -7 6
โญ [1,2,3] Array NaN 0 -1
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ /regex/ RegExp NaN 0 -1
โญ new Date Date 1667543819273 1096508425 -1096508426
โญ ()=>{} Function NaN 0 -1
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
for n-bit signed integers,
-x
=~x + 1
.~x
=-x
- 1
.~(x
ยฑ M)
โก~x
(mod M), where M=2n
๐ 2's complement
๐ 2's complement
๐ double tilde (~~) - nearest integer towards 0.
replit๏ผ bitwise not (~)
// โญ test ~x
const M = 2 ** 32; // 4294967296
const m = M/8; // 536870912
M, // 4294967296
m, // 536870912 (m = M/8)
// โญ overflow: 3m + 2m โก -3m (mod M)
~~(5*m), // -1610612736 (overflow)โ
-3*m, // -1610612736
// โญ underflow: -5m โก 3m (mod M)
~~(-5*m), // 1610612736 (underflow)โ
3*m, // 1610612736
-5*m, // -2684354560
// โญ ~(x ยฑ M) = ~x
~1234, // -1235
~(1234 - M), // -1235
~(1234 + M), // -1235
'----------',
// โญ `~x` === -x - 1 (truncate decimal if needed)
~(3), // -4 (-3 - 1)
~(-5), // 4 ( 5 - 1)
~(3.4), // -4 ( 3.4 -> 3 -> -3 - 1 = -4)
~(-5.5), // 4 (-5.5 -> -5 -> 5 - 1 = 4)
// โญ `~~x`:
~~(7.8), // 7 (โญ nearest integer towards 0)
~~(-3.4), // -3
~~(6), // 6 (โญ `~~x = x`, if x integer)
Last updated