# Bitwise OR ( | ) Operator in C programming Language

Spread the love
In previous posts, we discussed what are bitwise operators and different types of Bitwise Operators. Also, we discussed Bitwise AND operator, Today in this quick tutorial I am going to explain BITWISE OR operator.

### Bitwise OR:

• The output of Bitwise OR is true if anyone of the Input is true. Otherwise, the output is 0.
• In other words, Output of Bitwise OR is zero only on each and every Input is Zero, Otherwise, the output will be one.
• Bitwise OR is denoted with “ | ”.

### Here is the Truth Table for Bitwise OR:

 INPUT 1 INPUT 2 OUTPUT 0 0 0 0 1 1 1 0 1 1 1 1

### Example to Understand the Bitwise OR operator:

Let’s take A and B and assume A=10 and B=20.
As we already know that Bitwise Operators, operates on Bit-level unlike Logical Operators. So we need to take binary equivalents of A and B to calculate A | B.
Bitwise A|B is:
A     –>       0000 1010   (Binary Equivalent of 10)
B     –>       0001 0100    (Binary Equivalent of 20)
A|B –>       0001 1110     (= 30)

### Example program to Understand Bitwise OR ( | ) Operator :

1. #include<stdio.h>
2. int main()
3. {
4.      int a,b;
5.      a=10,b=20;
6.      printf(“bitwise OR of A and B (a|b) : %d n”,| b);
7.      return 0;
8. }

Output :

bitwise OR of A and B (a|b) : 30

Note:Bitwise OR is completely different from Logical OR. Logical OR works on byte level, on other hand Bitwise OR is works on Bit-Level.

### Program to Understand Difference between Bitwise OR and Logical OR :

1. #include<stdio.h>
2. int main()
3. {
4.      int a,b;
5.      a=10;
6.      b=20;
7.      printf(” Bitwise OR of A and B is ( A | B) : %d n, a|);
8.      printf(” Logical OR of A and B is ( A || B) : %d n, a||);
9.      return 0;
10. }
Output :

Bitwise OR of A and B is ( A | B) : 30 Logical OR of A and B is ( A || B) : 1

For more details on LOGICAL OR Operator Visit this Post Logical Operators in C“.

### Quick tips on Bitwise OR Operator:

Tip 1:
Output :

431

Tip 2 :
Output :

-1

Explanation:
Bitwise OR of -1 and 431 is  -1, because Binary Equivalent of -1 is all ones. Have a look on below Explanation
-1            =             1111 1111 1111 1111        (Binary Equivalent)
431         =             0000 0001 1010 1111        (Binary Equivalent)
Result   =             1111 1111 1111 1111   –>  -1
So Bitwise OR of –1 and any value is results into -1.
Few more Examples:

-1 | 1000             –>        -1
-1 | 1                   –>        -1
321 | -1               –>        -1
So it is clear that bitwise OR of –1 and any number is equal to -1.

### Also Read : Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.