Bitwise operators in C programming language: In this tutorial I am going to discuss bitwise operators with example C programs. As you know data is stored in memory in the form of bits and a bit is the unit of memory which can be either zero (0) or one (1). We will perform operations on individual bits.
C program
main()
{
int x = 7, y = 9, and, or, xor, right_shift, left_shift;
and = x & y;
or = x | y;
xor = x ^ y;
left_shift = x << 1;
right_shift = y >> 1;
printf("%d AND %d = %d\n", x, y, and);
printf("%d OR %d = %d\n", x, y, or);
printf("%d XOR %d = %d\n", x, y, xor);
printf("Left shifting %d by 1 bit = %d\n", x, left_shift);
printf("Right shifting %d by 1 bit = %d\n", y, right_shift);
return 0;
}
Left shift operator example program
main()
{
int n = 1, c, power;
for ( c = 1 ; c <= 10 ; c++ )
{
power = n << c;
printf("2 raise to the power %d = %d\n", c, power);
}
return 0;
}