The |= operator in C is a bitwise OR operator, which performs a bitwise logical OR operation on two integers. If both bits are 1, the result is 1; otherwise, the result is 0 . It can be used to set or update bit flags, combination flags and check flags.
|= Operator in C
What is |= Operator?
|= is the bitwise OR operator in C, which performs a bitwise logical OR operation on two integers.
Operation Principle
Assume there are two integers a and b, and the |= operator performs a logical OR operation on each binary bit of a and b. If both bits are 1, the result is 1; otherwise, the result is 0.
For example:
<code class="cpp">a = 01101 (二进制) = 13 (十进制) b = 10010 (二进制) = 18 (十进制) a |= b = 11111 (二进制) = 31 (十进制)</code>
Usage
|= operator can be used in various scenarios, including:
Example
<code class="cpp">// 设置第 3 位 int x = 0; x |= (1 << 2); // x 现在为 00000100 (二进制) // 组合标志 int flags = 0; flags |= FLAG_READ; // 设置 FLAG_READ 标志 flags |= FLAG_WRITE; // 设置 FLAG_WRITE 标志 // 检查标志 if (flags & FLAG_READ) { // FLAG_READ 标志已设置 }</code>
The above is the detailed content of What does |= mean in c++. For more information, please follow other related articles on the PHP Chinese website!