C语言 <<= 和 |= 的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6134417/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:46:36  来源:igfitidea点击:

Meaning of <<= and |=

coperatorssemanticsbit-shiftlogical-operators

提问by SK9

What is the meaning of <<=and |=in C?

<<=|=在C中是什么意思?

I recognise <<is bitshift etc. but I don't know what these are in combination.

我承认<<是 bitshift 等,但我不知道这些组合是什么。

回答by Chris Cooper

Just as x += 5means x = x + 5, so does x <<= 5mean x = x << 5.

正如x += 5手段x = x + 5,所以不x <<= 5平均x = x << 5

Same goes for |. This is a bitwise or, so x |= 8would mean x = x | 8.

也一样|。这是一个按位or,所以x |= 8将意味着x = x | 8

Here is an example to clarify:

这是一个澄清的例子:

int x = 1;
x <<= 2;         // x = x << 2;
printf("%d", x); // prints 4 (0b001 becomes 0b100)

int y = 15;
y |= 8;          // y = y | 8;
printf("%d", y); // prints 15, since (0b1111 | 0b1000 is 0b1111)