0

In javascript we can't able to evaluate larger than 32 bits in the bitwise operation. But In c# we can able to do this.

Here is the example If we try to run the below one browser console

4294967296 & 8589934591

Its returns 0 But in C# its returns the 4294967296

I googled and I have found that we can able to use this one goog.math.Long to implement larger than 32 bitwise implementation.

But my problem is, I am not able to find the sample to implement the logic in client side.

Can any one help me on this.

Thanks in advance

shankar.siva
  • 189
  • 2
  • 14

4 Answers4

1

I got clue from this URL but not used the goog.math.Long.

How to do bitwise AND in javascript on variables that are longer than 32 bit?

Here's a fun function for arbitrarily large integers:

function BitwiseAndLarge(val1, val2) {
    var shift = 0, result = 0;
    var mask = ~((~0) << 30); // Gives us a bit mask like 01111..1 (30 ones)
    var divisor = 1 << 30; // To work with the bit mask, we need to clear bits at a time
    while( (val1 != 0) && (val2 != 0) ) {
        var rs = (mask & val1) & (mask & val2);
        val1 = Math.floor(val1 / divisor); // val1 >>> 30
        val2 = Math.floor(val2 / divisor); // val2 >>> 30
        for(var i = shift++; i--;) {
            rs *= divisor; // rs << 30
        }
        result += rs;
    }
    return result;
}

Assuming that the system handles at least 30-bit bitwise operations properly.

Community
  • 1
  • 1
shankar.siva
  • 189
  • 2
  • 14
1

javascript bitwise 64 bit implementation using goog.math.Long

Here is the example:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Bitmask 64 Bit</title>
    <script type="text/javascript" src="goog.math.long.js"></script>
    <script type="text/javascript">
        var val1 = goog.math.Long.fromString("4611686018427387904"); // 2^62
        var val2 = goog.math.Long.fromString("6917529027641081856"); // 2^61 | 2^62
        var val3 = goog.math.Long.fromString("2305843009213693952"); // 2^61
        var val4 = goog.math.Long.fromString("4611686018427387904"); // 2^62
        document.writeln(val1.and(val2)); // 2^62 & (2^61 | 2^62)  
        document.writeln(val3.or(val4));  // 2^61 | 2^62
    </script>
</head>
<body>
</body>
</html>

Licenses were required to use this library.

shankar.siva
  • 189
  • 2
  • 14
0

I think you don't really need goog.math.long ? Suffice it to convert into string, to split. Then proceed AND operation and recompose :

var a = 8589934592;
var str = a.toString()
var subNumber1 = parseInt(str.slice(0, str.length - 5));
var subNumber2 = parseInt(str.slice(str.length - 5));
var res1 = (subNumber1 & subNumber1).toString();
var res2 = (subNumber2 & subNumber2).toString();
var res = parseInt(res1.concat(res2));
console.log(res);
kevin ternet
  • 4,514
  • 2
  • 19
  • 27
  • Why is that correct? (actually, *is* it correct?) AFAIK there's no simple way to relate bitwise operations to what happens to the decimal digits. – harold Jun 18 '16 at 14:39
  • I mean yea it works now, but that's because `x & x == x` is always true anyway, regardless of how meaningful the `x` is. If you had an `a` and a `b` though, there's no way this is going to work. – harold Jun 18 '16 at 14:42
  • You have Little misunderstood the question. Now I have updated different example. Can you check it. – shankar.siva Jun 18 '16 at 15:02
0

I'm not sure if this what you're looking for but if you are using google/closure-library with npm. You can use Browserify or Webpack to bundle the module into client side.

Narongdej Sarnsuwan
  • 5,095
  • 1
  • 12
  • 10