Bitwise Operators in C

C language provides six bitwise operators which for manipulation of data at bit level. These operators are used to perform shifting, complementing, ANDing, ORing and so on, al at bit level. These include the bitwise AND (&), bitwise OR (|), bitwise exclusive OR (^), shift left (<<), shift right (>>) and one’s complement (~) operator. All these operators perform the function what is signified by their name. All bitwise operators are binary operators except One’s Complement (~) operator which is a unary operator. Operands can be constants or variables containing integer quantities or characters but not floating point. In addition, the operands can be logical expressions, relational expressions and/or arithmetic expressions. The output of an expression having bitwise operator is numeric. Tabl 1 enumerates the working of each bitwise operator.

Screenshot from 2020-07-16 12-10-03

Screenshot from 2020-07-16 12-10-43

Table 1: Usage and meaning of Bitwise operators

Program 1 shows these operators in action which is followed by the screenshot of the output.

#include <stdio.h>
void main()
{
int i, j, k;
i = 10;
j = 20;
k = i & j;
printf(“i & j is %d\n”,k);
k = i | j;
printf(“i | j is %d\n”,k);

k = i ^ j;
printf(“i ^ j is %d\n”,k);
k = i << j;
printf(“i << j is %d\n”,k);
k = i >> j;
printf(“i >> j is %d\n”,k);
k = ~ i;
printf(“~i is %d\n”,k);
}

Program 1: Program shows bitwise operators in action

Screenshot from 2020-07-16 12-13-16