# Bitwise Operators in C

C supports a set of bitwise operator as listed in table . C supports six bit operators . these operators can operate only on an integer operand such as int, char, short, long int, and so on.

Let’s look at the truth table of the bitwise operators.

# Bitwise AND operator

Bitwise AND operator is denoted by the single ampersand sign (&). Two integer operands are written on both sides of the (&) operator. If the corresponding bits of both the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the output would be 0.

# Bitwise OR operator

The bitwise OR operator is represented by a single vertical sign (|). Two integer operands are written on both sides of the (|) symbol. If the bit value of any of the operand is 1, then the output would be 1, otherwise 0.

# Bitwise exclusive OR operator

Bitwise exclusive OR operator is denoted by (^) symbol. Two operands are written on both sides of the exclusive OR operator. If the corresponding bit of any of the operand is 1 then the output would be 1, otherwise 0.

# Bitwise complement operator

The bitwise complement operator is also known as one’s complement operator. It is represented by the symbol tilde (~). It takes only one operand or variable and performs complement operation on an operand. When we apply the complement operation on any bits, then 0 becomes 1 and 1 becomes 0.

# Bitwise shift operators

Two types of bitwise shift operators exist in C programming. The bitwise shift operators will shift the bits either on the left-side or right-side. Therefore, we can say that the bitwise shift operator is divided into two categories:

• Left-shift operator
• Right-shift operator

Left-shift operator

It is an operator that shifts the number of bits to the left-side.

1. Operand << n

Where,

Operand is an integer expression on which we apply the left-shift operation.

n is the number of bits to be shifted.

In the case of Left-shift operator, ’n’ bits will be shifted on the left-side. The ’n’ bits on the left side will be popped out, and ’n’ bits on the right-side are filled with 0.

Right-shift operator

It is an operator that shifts the number of bits to the right side.

Syntax of the right-shift operator is given below:

1. Operand >> n;

Where,

Operand is an integer expression on which we apply the right-shift operation.

N is the number of bits to be shifted.

In the case of the right-shift operator, ’n’ bits will be shifted on the right-side. The ’n’ bits on the right-side will be popped out, and ’n’ bits on the left-side are filled with 0.

Example:

`#include <stdio.h>main() {   unsigned int a = 60;	/* 60 = 0011 1100 */     unsigned int b = 13;	/* 13 = 0000 1101 */   int c = 0;              c = a & b;       /* 12 = 0000 1100 */    printf("Line 1 - Value of c is %d\n", c );   c = a | b;       /* 61 = 0011 1101 */   printf("Line 2 - Value of c is %d\n", c );   c = a ^ b;       /* 49 = 0011 0001 */   printf("Line 3 - Value of c is %d\n", c );   c = ~a;          /*-61 = 1100 0011 */   printf("Line 4 - Value of c is %d\n", c );   c = a << 2;     /* 240 = 1111 0000 */   printf("Line 5 - Value of c is %d\n", c );   c = a >> 2;     /* 15 = 0000 1111 */   printf("Line 6 - Value of c is %d\n", c );}`

OUTPUT:

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

--

--