2

I am trying to replicate the behavior of javascript bit-shift and bit-wise operations in java.

Did you ever try to do this before, and how can you do it reliably and consistently even with longs?

  var i=[some array with large integers];
  for(var x=0;x<100;x++)
  {
    var a=a large integer;

    var z=some 'long'>2.1 billion;
    //EDIT:
    z=i[x]+=(z>>>5)^(a<<2))+((z<<4)^(a<<5));

  }

what would you do to put this into java?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Sam Adamsh
  • 3,331
  • 8
  • 32
  • 53
  • I'm not sure what your actual question is? Java supports bit-shift and bit-wise operations in a similar fashion to Javascript (e.g. has & ? << >> >>> etc). What are you actually having trouble with? – Deco Dec 15 '11 at 04:57
  • ya its similar until it comes up to the boundary between integer max value and long.I'm trying to translate some javascript into java and it is exceedingly difficult to match the bitshift operations. Javascript translates everything in bitshift operands to 32 bit integers. in javascript : var zz=123456789123<<20 comes out to: -1473249280 . , in java, the same: long k=123456789123L<<20;, comes out to 129453826111438848 – Sam Adamsh Dec 15 '11 at 05:04
  • So you mean you wanted to replicate that undesirable behavior in Java? o_O – shinkou Dec 15 '11 at 05:09
  • absolutely i am. I need to translate javascript code into java, whether or not it is desirable. i can't fit code in here, so will paste it above. – Sam Adamsh Dec 15 '11 at 05:13

3 Answers3

2

Yes. Java has bit-wise operators and shift operators.

Is there something in particular you wish to ask?

edit: Ah, just cast to an int before shifting.

int zz = ((int) 123456789123L) << 20;

You will get -1473249280 just as you note the JavaScript gives you.

edit2: Do you just want something like this?

   long[] i=[some array with large integers];
   for(int x=0; x < 100; x++)
   {
     int a= (int) <a large integer>; // cast in case it's a long or something
     long z= <some 'long' gt 2.1 billion>;
     z=i[x]+=((int) z)>>>5)^(a<<2));
   }
jbindel
  • 5,535
  • 2
  • 25
  • 38
2

Java has bitwise operators that behave by-and-large the same. There is a subtle difference though.

Java's int type is 32-bit signed, whereas JavaScript's >>> operator returns a 32-bit unsigned integer value, so the following

"" + ((1 << 31) >>> 0)

produces

"2147483648"

in JavaScript but the same expression produces

"-2147483648"

in Java.

Java's long type will let you get all the precision you need to replicate JavaScript bit manipulation but you need to be sure to mask it to 32 signed bits when using >>> where the shift amount might be 0 (or a multiple of 32).

You can get the 32 low bits of a long by doing

(myLong & 0xffffffffL)
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

one way to translate bitshift and addition operations from javascript to java is to isolate the operands of a bit shift operation with int casts, and isolate operands of an addition/subtraction operation with long casts (since java will cast an addition operation exceeding 2 bil to an int without them, and javascript automatically casts longs in bitshifts to ints, while java doesn't:

       long z=(long)(((int)z>>>5)^((int)a<<2))+(long)(((int)z<<4)^((int)a<<5)); 
Sam Adamsh
  • 3,331
  • 8
  • 32
  • 53