奇怪的运算符“|=”

发布于 2024-11-08 15:18:11 字数 293 浏览 1 评论 0原文

我对编码比较陌生,无论如何,我在复制 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 技术交流群。

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

发布评论

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

评论(4

还如梦归 2024-11-15 15:18:11

&是一个按位与。

|= 是一个按位 OR 并将值分配给 collisionMap[j]

& is a bitwise AND.

|= is a bitwise OR and assigns the value to collisionMap[j].

没有伤那来痛 2024-11-15 15:18:11

它们被称为 按位运算符

collisionMap[j] |= 1 是相当于 CollisionMap[j] = CollisionMap[j] | 1

这将确保设置了 collisionMap[j] 的 LSB

They are called bitwise operators

collisionMap[j] |= 1 is equivalent to collisionMap[j] = collisionMap[j] | 1

Which will make sure the LSB of collisionMap[j] is set

帝王念 2024-11-15 15:18:11

|= 对两侧的值执行按位或(考虑优先级),将结果放入左侧变量中。因此,a |= ba = a | 相同。 b(假设没有 C++ 运算符重载)。更具体地说,

a is 01010000 binary, and
b is 10011001 binary, then after `a |= b`...
a is 11011001.

& 执行按位 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 as a = a | b (assuming no C++ operator overloading). More concretely, say:

a is 01010000 binary, and
b is 10011001 binary, then after `a |= b`...
a is 11011001.

& performs a bitwise AND, resulting in a value with only the bits from the values on either side.

人心善变 2024-11-15 15:18:11

单曲&执行按位与,而 |= 运算符执行按位或。

The single & does a bitwise and, while the |= operator does a bitwise or.

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