The pipe ( | ) operator in C++

The pipe operator (|) in C++ is also known as bitwise or operator. The “bitwise or” operator (or pipe operator) in C++ takes two numbers as operands and checks their corresponding bits. If the bit of any of the operands is set (bit 1 is called a set bit) then the corresponding bit of output will also be set or if both of the operands have 0 bit then the corresponding output bit will also be 0.

Examples:

2|3 = 3 
Explanation: In binary representation  10 | 11 = 11.

Here we can see that the least significant bit of 2 is not set but the same of 3 is set. So corresponding output bit is also set. The second bit in both of the numbers is set so the output bit is also set.

Below is the code implementation-


#include<bits/stdc++.h>
using namespace std;

int main(){
    int a=2;
    int b=3;
    int c= a|b;
    cout<<c<<endl;
    return 0;
}

Output:

3

Also read: The new[] operator in C++

Leave a Reply

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