blob: 5c7c9ce4e65549431123418abd87ca9c45e855b5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/*
js> (((-1 >>> 1) + 1) * Math.pow(2, 52 - 30) + 1) & 1
0
js> (((-1 >> 1) + 1) * Math.pow(2, 52 - 30) + 1) & 1
1
*/
function f(x) {
if (x >= 0) {
// if it does not fail, try with lower power of 2.
return (((x >>> 1) + 1) * 4194304 /* 2 ** (52 - 30) */ + 1) & 1;
}
return 2;
}
assertEq(f(-1 >>> 1), 1);
assertEq(f(-1 >>> 0), 0);
assertEq(f(-1 >>> 0), 0);
|