shankar.siva shankar.siva - 1 year ago 169
Javascript Question

javascript bitwise 64 bit implementation

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

Answer Source

javascript bitwise 64 bit implementation using goog.math.Long

Here is the example:

<!DOCTYPE html>
<html lang="en" xmlns="">
    <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

Licenses were required to use this library.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download