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:
result = operand1 | operand2;
Explanation:
operand1
: The first integer operand.|
: The Bitwise OR operator.operand2
: The second integer operand.result
: The integer result of the bitwise OR operation.- The operation is performed bit by bit, where each bit is set to
1
if at least one of the corresponding bits inoperand1
oroperand2
is1
.
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
#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:
- We declare two integers
a
(5) andb
(3). - We apply the
|
operator toa
andb
. - The binary representation of
5
is0101
and of3
is0011
. - Performing a bitwise OR operation:
0101 (5)
| 0011 (3)
------------
0111 (7)
- The result is stored in
result
and printed as7
.
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
#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:
- We declare an integer
num
with the value8
(binary1000
). - We declare a mask
2
(binary0010
). - The Bitwise OR operation sets the bits where either operand has
1
:
1000 (8)
| 0010 (2)
------------
1010 (10)
- The result is stored in
result
and printed as10
.
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
#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:
- We define two flags
FLAG1
(1) andFLAG2
(2). - We declare
status
and initialize it to0
. - Using
status = status | FLAG1
, we set FLAG1:
0000 (0)
| 0001 (1)
------------
0001 (1)
- Using
status = status | FLAG2
, we set FLAG2:
0001 (1)
| 0010 (2)
------------
0011 (3)
- The final value of
status
is3
, 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 is1
. - It is useful for bit manipulation, setting bits, and flag operations.