Published by: Nuru
Published date: 21 Jun 2021
Operator | Meaning |
---|---|
& | Bitwise AND |
| | Bitwise OR |
& | Bitwise AND |
^ | Bitwise X-OR |
<< | Shift left |
>> | Shift right |
a | b | a & b | a| b | a ^ b |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
Bitwise AND (& )The & operator is surrounded by its two integral operands. The result of AND operations is 1 if both the bit is 1 otherwise 0.Example: For 4-byte word machine:a = 00000000 00000000 00000000 00001101 ( 13 DEC )
b= 00000000 00000000 00000000 00011001 ( 25 DEC.)
c= 00000000 00000000 00000000 00001001 ( DEC 9)Bitwise OR ( | )The | operator is also surrounded by its two integral operands. The result of ORing operations is 1 if at least one of the bit of its operand is 1
otherwise 0.Example:a = 00000000 00000000 00000000 00001101 (13 D )
b= 00000000 00000000 00000000 00011001 (25 D)
c= 00000000 00000000 00000000 00011101 ( 29 D)Bitwise Exclusive OR ( ^)The ^ operator is also surrounded by its two integral operands. The result of X-OR operations is 1 if only one of the bit of its operand is 1
otherwise 0.
Example below:
a = 00000000 00000000 00000000 00001101 (13 D )
b= 00000000 00000000 00000000 00011001 (25 D)
c= 00000000 00000000 00000000 00010100 (20 D)
/*Program to demo bitwise logical operators*/
#include
main()
{
int a=13,b=25,c;
c=a&b;
printf("%d&%d=%d",a,b,c);
c=a|b;
printf("\n%d|%d=%d",a,b,c);
c=a^b;
printf("\n%d^%d=%d",a,b,c);
return 0;}Bitwise Shift operatorsThe shift operators are used to shift the bit of any integral expression to the left or right as indicated by the number of bits.
The shift right operator (>> )