C provides bitwise operators to deal with bits. Each bit is either “0” or “1”. In this article, we are going to discuss how to use bitwise operators in C.
C provides the following bitwise operators.
- & (Ampersand – Bitwise AND Operator)
- | (Pipe Symbol – Bitwise OR Operator)
- ^ (Cap Symbol – Bitwise XOR Operator)
- ~ (Tilde Symbol – Bitwise Complement or Bitwise NOT Operator)
- << (2 ‘<‘ Symbols – Left Shift Operator)
- >> (2 ‘>’ Symbols – Right Shift Operator)
To use these operators, we need two operands; except for the bitwise NOT operator; it needs only one operand, that is why we can call this operator a unary operator.
Let’s discuss them one by one here with different examples for each one.
Bitwise AND Operator (&
)
Compare each bit in two operands and return “1”, when both the bits in two operands are “1”; otherwise it returns “0” for the particular bit position.
Eg: let’s take two values 123 and 87. Compare binary values between these two and apply the bitwise AND operator (“&”). So the result will be 83. See the below calculation:
0111 1011 (123) & 0101 0111 (87) = 0101 0011 (83)
Bitwise OR Operator (|
)
Compares two operands and returns “0” when both the bits in two operands are “0”; otherwise it returns “1” for the particular bit position.
For example, see the below code to understand how we use this operator in ‘C’.
int a = 123, b = 87; c = a | b;
Bitwise Complement Operator (~
)
It toggles each bit in the operand. Only one operand is required to use this operator; whereas other bitwise operators need two operands. This operator is called a unary bitwise operator.
int a = 123; a = ~a;
Bitwise XOR Operator (^
)
This operator compares the bits between two operands and returns only “1” when either of the bit is “1”; otherwise it returns “0” for the particular bit position.
int onebit = 1, anotherbit = 0; if ( onebit ^ anotherbit ) printf("This is XOR.");
Test the above condition by changing the values “0” or “1” in both the variables. And observe that, the condition will be true only when either one’s value is “1”; not for both.
Left Shift Operator (<<
)
It shifts the bits left side in the left side operand based on the value in the right side operand. The same number of bits with the value “0” will be added on the right side.
This example left shifts the bits by 4 positions in the variable “a”.
int a = 123; printf("%d", 123 << 4);
Right Shift Operator (>>
)
This is useful to shift the bits right side in the left side operand based on the value in the right side operand. The same number of bits will be added from the left side. The bit value is based on the sign of the number; if it is a negative sign “1”s will be added; otherwise “0”s will be added.
Below example right shift 4 bits from the variable “a”.
int a = 123; printf("%d", 123 >> 4);
The number of bits will be allocated based on the type of the variable and the Operating System you are using.
// Sample.c #include <stdio.h> void main() { int a = 123, b = 87; printf("Bitwise AND (\"&\") : %d\n", a & b); printf("Bitwise OR (\"|\") : %d\n", a | b); printf("Bitwise XOR (\"^\") : %d\n", a ^ b); printf("Bitwise Complement (\"~\") : %d\n", ~a); printf("Bitwise Left Shift (\"<<\") : %d\n", a << 4); printf("Bitwise Right Shift (\">>\") : %d\n", a >> 4); }
Run the above code and observe the results.
// Malin