Bitwise operator in c

Bitwise operator in c

The C language has the ability to support manipulation of data with the bit level. The bitwise operator apply on integers only. and bitwise operators are used for operations on individual bits. The following bitwise operations are –

Bitwise Operators Name
& Bitwise AND
| Bitwise OR
<< Left shift
>> Right shift
~ Once’s Complement
^ Bitwise XOR

Bitwise AND operator

The output of the bitwise AND is 1 if both corresponding operands bits is 1. if any corresponding bit of operand is 0 then the result of corresponding bit evaluate 0

15 = 00001110 (In Binary)
26 = 00011010 (In Binary)
Bits operation of 15 and 26
   00001111
& 00011010
  ___________________
   00001010 = 10 (In decimal)

#include < stdio.h >
int
main()
{
int a = 15, b = 26;
printf(“Output is = %d”, a&b);
return 0;
}

output is= 10

Bitwise OR operator |

The output of the bitwise OR is 1, if any corresponding bit of two operands bits is 1. The bitwise OR operator is denoted by |

15 = 00001111 (In Binary)
26 = 00011010 (In Binary)
Bitwise OR Operation of 15 and 26
   00001111
| 00011010
  ________________
   00011111 = 31 (In decimal)

#include < stdio.h >
int
main()
{
int x = 15, y = 26;
printf(“Output = %d”, x|y);
return 0;
}