Logical Operator

Till now we were using bytes of any data but C provides the facility to do the operation with an individual bit of a byte. This we can do using the bitwise operator. There are 6 bitwise operators and those are listed below. These operators can work with int, char data types.

Operator symbol Operator name
& Bitwise and
| Bitwise or
^ Bitwise xor
~ Bitwise complement
<< Bitwise shift left
>> Bitwise shift right

Bitwise and (&)

This operator first converts variables in binary then does and operation and at last, it converts the result to its previous data type.
Let us see what happens when we do bitwise and operation with any two bits.

<1st variable> & <2nd variable>;

From the below table, we can say that when any of the bit is 0 then output is 0.

a b a&b
0 0 0
0 1 0
1 0 0
1 1 1

 Now let us take an example of multiple bits.

a = 10, b=6;

We want the result of a&b so convert a, b in binary.

a(binary) = 1010 b(binary) = 0110

For a&b, each individual bit of both variables will be added to each other individually.  So the result of a&b is 2 (binary 0010)

( 1 0 1 0 )  & (  0 1 1 0 )  = ( 0 0 1 0 )

 In the below program there are three int variables: a,b,c. a is having value 4 and b is having value 5 and c is a&b. Now a&b = 4(binary 100) & 5(binary 101) = 4(binary 100). Which is printed.

#include<stdio.h>
#include<conio.h>
void main()
{
  int a=4,b=5;
  int c;
  clrscr();
  c = a&b;
  printf("c = %d ",c);
  getch();
}

Output:

Bitwise or (|)

 Same as and operator, this operator first converts variables in binary then does or operation and at last, it converts the result to its previous data type. Let us see what happens when we do bitwise or operation with any two bits.

<1st variable> | <2nd variable>;

a b a&b
0 0 0
0 1 1
1 0 1
1 1 1

From the below table, we can say that when any of the bit is 1 then output is 1.

Now let us take an example of multiple bits.

a = 10, b=6;

We want result of a|b so convert a, b in binary.

a(binary) = 1010 b(binary) = 0110

For a|b, each individual bit of both variables will be ored to each other individually. So the result of a|b is 14 (binary 1110)
( 1 0 1 0 )  | (  0 1 1 0 )  = ( 1 1 1 0 )

In the below program there are three int variables: a,b,c. a is having value 4 and b is having value 5 and c is a&b. Now a|b = 4(binary 100) | 5(binary 101) = 5(binary 101). Which is printed.

#include<stdio.h>
#include<conio.h>
void main()
{
  int a=4,b=5;
  int c;
  clrscr();
  c = a|b;
  printf("c = %d ",c);
  getch();
}

Bitwise xor (^)

This operator first converts variables in binary then does xor operation and at last, it converts the result to its previous data type. Let us see what happens when we do bitwise xor operation with any two bits.

Now let us take an example of multiple bits.

<1st variable> ^ <2nd variable>

From the below table, we can say that when Both inputs are different then output is 1, and when both inputs are same then output is 0.

a b a&b
0 0 0
0 1 1
1 0 1
1 1 0

 a = 10, b=6;

We want the result of a^b so convert a, b into binary.

a(binary) = 1010 b(binary) = 0110

For a^b, each individual bit of both variables will be xored to each other individually. So the result of a^b is 12 (binary 1100)

( 1 0 1 0 )  & (  0 1 1 0 )  = ( 1 1 0 0 )

In the below program there are three int variables: a,b,c. a is having value 4 and b is having value 5 and c is a&b. Now a^b = 4(binary 100) ^ 5(binary 101) = 1(binary 001). Which is printed.

#include<stdio.h>
#include<conio.h>
void main()
{
  int a=4,b=5;
  int c;
  clrscr();
  c = a^b;
  printf("c = %d ",c);
  getch();
}

Output:

Learning from this blog:

  1. Need of bitwise operators.
  2. Types of bitwise operators.
  3. Difference between bitwise and, or and xor operators.
  4. Characteristic of bitwise and, or, xor.
  5. How to write the program bitwise and, or, xor.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments