Bitwise OR on Large Values

622 Views Asked by At

I need to do bitwise operations on large numbers.

For example:

2 | 2147483648

I would expect 2147483650, but instead, get -2147483646

Why is this, and what can I do about it?

Note, the code I am working on is some old javascript code that is run server side in classic asp, which I believe is an older version of js

1

There are 1 best solutions below

0
On BEST ANSWER

I found a workaround.

You can use the BigInteger.js library with slight modifications for use in server-side JS with Classic ASP.

https://raw.githubusercontent.com/peterolson/BigInteger.js/master/BigInteger.js

For use in classic asp, do the following:

Modify line 21 and 28 of the BigInteger.js library so it will work server side with classic ASP:

Change Line 21:

BigInteger.prototype = Object.create(Integer.prototype);

To:

BigInteger.prototype = new Object(Integer.prototype);

And make the same change to line 28.

Then, remove the last 4 lines. They are not needed:

// Node.js check
if (typeof module !== "undefined" && module.hasOwnProperty("exports")) {
    module.exports = bigInt;
}

Then in your script include it like this:

<script language="javascript" runat="server">

... the bigInteger.js code... 

function bitOrJS(a, b) {
    var big = new bigInt(a);

    return big.or(b);

}
</script>

You can now use the function in the classic ASP vbscript code:

Dim result

result = bitOrJS(2147483648, 2)

response.write result

Output will be 2147483650 as expected.