There are 6 types bitwise operators in C which will work at bit-level:
& (bitwise AND) This operator will take two numbers as operand and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
| (bitwise OR) This operator will take two numbers as operand and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
^ (bitwise XOR) This operator will take two numbers as operand and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
<< (left shift) This operator will take two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
>> (right shift) This operator will take two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
~ (bitwise NOT) This operator will take one number and inverts all bits of it.
Now look at the example which demonstrate the use of all bitwise operators
/* C Program to demonstrate use of bitwise operators */
#include<stdio.h>
int main()
{
unsigned char a = 5, b = 9; // a = 4(00000101), b = 8(00001001)
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a&b); // The result is 00000001
printf("a|b = %d\n", a|b); // The result is 00001101
printf("a^b = %d\n", a^b); // The result is 00001100
printf("~a = %d\n", a = ~a); // The result is 11111010
printf("b<<1 = %d\n", b<<1); // The result is 00010010
printf("b>>1 = %d\n", b>>1); // The result is 00000100
return 0;
}
Facts about bitwise operator:
No negative number should be used for the left shift and right shift operators We will get an undefined result if we take negative operands for << and >>. For example, results of both -3 << 3 and 3 << -3 are undefined.
The most useful operator from technical interview is the bitwise XOR operator Wheniniagven set of numbers where all elements occur even number of times except one number, finds the odd occurring number, this problemcanbeefficientlysolvedbyjustdoingXOR of all numbers.
// Function to return the only odd occurring element
int findOdd(int arr[], int n){
int res = 0, i;
for (i = 0; i < n; i++)
res ^= arr[i];
return res;
}
int main(void) {
int arr[] = {12, 12, 14, 90, 14, 14, 14};
int n = sizeof(arr)/sizeof(arr[0]);
printf (“The odd occurring element is %d “, findOdd(arr, n));
return 0;
}
// Output: The odd occurring element is 90
Bitwise operators should not be interchanged with logical operators Because bitwise operators return an integer value and result of logical operators (&&, || and !) is either 0 or 1, Also the logical operators consider any non-zero operand as 1.
int main()
{
int x = 2, y = 5;
(x & y)? printf("True ") : printf("False ");
(x && y)? printf("True ") : printf("False ");
return 0;
}
// Output: False True
The left-shift and right-shift operators are equivalent to multiplication and division by 2 respectively.
As mentioned in point 1, it works only if the numbers are positive.
int main()
{
int x = 19;
printf ("x << 1 = %d\n", x << 1);
printf ("x >> 1 = %d\n", x >> 1);
return 0;
}
// Output: 38 9
We can check whether the number is odd or even with bitwise operators The number is odd if the value of expression (x & 1) would be non-zero, otherwise the value would be zero.
// Note that the output of following program is compiler dependent
int main()
{
unsigned int x = 1;
printf("Signed Result %d \n", ~x);
printf("Unsigned Result %ud \n", ~x);
return 0;
}
/* Output:
Signed Result -2
Unsigned Result 4294967294d */
Carefully use the ~ operator The result of ~ operator can be a negative number if the result is stored in a signed variable (assuming that the negative numbers are stored in 2’s complement form where leftmost bit is the sign bit) and result on a small number can be a big number if the result is stored in a unsigned variable.