Preview

# Bitwise operators in C

Bitwise operators are used to manipulate individual bits of a numeric value. There are six bitwise operators provided by C language.

• Bitwise AND(&)
• Bitwise OR (|)
• Bitwise XOR(^)
• Bitwise complement(~)
• Bitwise Left shift(,)
• Bitwise Right shift(>>)

**Bitwise form of a number is obtained by converting the number into binary number.

## 1. Bitwise AND

Bitwise AND operator is represented as single ampersand sign (&).

If any of the input to this operator is 0, output would be 0.

If all the inputs of this operator are 1, output would be 1.

Values are converted into bitwise form and then their individual bits are applied bitwise And operation. Depending upon the bit values, a new value will be generated.

000
010
100
111

### Output

#include<stdio.h>
#include<conio.h>
int main()
{
int a=10,b=7,c;
clrscr();
c=a&b;
printf(“\nc=%d”,c);
getch();
return(0);
}
c=2

### Description

In the above program, variable a contains 10 and b contains 7, both these values have to be converted into bits. After conversion following bit values will be formed and bitwise AND will be applied as follows:
a=10       will be represented in bitwise form as  0000000000001010
b=7
will be represented in bitwise form as  0000000000000111

a&b        will generate                                         0000000000000010

After converting 0000000000000010 into decimal form, we get 2, so value of a&b would be 2.

## 2. Bitwise OR (|)

Bitwise OR operator is represented as single pipe sign (|).

If any of the input to this operator is 1, Output would be 1.

If all the inputs of this operator are 0, Output would be 0.

Values are converted into bitwise form and then their individual bits are applied bitwise OR operation. Depending upon the bit values, a new value will be generated.

000
011
101
111

### Output

#include<stdio.h>
#include<conio.h>
int main()
{
int a=10,b=7,c;
c=a|b;
printf(“\nc=%d”,c);
getch();
return(0);
}
c=15

### Description

In the above program, variable a contains 10 and b contains 7, both these values have to be converted into bits. After conversion following bit values will be formed and bitwise OR will be applied as follows:
a=10       will be represented in bitwise form as   0000000000001010
b=7         will be represented in bitwise form as   0000000000000111
a|b           will generate                                                0000000000001111
After converting 0000000000001111 into decimal form, we get 15, so value of a|b would be 15.

## 3. Bitwise XOR (^)

Bitwise XOR operator is represented as caret sign (^).

If odd number of inputs to this operator is 1, Output would be 1.

Values are converted into bitwise form and then their individual bits are applied bitwise XOR operation. Depending upon the bitwise values, a new value will be generated.

000
011
101
110

### Output

#include<stdio.h>
#include<conio.h>
int main()
{
int a=10,b=7,c;
c=a^b;
printf(“\nc=%d”,c);
getch();
return(0);
}
c=13

### Description

In the above program, variable a contains 10 and b contains 7, both these values have to be converted into bits. XOR will be applied as follows:

a=10       will be represented in bitwise form as  0000000000001010
b=7        will be represented in bitwise form as  0000000000000111
a^b         will generate                                              0000000000001101
After converting 0000000000001101 into decimal form, we get 13, so value of a^b would be 13.

## 4. Bitwise complement (~)

Bitwise complement operator is also known as bitwise NOT operator. It is represented as tild sign (~). It is used to reverse the input.

If input of this operator is 1, Output would be 0.

If input of this operator is 0, Output would be 1.

Value is converted into bitwise form and then individual bits are applied bitwise complement operation.

After applying this operation, all individual bits are reversed i.e. all 0’s become 1’s and all 1’s become 0’s.

Depending upon the bitwise values, a new value will be generated.

### Truth table for Bitwise Complement operator is

 Input Output 0 1 1 0

### Output

#include<stdio.h>
#include<conio.h>
int main()
{
unsigned int a=65535,b;
clrscr();
b=~a;
printf(“\nb=%u”,b);
getch();
return(0);
}
b=0

### Description

In the above program, variable a contains 65535.

a=65535   will be represented in bitwise form as  1111111111111111

~a    will generate 0000000000000000

After converting 0000000000000000 into decimal form, we get 0, so value of b would be 0.

## 5. Bitwise Left Shift(<<)

Bitwise Left Shift operator is represented as double less than sign(<<).

It is used to shift individual bits of a numeric value towards left by specified number of digits.

### Output

#include<stdio.h>
#include<conio.h>
int main()
{
int a=10,b;
b=(a<<1);
printf(“\nb=%d”,b);
getch();
return(0);
}
b=20

### Description

In the above program, variable a contains 10. Bitwise left shift operator will shift position of each bit towards left by one place as follows:

a=10 is represented in bitwise form as 0000000000001010

a<<1  will generate 0000000000010100

After converting 0000000000010100 into decimal form, we get 20, so value of b would be 20.

## 6. Bitwise Right Shift (>>)

Bitwise Right Shift operator is represented as double greater than sign(>>). It is used to shift individual bits of a numeric value towards right by specified number of digits.

### Output

#include<stdio.h>
#include<conio.h>
int main()
{
int a=10,b;
b=(a>>1);
printf(“\nb=%d”,b);
getch();
return(0);
}
b=5

### Description

In the above program, variable a contains 10. Bitwise Right shift operator will shift position of each bit towards right by one place.

a=10 will be represented in bitwise form as  0000000000001010

a>>1   will generate  0000000000000101

After converting 0000000000000101 into decimal form, we get 5, so value of b would be 5.

Lesson tags: bitwise and in c, bitwise complement in c, bitwise left shift in c, bitwise operators examples, bitwise operators of c, bitwise or in c, bitwise programs, bitwise right shit in c, bitwise xor in c
Back to: C Programming Language