在Javascript中检查Bitwise [英] Check Bitwise in Javascript

查看:63
本文介绍了在Javascript中检查Bitwise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我们将100663296设置为GM Leader,但数据库中的此字段也会被写入不同的内容,因此它将该数字更改为100794368我被告知可能使用逐位检查来检查是否第一个号码与第二个号码相同,我用谷歌搜索了比特检查,但对于我的支票用什么感到困惑。



这里有其他一些更改的数字,包括上面的数字。



预定义数字新更改的数字/不断更改的数字。 
100663296 = 100794368
67108864 = 67239936
117440512 = 2231767040





所以我该如何去做检查这些数字?



这是我在使用之前使用的代码的一部分,我注意到数字的变化。



 <   pre     lang   =  xml >  if(playerData [i] .nameflags == 67108864)
{
playerRows + =&#39 ;& lt; img src =& quot; icons / GM-Icon.png& quot; ALT =&安培; QUOT; GM&安培; QUOT;标题=安培; QUOT; GM&安培; QUOT;&安培; GT;&安培; LT; / IMG&安培; GT;&安培;#39 ;;
} < / pre >

解决方案

如果你需要检查两个整数的完全按位相等,你只需要''==' '运算符,但要使用它,你应该保证两个操作数都是整数:



 left =  12323 ; 
right = 12323 ;
if (left == right)
alert( 操作数是二进制相等的;我保证。:-));





但要小心;如果至少有一个操作数是表示数字的字符串而不是数字,则两个操作数都将被视为字符串,您可能会得到令人困惑的结果:



 left =   012323; 
right = 12323 ;
if (left!= right)
alert( 操作数不相等,即使它们代表相等的值,因为它们被比作字符串。);





一般来说,这些天,尝试使用表示数据而不是数据本身的字符串来操作是初学者的真正诅咒;并且很难向他们解释。在JavaScript中解释起来特别困难,它的松散型打字概念本身非常复杂且难以理解,背后是虚幻的简单性。



最后,如果你需要比较不同的位(并且,从你的问题,我看不到这个需要),你可以使用二元运算符:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators [ ^ ] 。



就是这样,基本上。



-SA

In my database we set 100663296 to be a GM Leader but also this field in the database gets written to for different things, so it changes that number to 100794368 i was told to possible use a bit-wise check to check whether the first number is the same as the second number, and I have googled on using bit-wise checks but got confused as to what to use for my check.

Here are some other numbers that change, including the one from above.

predefined number   new changed number/ever changing number.
100663296       =       100794368
67108864        =       67239936
117440512       =       2231767040



so how should i go about checking these numbers?

And here is part of my code that i was using before i noticed the change in the numbers.

<pre lang="xml">if (playerData[i].nameflags == 67108864)
{
    playerRows += &#39;&lt;img src =&quot;icons/GM-Icon.png&quot; alt=&quot;GM&quot; title=&quot;GM&quot;&gt;&lt;/img&gt;&#39;;
}</pre>

解决方案

If you need to check for full bitwise equality of two integers, all you need is just ''=='' operator, but to use it, you should guarantee that both operands are integers:

left = 12323;
right = 12323;
if (left == right)
   alert("Operands are binary equal; I'll guarantee that. :-)");



Be very careful though; if at least one of operands is string representing number, not a number, both operands will be considered strings and you can get confusing results:

left = "012323";
right = 12323;
if (left != right)
   alert("Operands are not equal, even though they represent 'equal values', as they are compared as strings.");



In general, these days, the attempt to operate with strings representing data instead of data itself is a real curse of the beginners; and it''s hard to explain to them. It is especially difficult to explain in JavaScript, with its loose-type typing concept, which is itself very complex and hard to understand, behind the illusory simplicity.

Finally, if you need to compare separate bits (and, from your question, I don''t see this need), you can use binary operators:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators[^].

That''s it, basically.

—SA


这篇关于在Javascript中检查Bitwise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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