Introduction
Bitwise operators interpret operands as strings of bits. Bit operations are performed on this data to get the bit strings. These bit strings are then interpreted according to data type. There are six bit operators: bitwise AND(&), bitwise OR(|), bitwise XOR(^), bitwise complement(~), left shift(<<), and right shift(>>).
Program
# includemain() { char c1,c2,c3; printf("ENTER VAULES OF c1 and c2"); scanf("%c,%c",&c1,&c2); c3 = c1 & c2; printf("\n Bitwise AND i.e. c1 & c2 = %c",c3); c3 = c1 | c2; printf("\n Bitwise OR i.e. c1 | c2 = %c",c3); c3 = c1 ^ c2; printf("\n Bitwise XOR i.e. c1 ^ c2 = %c",c3); c3 = ~c1; printf("\n ones complement of c1 = %c",c3); c3 = c1<<2; printf("\n left shift by 2 bits c1 << 2 = %c",c3); c3 = c1>>2; printf("\n right shift by 2 bits c1 >> 2 = %c",c3); }
Input
c1 = 4; c2 = 6;
Output
ENTER VALUES OF c1 and c2 4 6 Bitwise AND i.e. c1 & c2 = 4 Bitwise OR i.e. c1 | c2 = 6 Bitwise XOR i.e. c1 ^ c2 = 2 ones compliment of c1 = -4 left shift by 2 bits c1 << 2 = 16 right shift by 2 bits c1 >> 2 = 1
Explanation
-
Suppose you write
char c1, c2, c3; c1 = 4; c2 = 6;
The binary values are
c1 = 0000 0100 c2 = 0000 0110
-
Suppose you write
c3 = c1 & c2;
The value of c3 is interpreted as follows:
0000 0100 & 0000 0110 ----------. 0000 0100
Each bit of c1 is compared with the corresponding bit of c2. If both bits are 1 then the corresponding bit is set as 1, otherwise it is set as 0. Thus the value of c3 is 4.
-
c3 = c1 | c2
The value of c3 is interpreted as follows:
0000 0100 | 0000 0110 -----------. 0000 0110
Each bit of c1 is compared with the corresponding bit of c2. If any of the bits are 1 then the corresponding bit is set as 1; otherwise it is set as 0. Thus the value of c3 is 6.
-
c3 = c1 ^ c2
The value of c3 is interpreted as follows:
0000 0100 ^ 0000 0110 ----------. 0000 0010
Each bit of c1 is compared with the corresponding bit of c2. If only one bit is 1, the corresponding bit is set to 1; otherwise it is set to 0. Thus you will get the value of c3 as 2 because in the second position for c1, the bit is 0 and for c2, the bit is 1. So only one bit is set.
-
c3 = ~ c1
The value of c3 is interpreted as follows:
~ 0000 0100 ----------. 1111 1011
Each bit of c1 is complemented; for 1 the complement is 0. Thus you will get the value of c3 as −4, because the leftmost bit is set as 1.
-
c3 = c1 << 2;
This is a left-shift operation. The bits are shifted left by two places. c1 indicates the operand that should be an expression returning a whole number. 2 indicates a shift that should not be negative, and its value must be less than the number of bits allocated to that data type.
It is evaluated as follows:
c1 is 0000 0100
It is shifted 2 bits to the left to produce
0001 00**
While shifting, the high-order (left) bits are discarded. Since a vacuum is created on the right side, it is filled with 0s to get 0001 0000. Thus the value is 16.
-
c3 = c1 >> 2;
This is a right shift operation. The bits are shifted right by two places. c1 indicates the operand that should be an expression returning a whole number. 2 indicates a shift that should not be negative, and its value must be less than the number of bits allocated to that data type.
It is evaluated as follows:
c1 is 0000 0100
It is shifted 2 bits to the right to produce
**00 0001
While shifting, the low-order (right) bits are discarded. The asterisks are replaced using one of the following strategies:
Logical shift: In this case, the high-order bits are filled with 0s, thus you get 0000 0001.
Arithmetic shift: In this case the high-order bits are filled with the original sign bits, so if the sign bit is 1, then all bits are filled with 1s; otherwise, they are filled with 0s.
For unsigned data types, logical shift is used, whereas for signed data types arithmetic shift is used.
In these examples, the char data type which is signed is used. In number 4, the sign bit is 0, so you will get the bit pattern 0000 0001 (decimal 1).
No comments:
Post a Comment