C Program to Demonstrate Bitwise Operators

Write C code to demonstrate the use of bitwise operators.

//Write a c program to demonstrate bitwise operator

#include <stdio.h>

int main()
{
	int a = 7,b = 5, c;

	printf("Bitwise AND \t\t: %d & %d = %d", a, b, a & b);

	printf(" \nBitwise OR \t\t: %d | %d = %d",a, b, a | b);

	printf(" \nBitwise XOR \t\t: %d ^ %d = %d", a, b, a ^ b);

	printf(" \nBitwise COMPLEMENT \t: ~%d = %d", a, ~a);

	printf("\nRight Shift \t\t: %d >> 2 = %d", a, a >> 2);

	printf("\nLeft Shift \t\t: %d << 2 = %d\n", a, a << 2);

	return 0;
}

Output:

Bitwise AND : 7 & 5 = 5
Bitwise OR : 7 | 5 = 7
Bitwise XOR : 7 ^ 5 = 2
Bitwise COMPLEMENT : ~7 = -8
Right Shift : 7 >> 2 = 1
Left Shift : 7 << 2 = 28


<< Previous | Next >>