Bitwise operators are special types of operators that are used in programming the processor. In processor, mathematical operations like: addition, subtraction, addition and division are done using the bitwise operators which makes processing faster and saves power.
C language supports the following bitwise operators.
| – Bitwise OR
& – Bitwise AND
~ – One’s complement
^ – Bitwise XOR
<< – left shift
>> – right shift
Though we are calling it as a bitwise operators, it always operate on one or more bytes i.e, it will consider the whole representation of the number when applying bitwise operators. By using some techniques, we can manipulate a single bit on the whole representation of the number as we will see in later sections
Bitwise OR – |
Bitwise OR operator | takes 2 bit patterns, and perform OR operations on each pair of corresponding bits. The following example will explain it.
1010
1100
--------
OR 1110
--------
The Bitwise OR, will take pair of bits from each position, and if any one of the bit is 1, the result on that position will be 1. Bitwise OR is used to Turn-On bits as we will see in later sections.
Bitwise AND – &
Bitwise AND operator &, takes 2 bit patterns, and perform AND operations with it.
1010
1100
-------
AND 1000
-------
The Bitwise AND will take pair of bits from each position, and if only both the bit is 1, the result on that position will be 1. Bitwise AND is used to Turn-Off bits.
One’s Complement operator – ~
One’s complement operator (Bitwise NOT) is used to convert each “1-bit to 0-bit” and “0-bit to 1-bit”, in the given binary pattern. It is a unary operator i.e. it takes only one operand.
1001
NOT
-------
0110
-------
Bitwise XOR – ^
Bitwise XOR ^, takes 2 bit patterns and perform XOR operation with it.
0101
0110
------
XOR 0011
------
The Bitwise XOR will take pair of bits from each position, and if both the bits are different, the result on that position will be 1. If both bits are same, then the result on that position is 0.
Left shift Operator – <<
The left shift operator will shift the bits towards left for the given number of times.
int a=2<<1;
Let’s take the binary representation of 2 assuming int is 1 byte for simplicity.
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 0 1 0
Now shifting the bits towards left for 1 time, will give the following result
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
Now the result in decimal is 4. You can also note that, 0 is added as padding the in the position 0.
If you left shift like 2<<2, then it will give the result as 8. Therefore left shifting 1 time, is equal to multiplying the value by 2.
Right shift Operator – >>
The right shift operator will shift the bits towards right for the given number of times.
int a=8>>1;
Let’s take the binary representation of 8 assuming int is 1 byte for simplicity.
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 1 0 0 0
Now shifting the bits towards right for 1 time, will give the following result
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
Now the result in decimal is 4. Right shifting 1 time, is equivalent to dividing the value by 2.
C Program:
#include <stdio.h>
main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
}