Bitwise Operators in C

Bitwise Operator

Bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.

Bitwise operator are used in Low-level programming for applications such as device drivers, cryptographic software, video decoding software, memory allocators, compression software and graphics.

There are five types of Bitwise operator.

  • AND BITWISE
  • OR BITWISE
  • EXCLUSIVE OR
  • LEFT SHIFT
  • RIGHT SHIFT
Bitwise operator

Implementation of Bitwise Operator

AND BITWISE

Result is true only if both operands are true. It can be used to set up a mask to check the values of certain bits.The symbol of this operator is &.

OR BITWISE

Result is true if any of the operands is true.The symbol of this operator is |.

EXCLUSIVE OR

It is based on AND or OR Operator.It also helps to swap two variables without using a third one. The symbol of this operator is ^.

LEFT SHIFT

Shifts first operand a number of bits to the left as specified in the second operand, shifting in zeroes from the right.The symbol of this operator <<.

RIGHT SHIFT

Shifts first operand a number of bits to the right as specified in the second operand, and discards displaced bits and the symbol of this operator is >>.

Code for Bitwise Operator

#include <stdio.h>

int main()
{
int a,b,x,y,z,p,q;
a=15;
b=10;
x=a&b;
y=a|b;
z=a^b;
p=a<<4;
q=a>>3;

printf("%d",x);
printf("\n%d",y);
printf("\n%d",z);
printf("\n%d",p);
printf("\n%d",q);

return 0;
}
Output
10

15

5

240

1