C Bitwise OR Operator

In C, the Bitwise OR | operator is used to perform a bitwise OR operation between two integer operands. The operation compares corresponding bits of both operands and sets the resulting bit to 1 if at least one of the corresponding bits is 1. Otherwise, the resulting bit is 0.

The Bitwise OR operator is commonly used in bitwise manipulation, setting specific bits in a number, and working with flags.


Syntax of the Bitwise OR Operator

The syntax for using the Bitwise OR operator is:

</>
Copy
result = operand1 | operand2;

Explanation:

  1. operand1: The first integer operand.
  2. |: The Bitwise OR operator.
  3. operand2: The second integer operand.
  4. result: The integer result of the bitwise OR operation.
  5. The operation is performed bit by bit, where each bit is set to 1 if at least one of the corresponding bits in operand1 or operand2 is 1.

Examples of the Bitwise OR Operator

1. Performing a Bitwise OR Operation on Two Integers

In this example, we will perform a Bitwise OR operation on two integers and display the result.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 5, b = 3;
    int result = a | b;

    printf("Bitwise OR of %d and %d is %d\n", a, b, result);
    return 0;
}

Explanation:

  1. We declare two integers a (5) and b (3).
  2. We apply the | operator to a and b.
  3. The binary representation of 5 is 0101 and of 3 is 0011.
  4. Performing a bitwise OR operation:
  0101 (5)
| 0011 (3)
------------
  0111 (7)
  1. The result is stored in result and printed as 7.

Output:

Bitwise OR of 5 and 3 is 7

2. Using Bitwise OR to Set Specific Bits

In this example, we will use the Bitwise OR operator to set specific bits in a number.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 8; // Binary: 1000
    int mask = 2; // Binary: 0010
    int result = num | mask;

    printf("Result after setting bits: %d\n", result);
    return 0;
}

Explanation:

  1. We declare an integer num with the value 8 (binary 1000).
  2. We declare a mask 2 (binary 0010).
  3. The Bitwise OR operation sets the bits where either operand has 1:
  1000 (8)
| 0010 (2)
------------
  1010 (10)
  1. The result is stored in result and printed as 10.

Output:

Result after setting bits: 10

3. Using Bitwise OR for Flag Operations

In this example, we will use the Bitwise OR operator to set multiple flags in a status variable.

main.c

</>
Copy
#include <stdio.h>

#define FLAG1 1  // Binary: 0001
#define FLAG2 2  // Binary: 0010

int main() {
    int status = 0;

    // Setting FLAG1 and FLAG2 using Bitwise OR
    status = status | FLAG1;
    status = status | FLAG2;

    printf("Status after setting flags: %d\n", status);
    return 0;
}

Explanation:

  1. We define two flags FLAG1 (1) and FLAG2 (2).
  2. We declare status and initialize it to 0.
  3. Using status = status | FLAG1, we set FLAG1:
  0000 (0)
| 0001 (1)
------------
  0001 (1)
  1. Using status = status | FLAG2, we set FLAG2:
  0001 (1)
| 0010 (2)
------------
  0011 (3)
  1. The final value of status is 3, which is printed to output.

Conclusion

In this tutorial, we learned how to use the Bitwise OR | operator in C:

  • It sets a bit to 1 if at least one of the corresponding bits is 1.
  • It is useful for bit manipulation, setting bits, and flag operations.