44. WAP to scan a number and bit position from the user also display a menu for set, clear and compliment of bit. Take a menu selection input from user, depend on the selection perform the task and display the results.
#include<stdio.h>
void
main()
{
int num,pos,op,c=0;
printf("Enter the number :
\n");
scanf("%d",&num);
L1:
printf("Enter the bit position
(0-31) \n");
scanf("%d",&pos);
if(pos>=0 && pos<=31)
{
printf("Select the option
\n 1) Set a bit \n 2) Clear a bit \n 3) Toggle a bit\n");
scanf("%d",&op);
if(op==1)
{
num=num|(1<<pos);
printf("num = %d
\n",num);
}
else if(op==2)
{
num=num&~(1<<pos);
printf("num = %d
\n",num);
}
else if(op==3)
{
num=num^(1<<pos);
printf("num = %d
\n",num);
}
else
printf("Unknown
Option\n");
}
else
{
c++;
printf("Wrong Bit position
given as input");
if(c<3)
goto L1;
}
}
Comments
Post a Comment