Computer Science‎ > ‎

## C Program demonstrating the use of Bitwise Operators

```/*This program demonstartes the bitwise operations OR,XOR,NOT,AND,LEFT SHIFT and RIGHT SHIFT.*/
#include<stdio.h>
int main()
{
int num1,num2,resOR,resAND,resXOR,resNOT,resLSHIFT,resRSHIFT;
printf("This is program to demonstrate the results of bitwise OR,AND,NOT,XOR,LEFT SHIFT and RIGHT SHIFT operations \n");
printf("Enter the numbers on which the operations are to be performed\n");
scanf("%d%d",&num1,&num2);
/*In the OR operatrion the numbers are represented by the computer in binary form and then
Logical OR operation is performed on each subsequent pair.The resulting binary number is converted in decimal form and is the result.*/
resOR=num1|num2;
printf("The result of bitwise OR operation is %d\n",resOR);
/*In the AND operatrion the numbers are represented by the computer in binary form and then
Logical AND operation is performed on each subsequent pair.The resulting binary number is converted in decimal form and is the result.*/
resAND=num1&num2;
printf("The result of bitwise AND operation is %d\n",resAND);
/*In the NOT operatrion the number is represented by the computer in binary form and then
each bit is inverted.The resulting binary number is converted in decimal form and is the result.*/
resNOT=~num1;
printf("The result of bitwise NOT operation on %d is %d\n",num1,resNOT);
resNOT=~num2;
printf("The result of bitwise NOT operation on %d is %d\n",num2,resNOT);
//Logical XOR operation is performed on the indivisual pair of bits.
resXOR=num1^num2;
printf("The result of bitwise XOR operation is %d\n",resXOR);
resLSHIFT=num1<<1;    //The bits are shifted left wards by one place and the MSD occupies the LSD
printf("The result of bitwise LEFT SHIFT by 1 bits operation on %d is %d\n",num1,resLSHIFT);
resLSHIFT=num2<<1;
printf("The result of bitwise LEFT SHIFT by 1 bits operation on %d is %d\n",num2,resLSHIFT);
resRSHIFT=num1>>1; //The bits are shifted right wards by one place and the MSD occupies its place unchanged
printf("The result of bitwise RIGHT SHIFT by 1 bits operation on %d is %d\n",num1,resRSHIFT);
resRSHIFT=num2>>1;
printf("The result of bitwise RIGHT SHIFT by 1 bits operation on %d is %d\n",num2,resRSHIFT);
getch();
}
/*A sample run of the program was carried out and the results were found as:-
This is program to demonstrate the results of bitwise OR,AND,NOT,XOR,LEFT SHIFT and RIGHT SHIFT operations
Enter the numbers on which the operations are to be performed
2 3
The result of bitwise OR operation is 3
The result of bitwise AND operation is 2
The result of bitwise NOT operation on 2 is -3
The result of bitwise NOT operation on 3 is -4
The result of bitwise XOR operation is 1
The result of bitwise LEFT SHIFT by 1 bits operation on 2 is 4
The result of bitwise LEFT SHIFT by 1 bits operation on 3 is 6
The result of bitwise RIGHT SHIFT by 1 bits operation on 2 is 1
The result of bitwise RIGHT SHIFT by 1 bits operation on 3 is 1
*/```