Bitwise Operator in C

Bitwise Operator


Bitwise operators are used to perform operation on individual bits of a number. It is used only for integer type of values. It can not perform operation on other values except integer such as float,duble etc.

Let we discuss about bitwise operator in C.There are following bitwise operator used in C such as:

  1. Bitwise AND Operator (&)
  2. Bitwise OR Operator ( | )
  3. Bitwise NOT Operator ( ~ )
  4. Bitwise XOR Operator ( ^ )
  5. Bitwise Left Shift Operator ( << )
  6. Bitwise Right Shift Operator ( >> )

 Bitwise AND Operator (&)


It is a binary operator. It has two operands. It perform bitwise AND on two operands. It return true if both are true or we can say that return 1 if both bit are 1.

Let we understand the truth table of bitwise AND.


A B Bitwise AND (A & B)
0 0 0
0 1 0
1 0 0
1 1 1

Copy Code

Example : Write a program to perform Bitwise AND (&) operation.


#include<stdio.h>
#include<conio.h>

void main()
{
    int a;
    a=1;
    b=2;
    printf("Bitwise AND return =%d",a&∼a);
    getch();    
}


Bitwise OR Operator ( | ) 


It is a binary operator. It has two operands. It perform bitwise OR on two operands. It return true if any of them is true or both of them are true. we can say that return 1 if any of  bit  is 1 or both bit are 1.

Let we understand the truth table of Bitwise OR.


A B Bitwise OR (A | B)
0 0 0
0 1 1
1 0 1
1 1 1

Copy Code

Example : Write a program to perform Bitwise OR (|) operation.


#include<stdio.h>
#include<conio.h>

void main()
{
    int a,b;
    a=1;
    b=2;
    printf("Bitwise OR return =%d",a|b);
    getch();    
}


 Bitwise NOT Operator (∼)


It is a unary operator. It has single operands. It perform bitwise NOT on single operand.

Let we understand the truth table of Bitwise NOT.


A Bitwise NOT (∼ A)
0 1
1 0

Copy Code

Example : Write a program to perform Bitwise NOT (∼) operation.


#include<stdio.h>
#include<conio.h>

void main()
{
    int a;
    a=1;
    printf("Bitwise NOT return =%d",a);
    getch();    
}


 Bitwise XOR (Exclusive OR) Operator (^)


It is a binary operator. It has two operands. It perform bitwise XOR on two operands. It is represented by (^). It is same as Bitwise OR but if both A and B are true or we can say if both bit are 1 than it return false or 0 in XOR.

Let we understand the truth table of bitwise XOR.


A B Bitwise XOR (A ^ B)
0 0 0
0 1 1
1 0 1
1 1 0

Copy Code

Example : Write a program to perform Bitwise XOR (^) operation.


#include<stdio.h>
#include<conio.h>

void main()
{
    int a,b;
    a=1;
    b=2;
    printf("Bitwise XOR return =%d",a^b);
    getch();    
}


 Bitwise Left Shift Operator (<<)


It is a binary operator. It has two operands. It perform bitwise Left Shift on two operands. It is represented by (<<). It is equivalent to multiply left operand by 2Right Operand
First Operand >> Second Operand
here First operand shows what bit we shift left and the second operand shows how many number of place to shift the bit.
Let we understand with the help of example.
a << 1
if a=4
Represent 4 in 8-bit binary number we get
0000 0100
Now in (a<<1), here 1 represent how many number of bits are shifted to left.Here it is 1 means we will shift 1 bit towards left such as

Note:

It is equivalent to multiply left operand by  2Right Operand.Let we see how this statement is true. Suppose from above example i.e
(a << 1)
4 << 1 = 2
4 multiply by 21= 8

Examples: Implementation of Left Shift Operator

Copy Text
#include <stdio.h>

int main()
{
   int  a=4;
   printf("4<<1=%d",a<<1);

    return 0;
}

Output: 4<<1 = 8

Copy Text
#include <stdio.h>

int main()
{
   int  a=4;
   printf("4<<1=%d",a<<1);

    return 0;
}

Output: 4<<1= 8

 

Bitwise Right Shift Operator (>>)


It is a binary operator. It has two operands. It perform bitwise Right Shift on two operands. It is represented by (>>).
First Operand >> Second Operand
here First operand shows what bit we shift right and the second operand shows how many number of place to shift the bit.
It is noted when bits are shifted right then leading positions are filled with zeros.
It is equivalent to divide left operand by 2Right Operand

Note:

It is equivalent to divide left operand by  2Right Operand.Let we see how this statement is true. Suppose from above example i.e
(a >> 1)
4 >> 1 = 2
4 divide by 21= 2

Examples: Implementation of Right Shift Operator

Copy Text
#include <stdio.h>

int main()
{
   int  a=4;
   printf("4>>1=%d",a>>1);

    return 0;
}

Output: 4>>1= 2

Copy Text
#include <stdio.h>

int main()
{
   char  a=4;
   printf("4>>1=%d",a>>1);

    return 0;
}

Output: 4>>1= 2


No comments:

Post a Comment