C语言 C 中的按位运算 |=
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40551362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bitwise operation |= in C
提问by semenoff
I am going through example code and found this operation:
我正在查看示例代码并发现此操作:
displayMap[x + (y/8)*LCD_WIDTH]|= 1 (shift by) shift;
where
在哪里
byte shift = y % 8;
I understand |operand and =but what are two of them together do.
我理解|操作数,=但是它们两个一起做什么。
回答by Govind Parmar
|performs a bitwise OR on the two operands it is passed.
|对其传递的两个操作数执行按位或运算。
For example,
例如,
byte b = 0x0A | 0x50;
If you look at the underlying bits for 0x0Aand 0x50, they are 0b00001010and 0b01010000respectively. When combined with the OR operator the result in bis 0b01011010, or 0x5Ain hexadecimal.
如果您查看0x0Aand的底层位0x50,它们分别是0b00001010和0b01010000。与 OR 运算符结合使用时,结果b为0b01011010, 或0x5A十六进制。
|=is analogous to operators like +=and -=in that it will perform a bitwise OR on the two operands then store the result in the left operator.
|=类似于像运营商+=,并-=在其将执行按位或在两个操作数,然后将结果存储在左边的操作。
byte b = 0x0A;
b |= 0x50;
// after this b = 0x5A

