Bitwise Operators
Operator
Description
Example
Evaluation
Result
&
Bitwise AND between the left and right Operands. (Consider Both Operand Value in Binary)
12 & 5
00001100
00000101
00000100
4
|
Bitwise OR between the left and right Operands. (Consider Both Operand Value in Binary)
12 | 5
00001100
00000101
00001101
13
^
Bitwise EXCLUSIVE OR between the left and right Operands. (Consider Both Operand Value in Binary)
12 ^ 5
00001100
00000101
00001001
9
~
Bitwise COMPLEMENT of the Operand. (Consider Operand Value in Binary)
~12
00001100
11110011
01110011 00001100 00000001 00001101
13
<<
Bitwise LEFT SHIFT of the left operand, Number of bit(s) to be shifted to be defined by the right Operand. (Consider Left Operand Value in Binary)
12<<2
00001100
0000110000
48
>>
Bitwise RIGHT SHIFT of the left operand, Number of bit(s) to be shifted to be defined by the right Operand. (Consider Left Operand Value in Binary)
12>>2
00001100
00000011
3
#include<stdio.h>
int main(){
printf("12 & 5 Evaluated %d\n", 12 & 5);
printf("12 | 5 Evaluated %d\n", 12 | 5);
printf("12 ^ 5 Evaluated %d\n", 12 ^ 5);
printf("~12 Evaluated %d\n", ~12);
printf("12<<2 Evaluated %d\n", 12<<2);
printf("12>>2 Evaluated %d\n", 12>>2);
return 0;
}
#include<stdio.h>
int main(){
printf("%d %d\n", 0, ~0);
printf("%d %d %d\n", 3, 5, 3 | 5);
printf("%d %d %d\n", 3, 2, 3<<2);
printf("%d %d %d\n", 12, 2, 12>>2);
return 0;
}
#include<stdio.h>
int main(){
int a = 10;
a = 10;
printf("%d | 1 = %d\n", a, a | 1);
a = 10;
printf("%d & 1 = %d\n", a, a & 1);
a = 11;
printf("%d & 1 = %d\n", a, a & 1);
a = 10;
printf("%d ^ 1 = %d\n", a, a ^ 1);
return 0;
}
Last updated