We have different types of bitwise operators in the C programming language . The following is the list of the bitwise operators:
| Operator Symbol | Operator Name | |
|---|---|---|
& |
Bitwise AND operator | |
|
Bitwise OR operator | |
^ |
Bitwise exclusive OR operator | |
~ |
One's complement operator (unary operator) | |
<< |
Left shift operator | |
>> |
Right shift operator |
Here, we will discuss all the operators one by one.
Bitwise AND Operator (&) in C
In the C programming language, the bitwise AND operator is denoted by the single ampersand sign (&). Two integer operands are written on both sides of the (&) operator, and then it compares each bit of the given integers. If the corresponding bits of both operands are 1, the output would be 1; otherwise, the output would be 0.
Truth Table of Bitwise AND Operator
The Bitwise AND Operator performs their operations as per the following truth table in C.
| X | Y | X&Y |
|---|---|---|
0 |
0 | 0 |
0 |
1 | 0 |
1 |
0 | 0 |
1 |
1 | 1 |
For Example:
We have taken two variables, a and b, and initialized them with values 6 and 14. The binary representation of the given variables is given below:
a (6) = 0110 (Binary)
b (14) = 1110 (Binary)
0110
& 1110
--------
0110
When we apply the bitwise AND operation in the above two variables, i.e., a&b, the output would be 0110.
Bitwise AND Operator Example
Let us take an example to illustrate the Bitwise AND operator in C.
Example
#include <stdio.h>
int main() //main function
{
int a=6, b=14; // variable declarations
printf("The output of the Bitwise AND operator a&b is %d",a&b);
return 0;
}
Output:
The output of the Bitwise AND operator a&b is 6
Explanation:
In this example, we have created two variables, i.e., a and b. The values of a and b are 6 and 14. The binary values of a and b are 0110 and 1110. After that, we apply the AND operator between these two variables, which gives the output of a & b as 6 (0110).
Bitwise OR operator (|) in C
In the C programming language, the bitwise OR operator is represented by a single vertical sign (|). Two integer operands are written on both sides of the (|) symbol, and then it compares each bit of the given integers. If the bit value of any of the operands is 1, the output would be 1; otherwise, it will return 0.
Truth Table of Bitwise OR Operator
The Bitwise OR Operator performs their operations as per the following truth table in C.
| X | Y | X | Y |
|---|---|---|---|
0 |
0 | 0 | |
0 |
1 | 1 | |
1 |
0 | 1 | |
1 |
1 | 1 |
For Example:
We have taken two variables , a and b, and initialized them with values 23 and 10. The binary representation of the given variables is given below:
a (23) = 0001 0111
b (10) = 0000 1010
0001 0111
| 0000 1010
--------------
0001 1111
When we apply the bitwise OR operator in the above two variables, i.e., a|b, the output would be 0001 1111.
Bitwise OR Operator (|) Example in C
Let us take an example to illustrate the Bitwise OR Operator in C.
Example
#include <stdio.h>
int main() //main function
{
int a=23, b=10; // variable declarations
printf("The output of the Bitwise OR operator a|b is %d",a|b);
return 0;
}
Output:
The output of the Bitwise OR operator a|b is 31
Explanation:
In this example, we have taken two variables, a and b, and initialized them with values 23 and 10. The binary values of a and b are 0001 0111 and 0000 1010. After that, we apply the AND operator between these two variables, which gives the output of a & b as 31 (0001 1111).
Bitwise Exclusive OR Operator (^) in C
In the C programming language, the Bitwise Exclusive OR operator is denoted by the (^) symbol. Two operands are written on both sides of the exclusive OR operator, and then it compares each bit of the given integers. If the corresponding bit of any of the operands is 1, the output would be 1; otherwise, it would return 0.
Truth Table of Bitwise Exclusive OR Operator
The Bitwise Exclusive OR Operator performs their operations as per the following truth table in C.
| X | Y | X^Y |
|---|---|---|
0 |
0 | 0 |
0 |
1 | 1 |
1 |
0 | 1 |
1 |
1 | 0 |
For Example:
We have taken two variables, a and b, and initialized them with values 12 and 10. The binary representation of the given variables is given below:
a (12) = 0000 1100
b (10) = 0000 1010
0000 1100
^ 0000 1010
---------------
0000 0110
When we apply the bitwise exclusive OR operator in the above two variables (a^b), the result would be 0000 0110.
Bitwise Exclusive OR Operator Example in C
Let us take an example to illustrate the Bitwise Exclusive OR Operator in C.
Example
#include <stdio.h>
int main() //main function
{
int a=12, b=10; // variable declarations
printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b);
return 0;
}
Output:
The output of the Bitwise exclusive OR operator a^b is 6
Explanation:
In this example, we have taken two variables , a and b, and initialized them to 12 and 10. After that, we use the Binary Exclusive OR operator between the given operands, and then it prints the output using the printf function.
Bitwise Complement Operator (~) in C
The bitwise complement operator is also known as the one's complement operator. It is represented by the symbol tilde (~). It takes only one operand or variable and performs a complement operation on an operand. When we apply the complement operation on any bits, 0 becomes 1 and 1 becomes 0.
Truth Table of Bitwise Complement Operator
The Bitwise Complement Operator performs their operations as per the following truth table in C.
| x | ~x |
|---|---|
0 |
1 |
1 |
0 |
For Example:
We have taken a variable a, and then initialized it with the value 8. The binary representation of the given variable a is 0000 1000. If we operate using the bitwise complement operator, it looks like this:
~ 0000 1000
---------------
1111 0111
When we apply the bitwise complement operator to the operand, the output would be 1111 0111.
Bitwise Complement Operator Example in C
Let us take an example to illustrate the Bitwise Complement Operator in C.
Example
#include <stdio.h>
int main() //main function
{
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
Explanation:
In this example, we demonstrate the bitwise complement (~) operator. Here, we have taken a variable, which has a value of 8. After that, we use the ~ (tilde) operator that inverts all bits of the number, which converts 8 (0000 1000 binary) into 1111 0111 that represents -9 in 2's complement . Finally, it prints the value using the printf function.
Bitwise Shift Operators (>> <<) in C
In the C programming language, the bitwise shift operators are used to shift the bits either to 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
Now, we will discuss these shift operators one by one.
Left-shift operator (<<)
The left-shift operator is an operator that shifts the number of bits to the left-side.
Syntax:
It has the following syntax:
Operand << n
In this syntax,
- 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.
For Example:
Suppose we have a statement:
int a = 5;
The binary representation of 'a' is given below:
a = 0101
If we want to left-shift the above representation by 2, the statement would be:
a << 2;
0101<<2 = 00010100
Left-shift Operator Example in C
Let us take an example to illustrate the left-shift operator in C.
Example
#include <stdio.h>
int main() //main function
{
int a=5; // variable initialization
printf("The value of a<<2 is : %d ", a<<2);
return 0;
}
Output:
The value of a<<2 is : 20
Explanation:
In this example, we demonstrate the left-shift operator in C. First, we have taken a variable (a) and initialized it with the value 5. After that, we use the left shift operator that shifts the bits of the integer a two positions to the left. The binary number of 5 (0101) becomes (10100), which represents 20 in decimal. Therefore, the a<<2 prints the result 20.
Right-shift Operator (>>)
In the C programming language, the right-shift operator (>>) is an operator that shifts the bits to the right side. Each shift divides the number by 2.
Syntax:
It has the following syntax:
Operand >> n;
In this syntax,
- 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.
For Example:
Suppose we have a statement:
int a = 7;
The binary representation of the above variable would be:
a = 0111
If we want to right-shift the above representation by 2, the statement would be:
a>>2;
0000 0111 >> 2 = 0000 0001
Right-shift Operator Example in C
Let us take an example to illustrate the right-shift operator in C.
Example
#include <stdio.h>
int main() //main function
{
int a=7; // variable initialization
printf("The value of a>>2 is : %d ", a>>2);
return 0;
}
Output:
The value of a>>2 is : 1
Explanation:
In this example, we will demonstrate the right-shift operator in C. First, we have taken a variable and initialized that with the value 7. After that, we use the right-shift (>>) operator that shifts the bits of the integer a two positions to the right. The binary of 7 (0111) becomes 1 (0001). Therefore, the a>>2 prints the result 1.