在32位无符号整数位操作? [英] Bitwise operations on 32-bit unsigned ints?

查看:304
本文介绍了在32位无符号整数位操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript的执行按位运算前操作数转换为32位有符号整数。它也确实有32位有符号整数操作,这意味着结果是一个32位有符号整数。

由于我想做按位运算的32位无符号整数,我不知道是否有使用JavaScript的错位结果发现预期结果的方式。

为了证明我的想法,例如,在C,这是作为参考来想我,

  unsigned int类型A = 3774191835u;
unsigned int类型B = A>> 2;
/ * B = = 943547958 * /

在JavaScript中,

  VAR一个= 3774191835;
 变种B = A>> 2;
 / * B = = -130193866 * /

让我们试试这个有不同的操作。在C,

  unsigned int类型A = 1986735448u;
unsigned int类型B = A<< 1;
/ * B == 3973470896 * /

在JavaScript中,

  VAR一个= 1986735448;
 变种B = A&所述;&下; 1;
 / * B = = -321496400 * /

现在的JavaScript已经评估我的位运算的操作数作为一个符号整数,我们当然得到不同的结果是什么,我们会在C,在这里我们可以适当地做无符号整数位运算。

我知道这是可能的,但我不能确定的方式,我可以,基本上,把JavaScript的结果到预期的结果。


零填充权的结果由零作品只有第二壳体移位,而不是第一

  VAR一个= 3774191835;
 变种B =(A>&→2)>>> 0;
 / * B == 4164773430 * / VAR一个= 1986735448;
 变种B =(A&所述;&所述; 1)>>> 0;
 / * B == 3973470896 * /


解决方案

您只需要遵循以下规则:


  1. 最后总是逐位OPS与>>> 0,结果被间preTED为无符号。

  2. 请不要使用>>。如果最左边的位为1时它会尝试preseve符号,从而将推出1的左侧。始终使用>>>

例如:

  C:(3774191835>> 2)| 2147483648
JS:(3774191835>>→2 | 2147483648)GT;>> 0C:1986735448<< 1
JS:(1986735448<< 1)>>> 0C:3774191835&放大器; 4294967295
JS:(3774191835&安培; 4294967295)GT;>> 0

仅当最后op是>>>>>> 0是没有必要的。

JavaScript converts operands to 32-bit signed ints before doing bitwise operations. It also does the operation with 32-bit signed ints, meaning that the result is a 32-bit signed int.

Because I'd like to do bitwise operations with 32-bit unsigned ints, I'm wondering if there is a way to use JavaScript's mangled result to find out the intended result.

To demonstrate my idea, for example, in C, which is the reference as to what I'd like,

unsigned int a = 3774191835u;
unsigned int b = a >> 2;
/* b == 943547958 */

In JavaScript,

 var a = 3774191835;
 var b = a >> 2;
 /* b == -130193866 */

Let's try this with a different operation. In C,

unsigned int a = 1986735448u;
unsigned int b = a << 1;
/* b == 3973470896 */

In JavaScript,

 var a = 1986735448;
 var b = a << 1;
 /* b == -321496400 */

Now that JavaScript has evaluated my bitwise operation with the operand as an signed int, we of course, get a different result to what we would in C, where we can properly do bitwise operations on unsigned ints.

I know it's possible, but I'm unsure of a way that I can, essentially, turn JavaScript's result into the intended result.


Zero-fill right shift the result by zero works for the second case only, but not the first.

 var a = 3774191835;
 var b = (a >> 2) >>> 0;
 /* b == 4164773430 */

 var a = 1986735448;
 var b = (a << 1) >>> 0;
 /* b == 3973470896 */

解决方案

You only have to follow these rules:

  1. always end bit wise ops with ">>> 0" so the result gets interpreted as unsigned.
  2. don't use >>. If the left-most bit is 1 it will try to preseve the sign and thus will introduce 1's to the left. Always use >>>.

Examples:

C:  (3774191835 >> 2) | 2147483648
js: (3774191835 >>> 2 | 2147483648) >>> 0

C:  1986735448 << 1
js: (1986735448 << 1) >>> 0

C:  3774191835 & 4294967295
js: (3774191835 & 4294967295) >>> 0

Only if the last op is >>>, ">>> 0" is not necessary.

这篇关于在32位无符号整数位操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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