理解 JavaScript 中的按位运算

发布于 2024-07-24 07:20:10 字数 950 浏览 1 评论 0原文

我当前将数据以二进制形式存储在 XML 文档中,长度为 20 位,每个数字代表一个布尔值。

<matrix> 

    <resource type="single"> 
        <map>10001010100011110000</map> 
        <name>Resource Title</name> 
        <url>http://www.yoursite.com</url> 
    </resource>

</matrix>

我正在使用 jQuery 解析它,并且当前使用 for 循环和 charAt() 来确定是否在值 ==“1”时执行某些操作。

for (var i = 0; i < _mapLength; i++) {
    if (map.charAt(i) == "1") {
        //perform something here
    }
}

作为运行速度有点慢的巨大循环的一部分,这种情况会发生几次。 有人告诉我,我应该使用按位运算符来处理这个,它会运行得更快。

我的问题是:

有人能给我一个如何做到这一点的示例吗?我尝试过在线阅读教程,它们似乎就在我的脑海中飞过。 (仅供参考:我计划创建一个 Ruby 脚本,将二进制 0 和 1 转换为 XML 中的位。)

或者有人知道一个好的、简单的(甚至可能是简化版本) 教程或其他可以帮助我掌握这些按位运算符概念的东西?

I am currently storing data inside an XML doc as binary, 20 digits long, each representing a boolean value.

<matrix> 

    <resource type="single"> 
        <map>10001010100011110000</map> 
        <name>Resource Title</name> 
        <url>http://www.yoursite.com</url> 
    </resource>

</matrix>

I am parsing this with jQuery and am currently using a for loop and charAt() to determine whether to do stuff if the value is == "1".

for (var i = 0; i < _mapLength; i++) {
    if (map.charAt(i) == "1") {
        //perform something here
    }
}

This takes place a few times as part of a HUGE loop that has run sort of slow. Someone told me that I should use bitwise operators to process this and it would run faster.

My question is either:

Can someone offer me an example of how I could do this? I've tried to read tutorials online and they seem to be flying right over my head. (FYI: I am planning on creating a Ruby script that will convert my binary 0 & 1's into bits in my XML.)

Or does anyone know of a good, simple (maybe even dumbed down version) tutorial or something that could help me grasp these bitwise operator concepts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

姐不稀罕 2024-07-31 07:20:10

假设您的长度不超过 32 位,您可以使用 JavaScript 的内置 parseInt() 函数将 1 和 0 的字符串转换为整数,然后使用 & 测试标志;(和)运算符:

var flags = parseInt("10001010100011110000", 2); // base 2

if ( flags & 0x1 )
{
   // do something
}

...

另请参阅:如何检查我的字节标志?

(问题是关于 C 中的使用,但也适用于 JS 中的相同运算符)

Assuming you have no more than 32 bits, you can use JavaScript's built-in parseInt() function to convert your string of 1s and 0s into an integer, and then test the flags using the & (and) operator:

var flags = parseInt("10001010100011110000", 2); // base 2

if ( flags & 0x1 )
{
   // do something
}

...

See also: How to check my byte flag?

(question is on the use in C, but applies to the same operators in JS as well)

太阳男子 2024-07-31 07:20:10

单个 & 符号(&,与 && 相对)进行按位比较。 但首先您需要使用 parseInt() 将字符串转换为数字。

var map = parseInt("10010", 2); // the 2 tells it to treat the string as binary

var maskForOperation1 = parseInt("10000", 2);
var maskForOperation2 = parseInt("01000", 2);
// ...

if (map & maskForOperation1) { Operation1(); }
if (map & maskForOperation2) { Operation2(); }
// ...

Single ampersand (&, as opposed to &&) does bit-wise comparison. But first you need to convert your strings to numbers using parseInt().

var map = parseInt("10010", 2); // the 2 tells it to treat the string as binary

var maskForOperation1 = parseInt("10000", 2);
var maskForOperation2 = parseInt("01000", 2);
// ...

if (map & maskForOperation1) { Operation1(); }
if (map & maskForOperation2) { Operation2(); }
// ...
贪了杯 2024-07-31 07:20:10

要极其警惕。 JavaScript 没有整数——数字以 64 位浮点数存储。 您应该能够准确转换为 52 位。 如果你得到的标志比这个多,那么当你的“数字”四舍五入到最接近的可表示浮点数时,就会发生不好的事情。 (哎哟!)

此外,按位操作不会提高性能,因为浮点数将被转换为整数,进行测试,然后再转换回来。

如果您想在多个地方检查标志,我会在一个对象上设置标志,最好带有名称,如下所示:等等

var flags = {};
flags.use_apples = map.charAt(4);
flags.use_bananas = map.charAt(10);

...

然后您可以在循环内测试这些标志:

if(flags.use_apples) {
    do_apple_thing();
}

对象槽测试将是比按位检查更快,因为 Javascript 没有针对按位运算符进行优化。 但是,如果您的循环很慢,我担心解码这些标志可能不是缓慢的根源。

Be extremely wary. Javascript does not have integers -- numbers are stored as 64 bit floating-point. You should get accurate conversion out to 52 bits. If you get more flags than that, bad things will happen as your "number" gets rounded to the nearest representable floating-point number. (ouch!)

Also, bitwise manipulation will not help performance, because the floating point number will be converted to an integer, tested, and then converted back.

If you have several places that you want to check the flags, I'd set the flags on an object, preferably with names, like so:

var flags = {};
flags.use_apples = map.charAt(4);
flags.use_bananas = map.charAt(10);

etc...

Then you can test those flags inside your loop:

if(flags.use_apples) {
    do_apple_thing();
}

An object slot test will be faster than a bitwise check, since Javascript is not optimized for bitwise operators. However, if your loop is slow, I fear that decoding these flags is probably not the source of the slowness.

居里长安 2024-07-31 07:20:10

按位运算符肯定会更快,但只是线性的,而且不会快很多。 您可能会节省几毫秒(除非您正在用 Javascript 处理大量数据,无论如何这很可能是一个坏主意)。

您应该考虑分析循环中的其他代码,看看是什么最拖慢了它的速度。 还有哪些其他算法、数据结构和分配可以使用重构?

Bitwise operators will certainly be faster but only linearly and not by much. You'll probably save a few milliseconds (unless you're processing HUGE amounts of data in Javascript, which is most likely a bad idea anyway).

You should think about profiling other code in your loop to see what's slowing it down the most. What other algorithms, data structures and allocations do you have in there that could use refactoring?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文