如何在Java中复制javascript移位,按位操作, [英] how to replicate javascript bit-shift, bit-wise operations in java,

查看:45
本文介绍了如何在Java中复制javascript移位,按位操作,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Java中复制javascript移位和按位操作的行为.

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));

  }

您将如何将其放入Java?

what would you do to put this into java?

推荐答案

将位转换和加法运算从javascript转换为Java的一种方法是使用int强制转换隔离位转换运算的操作数,并隔离加法/进行长整型减法运算(因为Java会将超过20亿的加法运算强制转换为没有int的int,而javascript会自动将bitshift中的long转换为int,而Java不会:

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)); 

这篇关于如何在Java中复制javascript移位,按位操作,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆