平等否定以确定最不重要的位
基本上,我想检查最后一个重要位是0。 我已经有一个条件可以测试钻头是否设置(或者至少是我相信):
cells[x + y * cols] & 1 === 1
在某个时候,我需要否定这一点,所以我想:
cells[x + y * cols] & 1 === 0
但是我没有得到预期的结果。所以我只是:
(!cells[x + y * cols] & 1 === 1)
根据预期结果,这似乎更合适。 在两种情况下,我都没有得到预期的结果。 也许问题来自其他地方,或者我只是在误解使用位操作员的使用。
因此,首先,为什么这些行是不同的:
console.log(!(4 & 1 === 1)); // true
console.log(4 & 1 === 0); // 0
我的意思是,显然4的LSB为0。 我会说第一个是我需要的...
Basically, I want to check if the last significant bit is a 0.
I already had a condition to test if the bit is set or not (or at least, I believe so) :
cells[x + y * cols] & 1 === 1
At some point, I need the negation of this, so I thought :
cells[x + y * cols] & 1 === 0
But I didn't get the expected result. So I just :
(!cells[x + y * cols] & 1 === 1)
Which seems more appropriate according to the expected result.
I don't get the expected result actualy, in both case.
Maybe the problem comes from somewhere else or maybe I'm just misunderstanding the use of the bitwise operator.
So first of all, why these line are differents:
console.log(!(4 & 1 === 1)); // true
console.log(4 & 1 === 0); // 0
I mean, obviously the LSB of 4 is 0.
I would say the first one is the one I need though...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要包装
4& 1
像这样的括号中,因为如果您不这样做,则优先级
1 === 0
是false
,然后4& false
与4& 0
。You need to wrap the
4 & 1
into parenthesis like soBecause if you don't, it prioritizes
1 === 0
which isfalse
and then4 & false
which is the same as4 & 0
.