奇怪的运算符“|=”
我对编码比较陌生,无论如何,我在复制 iphone 像素技术教程时遇到了这个 if 语句,无论如何,我不太确定它在做什么,所以如果有人能为我解释一下,那就太棒了,谢谢。
for(int j = 0; j < (width * height); j++ )
{
if ( pixels[j] & 0xff000000 )
{
collisionMap[j] |= 1;
}
}
让我困惑的部分是“|=”和单个“&”符号。这是怎么回事?谢谢
i'm relatively new to coding, anyway i came across this if statement while copying a tutorial for an iphone pixel technique, anyway i'm not really sure what it's doing so if anybody could explain it for me it would be awesome thanks.
for(int j = 0; j < (width * height); j++ )
{
if ( pixels[j] & 0xff000000 )
{
collisionMap[j] |= 1;
}
}
The parts that confuse me are the '|=' and the single '&' sign. What's going on here? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
&是一个按位与。
|= 是一个按位 OR 并将值分配给
collisionMap[j]
。& is a bitwise AND.
|= is a bitwise OR and assigns the value to
collisionMap[j]
.它们被称为 按位运算符
collisionMap[j] |= 1
是相当于 CollisionMap[j] = CollisionMap[j] | 1这将确保设置了
collisionMap[j]
的 LSBThey are called bitwise operators
collisionMap[j] |= 1
is equivalent tocollisionMap[j] = collisionMap[j] | 1
Which will make sure the LSB of
collisionMap[j]
is set|=
对两侧的值执行按位或(考虑优先级),将结果放入左侧变量中。因此,a |= b
与a = a | 相同。 b
(假设没有 C++ 运算符重载)。更具体地说,&
执行按位 AND,生成一个仅包含两侧值中的位的值。|=
performs a bitwise OR of the values on either side (considering precendence), putting the result into the left-side variable. So,a |= b
is the same asa = a | b
(assuming no C++ operator overloading). More concretely, say:&
performs a bitwise AND, resulting in a value with only the bits from the values on either side.单曲&执行按位与,而
|=
运算符执行按位或。The single & does a bitwise and, while the
|=
operator does a bitwise or.