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:
variable |= expression;
Explanation:
variable: The left operand, which will store the result.|=: The Bitwise OR Assignment operator.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
#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:
- We declare two integer variables
aandb, with values 5 and 3. - The binary representation of
ais0101, andbis0011. - The expression
a |= bperforms0101 | 0011, resulting in0111(decimal 7). - 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
#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:
- We initialize
flagsto 0, meaning all bits are 0 (0000). - We define a bitmask
maskwith the value0b1010(binary: 1010). - The operation
flags |= masksets bits inflagswheremaskhas 1s. - The new value of
flagsis 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
#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:
- We define flag constants
FLAG_A,FLAG_B, andFLAG_Cas bit positions. - We initialize
flagsto 0, meaning no flags are set. - We use
flags |= FLAG_Ato enableFLAG_A(binary0001). - We use
flags |= FLAG_Cto enableFLAG_C(binary0100). - The final
flagsvalue is printed, which is 5 (binary0101).
Output:
Final flags value: 5
Conclusion
In this tutorial, we explored the Bitwise OR Assignment |= operator in C. Important points to remember are:
- The
|=operator performs a bitwise OR operation and assigns the result to the left operand. - It is commonly used for setting specific bits and managing boolean flags.
- It modifies the original variable without requiring an additional assignment.
