1
5.9kviews
Explain with example bitwise operators in C language
1 Answer
| written 9.3 years ago by |
Bitwise operators modify variables considering the bit patterns that represent the values they store.
| Operator | Description |
|---|---|
| & | Bitwise AND |
| Bitwise Inclusive OR | |
| ^ | Bitwise Exclusive OR |
| ~ | Bit Inversion |
| << | Shift Left |
| > | Shift Right |
Bitwise AND, OR, XOR, NOT is same as logical operator.
Shift Left: If no is shifted by n bits then its value is multiplied by $2^n$.
Shift Right: If no is shifted right by n bits then its value is divided by $2^n$.
Example:
#include<stdio.h>
int main(){
unsigned int x = 15, y = 30;
printf("%d", (x&y)); //output: 14
printf("%d", (x|y)); //output: 31
printf("%d", (x^y)); //output: 17
printf("%d", (~x)); //output: -16
printf("%d", (~y)); //output: -31
printf("%d", (x<<1)); //output: 30
printf("%d", (x<<3)); //output: 120
printf("%d", (x>>1)); //output: 7
printf("%d", (y>>2)); //output: 7
}