C Bitwise OR Assignment Operator

In C, the Bitwise OR Assignment |= operator is a compound assignment operator that performs a bitwise OR operation between two operands and assigns the result to the left operand.

Bitwise OR Assignment operator is commonly used in bit manipulation operations, such as setting specific bits in a variable.


Syntax of the Bitwise OR Assignment Operator

The syntax to use the Bitwise OR Assignment operator is:

</>
Copy
variable |= expression;

Explanation:

  1. variable: The left operand, which will store the result.
  2. |=: The Bitwise OR Assignment operator.
  3. expression: The right operand, which will be used in the bitwise OR operation.

Examples of the Bitwise OR Assignment Operator

1. Using Bitwise OR Assignment on Integers

In this example, we will use the |= operator to perform a bitwise OR operation on an integer.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 5;  // Binary: 0101
    int b = 3;  // Binary: 0011

    a |= b;  // a = a | b -> 0101 | 0011 = 0111 (7)

    printf("Result after Bitwise OR Assignment: %d\n", a);
    return 0;
}

Explanation:

  1. We declare two integer variables a and b, with values 5 and 3.
  2. The binary representation of a is 0101, and b is 0011.
  3. The expression a |= b performs 0101 | 0011, resulting in 0111 (decimal 7).
  4. The new value of a (7) is printed.

Output:

Result after Bitwise OR Assignment: 7

2. Setting Specific Bits Using Bitwise OR Assignment

In this example, we will use the |= operator to set specific bits in an integer variable.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int flags = 0;      // Binary: 0000
    int mask = 0b1010;  // Binary: 1010

    flags |= mask;  // flags = flags | mask -> 0000 | 1010 = 1010

    printf("Updated flags: %d\n", flags);
    return 0;
}

Explanation:

  1. We initialize flags to 0, meaning all bits are 0 (0000).
  2. We define a bitmask mask with the value 0b1010 (binary: 1010).
  3. The operation flags |= mask sets bits in flags where mask has 1s.
  4. The new value of flags is printed, which is 10.

Output:

Updated flags: 10

3. Using Bitwise OR Assignment in Boolean Flags

In this example, we will use the |= operator to enable multiple boolean flags stored in a single integer.

main.c

</>
Copy
#include <stdio.h>

#define FLAG_A 0x01  // Binary: 0001
#define FLAG_B 0x02  // Binary: 0010
#define FLAG_C 0x04  // Binary: 0100

int main() {
    int flags = 0;  // No flags set (Binary: 0000)

    // Set FLAG_A and FLAG_C using bitwise OR assignment
    flags |= FLAG_A;
    flags |= FLAG_C;

    printf("Final flags value: %d\n", flags);
    return 0;
}

Explanation:

  1. We define flag constants FLAG_A, FLAG_B, and FLAG_C as bit positions.
  2. We initialize flags to 0, meaning no flags are set.
  3. We use flags |= FLAG_A to enable FLAG_A (binary 0001).
  4. We use flags |= FLAG_C to enable FLAG_C (binary 0100).
  5. The final flags value is printed, which is 5 (binary 0101).

Output:

Final flags value: 5

Conclusion

In this tutorial, we explored the Bitwise OR Assignment |= operator in C. Important points to remember are:

  1. The |= operator performs a bitwise OR operation and assigns the result to the left operand.
  2. It is commonly used for setting specific bits and managing boolean flags.
  3. It modifies the original variable without requiring an additional assignment.