Search code examples
javascriptjavabitwise-operatorsbitwise-and

What is the different of bitwise operation between javascript and java


I'm doing some code conversion from javascript to java. When starting process some bitwise operation, I found the behavior of the bitwise operation between javascript and java are so different. I'm relatively experienced in the JVM side, the operation seems normal for me. But in the javascript side, I'm kind of confusing. I have checked MDN on mozilla the demo listed seem normal for me, but I found some case, not as my expected.

Question:

  1. Would you help to explain why the bitwise operation & in the code that I attached are so different in javascript and java?

  2. Also, I know that my knowledge in javascript is not enough for me to perform some kind of code conversion between these two languages, is there any useful site that helps me have a better understanding on the bitwise operation or the data structure of numeric data in javascript?

Java code

        Long x = 4023233417L;
        System.out.println(String.format("%d => %s",x, Long.toBinaryString(x)));
        Long y = 2562383102L;
        System.out.println(String.format("%d => %s",y, Long.toBinaryString(y)));
        Long result = x & y;
        System.out.println(String.format("%d => %s",result, Long.toBinaryString(result)));

        //Output
        //4023233417 => 11101111110011011010101110001001
        //2562383102 => 10011000101110101101110011111110
        //2290649224 => 10001000100010001000100010001000

Javascript code

x= 4023233417
console.log(x.toString(2))
y = 2562383102
console.log(y.toString(2))
result = x & y
console.log(result.toString(2))
//Output
//11101111110011011010101110001001
//10011000101110101101110011111110
//-1110111011101110111011101111000

I expecting x & y should "10001000100010001000100010001000".

But in the javascript code, the result is: "-1110111011101110111011101111000"


Solution

  • In javascript, you can perform bitwise operations on 32-bit numbers only. Even javascript use IEEE 754 floating-point standard to represent the number. Bitwise operation can be performed on 32-bit number. Hence rest of the bits will get ignored.

    To convert any number to its 32-bit representation, number | 0 with it. When javascript reads x & y it considers 32-bits of x and y and ignores other bits silently. In your case, the numbers on which you are performing bitwise are x = -271733879 and y = -1732584194.

    The safe range for bitwise operations in Javascript is Math.pow(-2,31) (-2147483648, about -2.1 billion) to Math.pow(2,32)-1 (2147483647, about +2.1 billion).

    x= 4023233417;
    console.log(x.toString(2))
    y = 2562383102 ;
    console.log(y.toString(2));
    
    result = x & y;
    
    console.log(result.toString(2)); //-1110111011101110111011101111000
    
    x = x | 0; //-271733879
    y = y | 0; //-1732584194
    console.log("32 bit x: " + x);
    console.log("32 bit y: " + y);
    result = x & y;
    
    console.log(result.toString(2)); //-1110111011101110111011101111000