Bitwise Operators
Bitwise Operators
Output = 8
Bitwise OR operator |
The output of bitwise OR is 1 if at least one corresponding bit of two
operands is 1. In C Programming, bitwise OR operator is denoted by |.
Output = 21
2's Complement
Two's complement is an operation on binary numbers. The 2's complement
of a number is equal to the complement of that number plus 1. For example:
Decimal Binary 2's complement
0 00000000 -(11111111+1) = -00000000 = -
0(decimal)
1 00000001 -(11111110+1) = -11111111 = -
256(decimal)
12 00001100 -(11110011+1) = -11110100 = -
244(decimal)
220 11011100 -(00100011+1) = -00100100 = -
36(decimal)
complement = -36
Output = 11
printf("\n");
return 0;
}
& Binary AND Operator copies a bit to the result if it exists (A & B) =
in both operands. 12 i.e.,
0000 1100
<< Binary Left Shift Operator. The left operands value is A << 2 =
moved left by the number of bits specified by the right 240 i.e.,
operand. 1111 0000
>> Binary Right Shift Operator. The left operands value is A >> 2 =
moved right by the number of bits specified by the right 15 i.e.,
operand. 0000 1111
Example
Try the following example to understand all the bitwise operators
available in C −
#include <stdio.h>
main() {
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
When you compile and execute the above program, it produces the
following result −
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15