Bitwise Operators
Bitwise Operators
Bitwise operators are special types of operators that are used in programming the processor. In processor,
mathematical operations like: addition, subtraction, addition and division are done using the bitwise operators
which makes processing faster and saves power.
Operators
Meaning of operators
&
Bitwise AND
Bitwise OR
Bitwise exclusive OR
Bitwise complement
<<
Shift left
>>
Shift right
= 8 (In decimal)
As, every bitwise operator works on each bit of data. The corresponding bits of two inputs are check and if both
bits are 1 then only the output will be 1. In this case, both bits are 1 at only one position,i.e, fourth position
from the right, hence the output bit of that position is 1 and all other bits are 0.
#include <stdio.h>
int main()
{
int a=12,b=39;
printf("Output=%d",a&b);
return 0;
}
Output
Output=4
Bitwise OR operator in C
The output of bitwise OR is 1 if either of the bit is 1 or both the bits are 1. In C Programming, bitwise OR
operator is denoted by |.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
= 29 (In decimal)
#include <stdio.h>
int main()
{
int a=12,b=25;
printf("Output=%d",a|b);
return 0;
}
Output
Output=29
00010101
= 21 (In decimal)
#include <stdio.h>
int main()
{
int a=12,b=25;
printf("Output=%d",a^b);
return 0;
}
Output=21
2's Complement
Two's complement is the operation on binary numbers which allows number to write it in different form. The 2's
complement of number is equal to the complement of number plus 1. For example:
Decimal
Binary
2's complement
00000000
00000001
12
00001100
220
11011100
If we consider the bitwise complement of 35, 220(in decimal) is converted into 2's complement which is -36.
Thus, the output shown by computer will be -36 instead of 220.
N=-(N+1) ?