Bit Wise Operators
Bit Wise operators are manipulates of individual bits with in a word of memory. The bit wise operators can be divided in to three general category.
- One’s Complement Operator
- Logical bit wise Operator
- Shift Operator
One’s Complement Operator:
The one’s complement operator (~) is a unary operator that causes the bits of its operand to be inverted so that 1s become 0s and 0s become 1s. this operator always precedes its operand. The operand must be an integer – type quantity. Generally, the operand will be an unsigned octal or an unsigned hexadecimal quantity.
Ex:- consider the hexadecimal number 07ff. the corresponding bit pattern, expressed in terms of a 16 – bit word, is 0000 0111 1111 1111. So that one’s complement of this bit pattern is
1111 1000 0000 0000
f 8 0 0 which corresponds to the hexadecimal number f800.
Ex:- #include<stdio.h>
main( )
{
unsigned i=0x5b3c;
printf("Hexadecimal values:i=%x ~i=%x\n",i,~i);
printf("Hexadecimal equivalents: i=%u ~i=%u",i,~i);
}
Logical bit wise operators:
There are three logical bit wise operators
- bitwise and (&)
- bitwise exclusive or (^)
- bitwise or (i)
The operations are carried out independently on each pair of corresponding bits within the operands. Thus, the least significant bits (i.e., the rightmost bits) within the two operands will be compared, then the next least significant bits, and so on, until all of the bits have been compared. The results of these comparisons are:
- A bitwise and expression will return a 1 if both bits have a value of 1
(i.e., if both bits are true). Otherwise, it will return a value of 0.
- A bitwise exclusive or expression will return a 1 if one of the bits has a value of 1 and the other has a value of 0 (one bit is true, the other false). Otherwise, it will return a value of 0.
- A bitwise or expression will return a 1 if one or more bits have a value of 1 ( one or both bits are true). Otherwise, it will return a value of 0.
Table Logical Bitwise operators
b1 | b2 | b1 & b2 | b1 ^ b2 | b1 | b2 |
1 1 0 0 | 1 0 1 0 | 1 0 0 0 | 0 1 1 0 | 1 1 1 0 |
Ex:- a and b are unsigned variables whose values are 0x6db7 and 0xa726
one’s complement of a and b
a=6db7 b=a726
a= 0110 1101 1011 0111 b= 1010 0111 0010 0110
~a= 1001 0010 0100 1000 ~b= 0101 1000 1101 1001
9 2 4 8 5 8 d 9
~a= 0x9248 ~b=0x58d9
bitwise and bitwise exclusive or
a= 0110 1101 1011 0111 a= 0110 1101 1011 0111
b= 1010 0111 0010 0110 b= 1010 0111 0010 0110
a & b= 0010 0101 0010 0110 a ^ b= 1100 1010 1001 0001
2 5 2 6 c a 9 1
a & b= 0x2526 a ^ b= 0xca91
bitwise or ( | )
a= 0110 1101 1011 0111
b= 1010 0111 0010 0110
a | b = 1110 1111 1011 0111
e f b 7
a | b = 0xefb7
Each of the logical bitwise operators has its own precedence. The bitwise and (&) operator has the highest precedence, followed by bitwise exclusive or (^), then bitwise or ( | ). Bitwise and follows the equality operators (== and !=). Bitwise or is followed by logical and (&&). The associativity for each bitwise operator is left to right.
The Shift Operators:
The two bitwise shift operators are shift left (<<) and shift right (>>). Each operator requires two operands. The first is an integer – type operand that represents the bit pattern to be shifted. The second is an unsigned integer that indicates the number of displacements. This value cannot exceed the number of bits associated with the word size of the first operand.
The left shift operator causes all of the bits in the first operand to be shifted to the left by the number of positions indicated by the second operand. The leftmost bits in the original bit pattern will be lost. The rightmost bit positions that become vacant will be filled with 0s.
The right shift operator causes all of the bits in the first operand to be shifted to the right by the number of positions indicated by the second operand. The rightmost bits in the original bit pattern will be lost. If the bit pattern being shifted represents an unsigned integer, then the leftmost bit positions that become vacant will be filled with 0s. Hence, the behavior of the right shift operator is similar to that of the left shift operator when the first operand is an unsigned integer.
Ex:- a is an unsigned integer variable, whose value is 0x6db7. The expression
b = a<<6; (left shift operator)
|lost bits|
a= 0110 1101 1011 0111
shift left
a << 6= 0110 1101 1100 0000 = 0x6dc0
| 0s |
b= a >> 6;
|lost bits|
a= 0110 1101 1011 0111
shift right
A >> 6 = 0000 0001 1011 0110 = 0x01b6
| 0s |
Ex:- #include<stdio.h>
main( )
{
unsigned a= 0xf05a;
int b=a;
printf("%u %d\n", a,b);
printf("%x\n", a>>6);
printf("%x\n",b>>6);
}
Bitwise Assignment Operators:
C also contains the following bitwise operators.
&= ^= |= <<= >>=
These operators combine the preceding bitwise operations with ordinary assignment. The left operand must be an assignable integer – type identifier, and the right operand must be a bitwise expression. The left operand is interpreted as the first operand in the bitwise expression. The value of the bitwise expression is then assigned to the left operand.