64bit bitmask and javascript

1.6k views Asked by At

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?

1

There are 1 answers

0
Bob__ On

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:

bitMask = function(high = 0x0,low = 0x0) {
    this.bm = new Uint32Array(2);

    if (arguments.length === 0 ) {
        this.bm[0] = 0x0;
        this.bm[1] = 0x0;
    }  else if (arguments.length === 2 && typeof arguments[0] === "number" && typeof arguments[1] === "number") {
        this.bm[0] = arguments[1];
        this.bm[1] = arguments[0];
    }

    this.bwAND = function(filter) {
        result = new bitMask();
        result.bm[0] = this.bm[0] & filter.bm[0];
        result.bm[1] = this.bm[1] & filter.bm[1];
        return result;
    }

    this.bwOR = function(filter) {
        result = new bitMask();
        result.bm[0] = this.bm[0] | filter.bm[0];
        result.bm[1] = this.bm[1] | filter.bm[1];
        return result;
    }

    this.bwXOR = function(filter) {
        result = new bitMask();
        result.bm[0] = this.bm[0] ^ filter.bm[0];
        result.bm[1] = this.bm[1] ^ filter.bm[1];
        return result;
    }

    this.bwNOT = function() {
        result = new bitMask();
        result.bm[0] = ~this.bm[0];
        result.bm[1] = ~this.bm[1];
        return result;
    }

    this.bwEQUALS = function(b){
        return (this.bm[0] == b.bm[0]) && (this.bm[1] == b.bm[1]);
    }

    this.toString = function() {
        var zeroes = "00000000000000000000000000000000";
        var strH = this.bm[1].toString(2);
        var zerH = zeroes.substr(0,32-strH.length);
        var strL = this.bm[0].toString(2);
        var zerL = zeroes.substr(0,32-strL.length);
        return zerH + strH + zerL + strL;
    }

}

You can use it like this:

var a = new bitMask(0x0FEDCBA9,0xFF00FF00);
var b = new bitMask(0x12345678,0x0000FFFF);
var c = b.bwAND(a);
var d = b.bwOR(a);
var e = b.bwXOR(a);
var f = b.bwNOT();
var g = b.bwEQUALS(a);

Results:

    a = 0000111111101101110010111010100111111111000000001111111100000000
    b = 0001001000110100010101100111100000000000000000001111111111111111
a & b = 0000001000100100010000100010100000000000000000001111111100000000
a | b = 0001111111111101110111111111100111111111000000001111111111111111
a ^ b = 0001110111011001100111011101000111111111000000000000000011111111
   ~b = 1110110111001011101010011000011111111111111111110000000000000000
(a == b)? = false