Page Contents

## Bitwise Operators In C

As you may know, the CPU of a computer consists of an arithmetic logical unit that performs mathematical operations such as addition, subtraction, multiplication, and division at the bit level. If we want to do the same thing in C language, then we use Bitwise Operators. Bitwise Operators perform mathematical operations at bit level.

Bitwise Operators are used to perform operations in the bit level between operands. Bitwise Operators first convert the operands to Bit and then perform the operation.

We have different types of Bitwise Operators in C Language. The list of which is the following.

## List of Bitwise Operators in C Language

Operators | Meaning of operators |

& | Bitwise AND |

| | Bitwise OR |

^ | Bitwise XOR |

~ | Bitwise complement |

<< | Shift left |

>> | Shift right |

Let us now see a TRUTH TABLE of Bitwise operators.

X | Y | X&Y | X|Y | X^Y |

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 1 |

Let’s now know all Bitwise Operators one by one.

## Bitwise AND operator

It is one of the most commonly used logical bitwise operators. It is represented by the (&) symbol. There are two integer expressions on each side of the (&) operator.

The Bitwise AND operator returns 1 in the result if both bits have a value of 1 or if both bits does not have a value of 1, then it always returns 0 in the result.

Suppose we have the following two values in binary form

```
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
________
00001000 = 8 (In decimal)
```

As we can see from the above result, the bits of both operands are compared one by one and if the value of both bits is 1, the output is 0 if not 1.

Let us understand Bitwise AND (&) Operator better through a program.

Example Program -: Bitwise AND (&)

```
#include <stdio.h>
int main ()
{
int a = 12, b = 25;
printf ("Output = %d", a&b);
return 0;
}
```

In the above program, we have created two variables named ‘a’ and ‘b’, and stored 12 and 25 in these ‘a’ and ‘b’ variables. The binary values of ‘a’ and ‘b’ are 00001100 and 00011001. When we apply the AND

operator between these two variables,then its result is something like this:

a AND b = 00001100 & 00011001 = 00001000 Its value in decimal is 8.

Output -:

`Output = 8`

## Bitwise OR operator

The Bitwise OR operator is represented by the (|) symbol. The Bitwise OR operator returns 1 in the result if either of their corresponding operands is 1 in the operand and if it does not, then it returns 0 in the result.

For example,

```
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)
```

As we can see from the result above that the bits of both operands are compared one by one and if the value of a single bit is 1,then the output is 1 otherwise 0.

Let us try to create a program to understand Bitwise AND (&) Operator better.

```
#include <stdio.h>
int main ()
{
int a = 12, b = 25;
printf ("Result Is: %d", a|b);
return 0;
}
```

Output -:

`Result Is: 29`

## Bitwise XOR (exclusive OR) operator

The Bitwise XOR operator is represented by the (^) symbol. The Bitwise XOR operator returns 1 in the result if its respective operands are different and if it does not, then it returns 0 in the result.

We can also say that if one of the corresponding bits is 1 and the other bit is 0, then Bitwise XOR Operator will always return only 1 in the result.

Let us understand this through an example.

```
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101 = 21 (In decimal)
```

Example Program -: Bitwise XOR (^)

```
#include <stdio.h>
int main ()
{
int a = 12, b = 25;
printf ("Result Is: %d", a^b);
return 0;
}
```

Output -:

`Result Is: 21`

## Bitwise complement operator ~

The bitwise complement operator is also known as the complement operator. It is represented by the tilde (~) symbol. It only takes one operand or variable to perform an operation and performs a complement operation on one operand.

When we apply complement operation, on any bit, it becomes 0, 1 and becomes 1, 0

For example,

```
Suppose we have a variable named 'a' whose value is
a = 8;
This result is obtained by converting the value 8 in the variable a to binary.
a = 1000
When we apply bitwise complement operator in the operand, the output will be:
Result = 0111
```

As we can see from the above result that if bit is 1, then it is 0 and if bit is 0, it changes it to 1.

Let’s now understand the bitwise complement operator through a program.

```
#include <stdio.h>
int main ()
{
int a = 8; // variable declarations
printf ("The output of the Bitwise complement operator ~a is %d", ~a);
return 0;
}
```

Output -:

`The output of the Bitwise complement operator ~ a is -9`

## Bitwise Shift Operators in C Language

There are two types of bitwise shift operators in C language.

- Left-shift operator
- Right-shift operator

### #1. Left-shift operator

The left-shift operator shifts the number of bits to the left. The left-shift operator is represented by << symbol.

**Syntax of the left-shift operator**

`Operand<<n`

Here the operand can be any integer variable and n is the number of bits to shift.

In the case of the left-shift operator, the bits are left-side shifts. In the list-shift operator, the ‘n’ bits of the left side will be removed, and the ‘n’ bits of the right side will be filled with 0.

**For example,**

```
Suppose we have this statement
int a = 5;
By representing the value of a in binary,
a = 00000101
If we want to left-shift the above representation by 2, then the statement would be:
a<<2;
Result: 00000101<<2 = 00010100
```

Let us understand this through a program.

```
#include <stdio.h>
int main ()
{
int a = 5; // variable initialization
printf ("The value of a<<2 is: %d", a<<2);
return 0;
}
```

After performing left shift operation, the value will be 20.

Output -:

`The value of a<<2 is: 20`

### #2. Right-shift operator

The right-shift operator shifts the number of bits to the right. The right-shift operator is represented by the >> symbol.

Syntax of the Right-shift operator

`Operand>>n`

Here the operand can be any integer variable on which right-shift operation will be applied and n is the number of shifting bits.

In the case of the right-shift operator, the bits are right-side shift, in the right-shift operator the ‘n’ bits of the right side will be removed, and the ‘n’ bits of the left side will be filled with 0.

For example,

```
Suppose we have this statement
int a = 7;
By representing the value of a in binary,
a = 00000111
If we want to right-shift the above representation by 2, then the statement would be:
a>>2;
Result: 00000101>>2 = 0000 0001
```

Let us understand this through a program.

```
#include <stdio.h>
int main ()
{
int a = 7; // variable initialization
printf ("The value of a>>2 is: %d", a>>2);
return 0;
}
```

The value will be 1 after performing the right-shift operation.

Output -:

`The value of a>>2 is : 1`

**Read More -:**

- What is Operators In C
- Relational Operators In C
- Logical Operators In C
- Arithmetic Operators In C
- Assignment Operators In C
- Conditional Operator in C

## Conclusion

Friends, I hope you have found the answer of your question and you will not have to search about the bitwise operators in C Language

However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.

If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the bitwise operator in C Language

To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.