如何做到位与在JavaScript上,比32位长的变量? [英] How to do bitwise AND in javascript on variables that are longer than 32 bit?

查看:99
本文介绍了如何做到位与在JavaScript上,比32位长的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript 2号,我想一下,。他们都是33bit长

I have 2 numbers in javascript that I want to bit and. They both are 33bit long

在C#:<​​/ P>

in C#:

 ((4294967296 & 4294967296 )==0) is false

但在javascript:

but in javascript:

 ((4294967296 & 4294967296 )==0) is true

4294967296是((长)1) - =;&LT; 32

4294967296 is ((long)1) << 32

据我了解,这是由于JavaScript的转换价值进行逐位运算时INT32的事实。

As I understand it, it is due to the fact that javascript converts values to int32 when performing bit wise operations.

我如何解决此问题? 关于如何更换位和一组其它数学运算,使位不丢失任何建议?

How do I work around this? Any suggestions on how to replace bit and with a set of other math operations so that bits are not lost?

推荐答案

您可以每个瓦尔的分成2个32位值(如高字和低字),然后做两个对的位操作。

You could split each of the vars into 2 32-bit values (like a high word and low word), then do a bitwise operation on both pairs.

下面的脚本作为Windows的.js脚本运行。您可以为网络代替WScript.Echo()与警告()。

The script below runs as a Windows .js script. You can replace WScript.Echo() with alert() for Web.

var a = 4294967296;
var b = 4294967296;

var w = 4294967296; // 2^32

var aHI = a / w;
var aLO = a % w;
var bHI = b / w;
var bLO = b % w;

WScript.Echo((aHI & bHI) * w + (aLO & bLO));

这篇关于如何做到位与在JavaScript上,比32位长的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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