• Home
  • 3.15 Bitwise OR Operator (|)

3.15 Bitwise OR Operator (|)

View Categories

3.15 Bitwise OR Operator (|)

1 min read

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 1 if either corresponding operand bit is 1.
  • A result bit is 0 only when both corresponding operand bits are 0.
  • The operands 12 and 10 have the following binary representations:12 = 1100â‚‚ 10 = 1010â‚‚ -------------- | = 1110â‚‚
  • The binary result 1110â‚‚ is equal to the decimal value 14.
  • 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.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *