Bitwise Operators
1. Bitwise Operators are used for operating numbers at the bit level.
2. There are following Bitwise operators supported by C language:
Operators | What They Do .. | Precedence |
---|---|---|
~ | This operator gives the NOT for every bit of the character. Also , this is followed : ~x = -x - 1. | 1 |
<< | It shifts the bits to the left , accordingly. | 2 |
>> | Similar operation , but to the right. | 2 |
& | This operator performs the AND logical operation on every corresponding bit of characters , and gives cumulative result of each bit as the result. | 3 |
| | Similar , but performs OR operation on each bit. | 4 |
^ | Similar , but performs XOR operation on each bit. | 5 |
Syntax for Bitwise NOT (~):
~[variable\constant]
Example:~5
Syntax for all other Bitwise Operators:
[variable\constant] [Bitwise Operator] [variable\constant]
Example:(A ^ B)
C Example
#include<stdio.h> int main() { int a,b,c,d,e,f,g,h; a=10; b=11; c=~a; // Bitwise NOT d=b<<2; // Bitwise LEFT SHIFT e=a>>2; // Bitwise RIGHT SHIFT f=a&b; // Bitwise AND g=a|b; // Bitwise OR h=a^b; // Bitwise XOR printf("a=%d,b= %d",a,b); printf("c=%d,d= %d,e=%d,f= %d,g=%d,h=%d,",c,d,e,f,g,h); return 0; }