JavaScript的按位屏蔽 [英] JavaScript Bitwise Masking

查看:121
本文介绍了JavaScript的按位屏蔽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是类似此另一个问题;不过,我想明白为什么这是工作,因为它是。

This question is similar to this other question; however, I'd like to understand why this is working as it is.

以下code:

console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x0000ff00', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x00ff0000', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0xff000000', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x0000ffff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x00ffffff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0xffffffff', 16)).toString(16));

返回:

ef
be00
ad0000
-22000000
ef
beef
adbeef
-21524111

当什么,我从.string期待(16)将是:

When what I am expecting from .string(16) would be:

ef
be00
ad0000
de000000
ef
beef
adbeef
deadbeef

这是怎么回事这个?

What's going on with this?

感谢您提前对您有所帮助。

Thank you in advance for your help.

随着谢谢你的反应和评论者下方,同时也为以下来源:

With thank you to the responders and commenters below, and also to the following sources:

  • http://speakingjs.com/es5/ch11.html
  • JavaScript C Style Type Cast From Signed To Unsigned

下面是通过提供实用功能的工作原理转换基数为16的32位数字,并从一个符号的32位整数一个解决方案:

Here is a solution that works by providing utility functions to convert a radix-16 32-bit number to and from a signed 32-bit integer:

// Convert 'x' to a signed 32-bit integer treating 'x' as a radix-16 number
// c.f. http://speakingjs.com/es5/ch11.html
function toInt32Radix16(x) {
    return (parseInt(x, 16) | 0);
}

// Convert a signed 32-bit integer 'x' to a radix-16 number
// c.f. http://stackoverflow.com/questions/14890994/javascript-c-style-type-cast-from-signed-to-unsigned
function toRadix16int32(x) {
    return ((x >>> 0).toString(16));
}

console.log(toRadix16int32(toInt32Radix16('0xdeadbeef') & toInt32Radix16('0x000000ff')));
console.log(toRadix16int32(toInt32Radix16('0xdeadbeef') & toInt32Radix16('0x0000ff00')));
console.log(toRadix16int32(toInt32Radix16('0xdeadbeef') & toInt32Radix16('0x00ff0000')));
console.log(toRadix16int32(toInt32Radix16('0xdeadbeef') & toInt32Radix16('0xff000000')));
console.log(toRadix16int32(toInt32Radix16('0xdeadbeef') & toInt32Radix16('0x000000ff')));
console.log(toRadix16int32(toInt32Radix16('0xdeadbeef') & toInt32Radix16('0x0000ffff')));
console.log(toRadix16int32(toInt32Radix16('0xdeadbeef') & toInt32Radix16('0x00ffffff')));
console.log(toRadix16int32(toInt32Radix16('0xdeadbeef') & toInt32Radix16('0xffffffff')));

这产生预期的输出结果:

Which yields the expected output:

ef
be00
ad0000
de000000
ef
beef
adbeef
deadbeef

,以及对我的关于JavaScript的整数行为的一部分了一些很好的学习。

As well as some good learning on my part about JavaScript integer behavior.

推荐答案

在JavaScript中,所有的位操作(和&安培; 其中)的回报签署的32位整数,结果,范围-2 31 通过2 31 -1,包容性。这就是为什么你有额外的位( 0xde000000 大于 0x7ffffff )再$ P $现在psenting的标志,意义你得到一个负值来代替。

In JavaScript, all bitwise operations (and & among them) return signed 32-bit integer as a result, in the range −231 through 231−1, inclusive. That's why you have that extra bit (0xde000000 is greater than 0x7ffffff) representing a sign now, meaning that you get a negative value instead.

一个可能的修正:

var r = 0xdeadbeef & 0xff000000;
if (r < 0) {
  r += (1 << 30) * 4;
}
console.log( r.toString(16) ); // 'de000000'

这篇关于JavaScript的按位屏蔽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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