Bitwise OR Operator in C Programming Language

Spread the love

Bitwise-OR-Operator-in-C-with-Example-Programs

Introduction:

In the previous articles, We have discussed what are Bitwise Operators and Bitwise AND Operator. In today’s article, we will learn about the Bitwise OR operator in C Programming Language.

Bitwise OR Operator in C Language:

The Bitwise OR operator is Binary Operator and Takes two input values ( Binary sequences of two values ) and performs the Bitwise OR on each pair of bits in the given binary sequence.

In the C Programming Language, The bitwise OR operator is denoted by the Vertical Bar or Vertical Line |.

The result of Bitwise OR operation is False(0) only if both of the input bits are False(0).

Bitwise Operator returns True(1), When at least one of the input bits are True(1)

Bitwise OR operator can be applied on Integer data values only. We can’t apply bitwise OR to the Floating point data.

Truth Table of Bitwise OR Operator:

Input bit 1 ( Xi )Input bit 2 (Yi)Result ( Xi | Yi )
000
011
101
111

Here Xi and Yi are the Pair of bits in the Binary sequence of X and Y values.

How Bitwise OR ( ‘|’ ) Operator in C Works? :

Let’s go through a step by step example, We will take two numbers X=6 and Y=10.

Now we need to apply the Bitwise OR operator on variables X and Y. i.e X | Y.

To calculate the bitwise OR operator, First of all, we need to convert the values of X and Y into the Binary form or Base-2 form.

➡️ For making calculations easy, We are going to take only 8 bits for the integer value. But note that the Integer value will be stored in 32 bits in the memory.

The Binary Equivalent of the X or 6 is 0000 0110.

And Binary Equivalent of the Y or 10 is 0000 1010.

The Left-most bit in the binary representation is MSB or Most significant bit and the Right-most bit is the LSB or Least Significant bit.

Now we need to apply the Bitwise OR operation on each pair of bits.

Here Pair of bits means, MSB in the first number X and MSB in the second number Y, So on until we reach the LSB of X and LSB of Y.

➡️ Bitwise X | Y is:
                X     –>      0000 0110   (6 in Binary)
                Y     –>      0001 1010   (10 in Binary)
                X | Y –>      0000 1110   ( X | Y = 14 )

So the result of the bitwise OR operation of X and Y ( 6 | 10 ) is 14.

Here is the pictorial representation of the above Bitwise OR operation.

Bitwise-OR-Operator-in-C-Language

As we can see from the above graphical representation, The Bitwise OR operator perform on the Bit-Level. So only if both bits are False or Zero the resulting bit will be Zero.

So above problem X | Y or 6 | 10 is 14.

Let’s see a program to use the bitwise OR operator.

Example Program : Bitwise OR Operator in C:

We are going to apply the bitwise OR operator on the above values 6 and 10.

Program Output:

As you can see the output of 6 & 10 is 14.

Bitwise OR Example 2:

Let’s look at one more example. Will take two numbers A=16 and B=15.

The Bitwise OR of A and B ( A | B ) is 31.

As we discussed earlier, We need to convert the given A and B values into the Binary representation.

Binary Equivalent of the variable A ( i.e 16 ) is 0001 0000

Similarly, The binary equivalent of variable B(i.e 15) is 0000 1111

Now we need to apply the Bitwise OR operator on variables A and B i.e A | B.

➡️ Bitwise A | B is:
                A     –>    0001 0000   (16 in Binary)
                B     –>     0000 1111    (15 in Binary)
             A | B –>     0001 1111   ( A | B = 31 )

Here is the pictorial representation of above Bitwise OR calculation.

Bitwise-OR-Operator-in-C-Language

If any one of the input bits are True(1), Then the resulting bit will be 1.

let’s verify the above bitwise OR calculations using the C program.

Example Program 2: Bitwise OR Operator:

Applying the bitwise OR operation on values 16 and 15

Program Output:

Difference between the Logical OR and Bitwise OR Operators in C:

Don’t confuse with the bitwise OR with the Logical OR operator, The Logical OR Operator is denoted with Two Vertical Bars ( || ), While the Bitwise OR is denoted with a single Vertical bar ( | ) symbol.

Let us take an example to understand the difference between these operators.

Take two variable A and B and their values are A = 20 and B = 10.

Logical OR ( || ) Operator:

A || B ;

20 || 10 ;

The result is 1

The output of the above expression is one(1), Because both of the inputs ( 10, 20) are non-zero. So the output of the logical OR operation is One(1).

Bitwise OR ( | ) Operator:

A | B ;

20 | 10 ;

But unlike Logical OR, The Bitwise OR operates on Bit-Level, So we need to look at the binary representations of A and B. Then apply the Bitwise OR operation.

Here A and B are integer data. The size of the integer is 4 bytes i.e 32 bits (Let’s take full 32 bits for this example).

We need to apply the Bitwise OR on these 32 bits sequences of A and B ( because bitwise OR Operates on bits).

A ( 20 ) Binary Representation is  0000 0000 0001 0100
B ( 10 ) Binary Representation is   0000 0000 0000 1010

The bitwise OR of the above Example A | B that is 20 | 10

➡️ Bitwise A|B is:

0000 0000 0001  0100 ( A = 20 )
   &  0000 0000 0000 1010    ( B = 10 )
       ————————
        0000 0000 0001 1110   ( A|B = 30 )
       ————————

The result of A|B is 30

From the above example, It is clear that The Bitwise OR Operator works on the Bit-Level. While the Logical OR works on the Byte-Level.

Program: Bitwise OR Vs Logical OR operator:

The following program is going to demonstrate the differences between the Logical and bitwise OR operators.

Please go through the comments of the program for more details.

  • Use Double Vertical Bar for Logical OR Operation – A || B
  • Use single Vertical Bar for Bitwise OR Operation – A | B

Program output:

Set Nth Bit using the Bitwise OR operator

One of the uses of the bitwise OR operator is, It can be used to set the bit at the specified position.

Setting the Nth bit means, We are going to make the Nth bit as One(1).

Set Nth bit Logic :

The Setting the Nth bit means, If the Nth bit is 0, Then by using the set bit logic we can convert the Nth bit value from Zero(0) to One(1).

If the Nth bit already has the value One(1), Then Set bit logic won’t have any effect on the bit.

To Set the Nth bit, Use the following bitwise OR logic in C Language.

Here,
The InputNumber is the number where we are setting the bit
The bitPosition is the position of the bit (Nth bit) in the binary representation of InputNumber

Set Nth Bit Example:

Let’s take a Number, InputNumber = 431. The binary equivalent of 431 is 110101111.

Now, let’s say you want to set the bit at the position ‘4’ of above value 431.

📢 Here position will start from the end i.e from the Least significant bit (LSB). And position value starts with Zero(0) and up to the length of the Integer. So here we are trying to set the bit at the position 3, So it will be the 4th bit from the end.

To Set the 4th position bit of number 431, Use the below clear bit logic.

After this operation, The 4th position of the InputNumber(431) will be converted into 1. And the InputNumber becomes 447.

The binary sequence of 447 is 110111111

If you observe closely, The 4th position bit ( i.e 5th bit from the end) is Set or Modified to 1.

Let’s convert this into a C program.

Set Nth bit Program in C Language:

We are going to use the above set bit logic. The program is going to take the inputNumber from the user and The program also asks the user to provide the bitPosition to Set.

Once we got the user input, We will apply the above Set bit logic to Set the bit at the specified position.

Program Output:

As you can see from the above output, We got the desired output of 447.

The program set the bit at the 4th index from the end to 1. Which increased the value of inputNumber from 431 to 447.

Re-Run the above program with other examples and see the results.

Here is the output of the program for different input values.

Set-Bit-using-bitwise-OR-Operator-in-C

Few Quick Tips on Bitwise OR Operator:

Bitwise OR operator with Zero :

If you apply the bitwise OR on a Number(X) with the Zero, Then the result will the Number(X).

Bitwise OR with Zero Example program:

Output:

As you can see the Applying the bitwise OR on the Zero(0) and 44, resulted as 44.

Bitwise OR Operator with Negative One (-1) :

If we apply the Bitwise OR on any value and the -1, Then the result will be always -1

This is due to the fact that Negative One (-1) is represented as the all Ones in Binary format. So applying the bitwise OR with all 1’s will results in all 1’s. Which is Negative One(-1).

Negative One (-1) in Binary – 1111 1111 1111 1111    

Let’s look at a example program.

Bitwise OR with Negative One(-1):

Output:

Explanation:

The Bitwise OR of values -1 and 431 is  -1, Because the binary Equivalent of -1 is all ones. Please look at the following explanation.

➡️ Bitwise OR ( -1 | 431 )

-1      =    1111 1111 1111 1111        (Binary Equivalent)

431    =   0000 0001 1010 1111      (Binary Equivalent)

Result   =    1111 1111 1111 1111   ( -1 | 431 =  -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

Conclusion:

In this article, We have discussed the Bitwise OR operator and the Truth table of Bitwise OR. How the bitwise OR operator works in C language and also looked at a few examples and programs.

C Tutorials Home:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. October 22, 2017

    […] Bit-wise OR ( | ) […]

  2. October 23, 2017

    […] Bit-wise OR ( | ) […]

  3. July 25, 2021

    […] 📢 Bitwise OR ( | ) Operator in C programming Language – SillyCodes […]

Leave a Reply