Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsigned 64-bit bitwise AND/OR in Javascript

I've looked through Stack Overflow and haven't quite found an answer for this. I have bitmask values that go from 0x0000000000000001 to 0x0200000000000000, each representing a field in my object that can be modified. Modified fields have their corresponding mask value bitwise-OR'd together to have a single 64-bit value that can be interpreted later. But I'm having difficulties because the Javascript bitwise operators convert the operands to 32-bit signed values. I tried to write unsigned 64-bit unsigned methods to mimic bitwise-OR and bitwise-AND, but ran into difficulties. My first stab was:

    _bitwiseOr64: function(a, b) {
         var aHi = (a >> 32);
         var aLo = (a & 0xffffffff);
         var bHi = (b >> 32);
         var bLo = (b & 0xffffffff);
         return (((aHi | bHi) << 32) + (aLo | bLo));
    }

If a = 0 and b = 0x80000000, I'd want the result of _bitwiseOr64 to be 0x80000000 (unsigned because I'm dealing with bitmasks here). I'm not getting that result. Can anyone offer help?

like image 791
Steve Avatar asked Dec 30 '25 22:12

Steve


1 Answers

Take a look at this library: http://google.github.io/closure-library/api/class_goog_math_Long.html

They have multiple bitwise functionality like add(other) and or(other) that support 64-bit as well.

For Node.JS, you can use Long.js

like image 168
Peyman Avatar answered Jan 01 '26 11:01

Peyman