Bitwise operators
These operators are used to perform operations on individual bits of a variable rather than the entire value. They are often used in low-level programming, device driver programming, and system programming. In this post, we will discuss the six types of bitwise operators in C language and how they operate on the bit level.
1. One's complement (~)
The one's complement operator is a unary operator that inverts all the bits of the operand. If the operand has a value of 0, the result will be -1, and if the operand has a value of -1, the result will be 0. For example, if we take an integer variable 'a' with a value of 5, the one's complement of 'a' will be -6.
2. Right shift (>>)
The right shift operator is a binary operator that shifts the bits of the left operand to the right by the number of positions specified by the right operand. For example, if we take an integer variable 'a' with a value of 5, and perform a right shift operation by 1 bit, the result will be 2.
3. Left shift (<<)
The left shift operator is a binary operator that shifts the bits of the left operand to the left by the number of positions specified by the right operand. For example, if we take an integer variable 'a' with a value of 5, and perform a left shift operation by 3 bits, the result will be 40.
4. Bitwise AND (&)
The bitwise AND operator is a binary operator that performs an AND operation between the bits of the left and right operands. If both bits are 1, the resulting bit will be 1, otherwise, it will be 0. For example, if we take an integer variable 'a' with a value of 5, and perform a bitwise AND operation with 2, the result will be 0.
5. Bitwise OR (|)
The bitwise OR operator is a binary operator that performs an OR operation between the bits of the left and right operands. If either bit is 1, the resulting bit will be 1, otherwise, it will be 0. For example, if we take an integer variable 'a' with a value of 5, and perform a bitwise OR operation with 5, the result will be 5.
6. Bitwise XOR (^)
The bitwise XOR operator is a binary operator that performs an exclusive OR operation between the bits of the left and right operands. If both bits are the same, the resulting bit will be 0, otherwise, it will be 1. For example, if we take an integer variable 'a' with a value of 5, and perform a bitwise XOR operation with 2, the result will be 7.
note:-
Important instruction: These operators can operate upon int and char but will not operate on float and double.
Now, let's see an example of how to use these operators in C language.
copy
#include <stdio.h>
// This function prints a number in bits
void showbits(char n)
{
int i, k, andmask;
for (i = (sizeof(char) * 8) - 1; i >= 0; i--)
{
andmask = 1 << i;
k = n & andmask;
k == 0 ? printf("0") : printf("1");
}
printf("\n");
}
int main()
{
int a = 5;
// Print the binary representation of a
printf("The binary representation of %d is: ", a);
showbits(a);
// Print the binary representation of the one's complement of a
printf("The one's complement of %d is: ", a);
showbits(~a);
// Print the binary representation of a shifted one bit to the right
printf("Shifting %d one bit to the right gives: ", a);
showbits(a >> 1);
// Print the binary representation of a shifted three bits to the left
printf("Shifting %d three bits to the left gives: ", a);
showbits(a << 3);
// Print the binary representation of the bitwise AND of a and 2
printf("The bitwise AND of %d and 2 is: ", a);
showbits(a & 2);
// Print the binary representation of the bitwise OR of a and 5
printf("The bitwise OR of %d and 5 is: ", a);
showbits(a | 5);
// Print the binary representation of the bitwise XOR of a and 2
printf("The bitwise XOR of %d and 2 is: ", a);
showbits(a ^ 2);
return 0;
}
we provide a code example that uses the showbits() function to print the binary representation of various numbers. The showbits() function takes a char argument and iterates through its bits to print a binary representation of the number. We then use this function to print the binary representation of different values after applying the bitwise operators.
output:
The binary representation of 5 is: 00000101 The one's complement of 5 is: 11111010 Shifting 5 one bit to the right gives: 00000010 Shifting 5 three bits to the left gives: 00101000 The bitwise AND of 5 and 2 is: 00000000 The bitwise OR of 5 and 5 is: 00000101 The bitwise XOR of 5 and 2 is: 00000111