Bitwise Operator
One of the C's powerful features is a set of bit manipulation operators. These operators permit the programmer to access and manipulate individual bit within a piece of data. The various bitwise operators are available in C that can be categorized as follows:
1. Bitwise logical operators
2. Bitwise shift operators
3. One's compliment operators
These operators can operates upon int and char data types only and not on float and double.
Bitwise logical operators: (Binary)
There are three bitwise logical operators:
1. Bitwise AND (&)
2. Bitwise OR (|)
3. Bitwise Exclusive OR/XOR (^)
They operate on their operands bit by bit starting from least significant (right most) bit.
Bitwise AND (&)
In the operation of & the operands are compared bit-by-bit basis. Both the operands must be of same type either int or char. The & operates upon a pair of bits to yield a resultant bit. This can be represented by a truth table.
The resultant bit is one if both the bits are one in the bit pair otherwise zero. In the context of & operator, the operation performed on one pair of bits is completely independent of the operation performed on the other pairs. The best use of the & operator is to check whether a particular bit of an operand is zero or one (ON/OFF).
Let x = 5 and y = 9 are two integer variables.
Binary representation
x = 5 0000000000000101
x = 9 0000000000001001
x & y 0000000000000001
Result= 1
Example:
# include <stdio.h>
main ( )
{
int x, y;
printf("Enter the value of x:");
scanf("%d", &x);
printf("Enter the value of y:");
scanf("%d", &y);
showbit (x);
showbit (y);
printf("x & y = %d", x & y);
}
Bitwise OR (|)
This operator is also known as inclusive OR operator. In the operation of | the operands are compared on a bit-by-bit basis. The resultant bit obtained after the operation on a pair of bits is shown in the following truth table:
The | Operator results in 1 only when both or either of the both bits is one in the bit pair otherwise zero. The bitwise 'inclusive OR' operator is often used to set a particular bit as ON or 1 in the given bit pattern of a number.
Ex-
x = 5 0000000000000101
y = 9 0000000000001001
x | y 0000000000001101
Result = 13
Bitwise Exclusive OR/XOR (^)
The ^ operators works similarly & and | operators, only its comparison logic is different. The resultant bit obtained after the operation of ^ on a bit pair is shown in the following truth table:
XOR operator returns one only if one of the two bits is one (ON), otherwise zero. ^ Operator is used to toggle a bit 1 or 0 (ON or OFF).
Ex-
x = 5 0000000000000101
y = 9 0000000000001001
x ^ y 0000000000001100
Result = 12
Bitwise shift operators: (Unary)
The shift operators are used to move bit patterns either to the left or to the right. The operand must be the integer expression.
Right shift operator (>>): It shifts each bit in the operand's bit sequence to the right. The number of places the bits are shifted depends on the number (n) specified in the expression syntax:
operand >> n
Thus, ch >> 3 would shift all bits three places to the right. As the bits are shifted to the right, blanks are created on the left of the bit sequence. These blanks are filled with zeros.
If the operand is a multiple of two then shifting the operand one bit to the right is same as dividing it by 2. Thus, 64>>1 yields 32, 64>>2 yields 16 and 128>>3 yields 32.
Left shift operator (<<): It shifts each bit in the operand's bit sequence to the left. The number of places the bits are shifted depends on the number (n) specified in the expression syntax:
operand << n.
Thus, ch << 5 would shift all bits five places to the left. As the bits are shifted to the left, blanks are created on the right of the bit sequence. These blanks are filled with zeros.
If the operand is a multiple of two then shifting the operand one bit to the left is same as multiplying it by 2. Thus, 64<<1 yields 128, 64<<2 yields 256 and 128<<3 yields 1024.
Note: while using bitwise shift operators the defined number of places (n) must not be a negative number and must not greater than or equal to the width in bits of operand.
One's compliment operators (~): It inverts each bit in the bit sequence of an operand. Thus, all ones present in the bit sequence are converted to zeros and vice-versa. For example: one's compliment of 1010 would be 0101. Similarly one's compliment of 1111 would be 0000.
Note: The bitwise one's compliment operator ~ can efficiently be used in development of file encryption utilities.
Example:
# include <stdio.h>
main ( )
{
int x;
printf ("Enter the value of x :");
scanf ("%d", &x);
printf ("%d", ~x);
}