Table of Contents
The bitwise OR operator (|) performs a logical OR operation on each corresponding bit of two integral operands. A bit in the result is set to 1 if either corresponding operand bit is 1. The result bit is 0 only when both corresponding bits are 0.
Bitwise OR is commonly used to set one or more bits in a value without affecting other bits.
Source Code #
#include <iostream>
int main()
{
int num1 = 12;
int num2 = 10;
int result = num1 | num2;
std::cout << "First Number : " << num1 << '\n';
std::cout << "Second Number: " << num2 << '\n';
std::cout << "Result : " << result << '\n';
return 0;
}
Output #
First Number : 12
Second Number: 10
Result : 14
Explanation #
- The bitwise OR operator (
|) compares the corresponding bits of two integral operands. - A result bit is set to
1if either corresponding operand bit is1. - A result bit is
0only when both corresponding operand bits are0. - The operands
12and10have the following binary representations:12 = 1100â‚‚ 10 = 1010â‚‚ -------------- | = 1110â‚‚ - The binary result
1110â‚‚is equal to the decimal value14. - Bitwise OR operations are commonly used to set specific bits in flags and control registers.
- The bitwise OR operator is applicable only to integral data types.
return 0;terminates the program successfully.