I am currently fighting with a javascript problem where I have a 62bit bitmask that should be used as filter.
I used the snippet from here but I cant get it to work for some cases. How to do bitwise AND in javascript on variables that are longer than 32 bit?
function testBitmask(fd, filterMask){
var a = fd;
var b = filterMask;
var w = 4294967296; // 2^32
var aHI = a / w;
var aLO = a % w;
var bHI = b / w;
var bLO = b % w;
var aAll = (aHI & bHI) * w;
var bAll = (aLO & bLO);
var retVal = (aAll + bAll) == filterMask;
console.log("retVal:",retVal)
return retVal;
}
I dont understand why testBitmask(2147483648,2147483648) returns false, thats for 2^31. 2^32 => true. 2^33 => true.
bAll gets negative here so I assume an overflow of the 32bit int, ideas?
If in javascript all numbers are 64 bit floating point and there are no 64 bit integers, you can't expect to define a and b (or fd and filtermask) with that precision, without rounding errors.
Try to define an object which encapsulates the 64bit integer type.
As an example you can look at the js-ctype implementation made by Mozilla MDN:
https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/js-ctypes_reference/Int64
and in particular
https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/Working_with_data#64-bit_integers
Theirs Int64 and UInt64 objects don't provide any methods for performing arithmetic, but you can pull out the high and low 32-bit portions and do math on them, then join them back together.
A simple code example, using typed arrays instead:
You can use it like this:
Results: