25. WAP to set, clear and toggle a bit.
#include
<stdio.h>
void
main()
{
// Setting a bit
int num,pos;
printf("Enter the number : ");
scanf("%d",&num);
printf("Enter the position : ");
scanf("%d",&pos);
printf("Before setting bit, number is
: %d\n",num);
num=num|1<<pos;
printf("After setting bit, number is :
%d\n",num);
// Clearing a bit
int num,pos;
printf("Enter the number : ");
scanf("%d",&num);
printf("Enter the position : ");
scanf("%d",&pos);
printf("Before clearing bit, number is
: %d\n",num);
num=num&~(1<<pos);
printf("After clearing bit, number is
: %d\n",num);
// Toggling a bit
int num,pos;
printf("Enter the number : ");
scanf("%d",&num);
printf("Enter the position : ");
scanf("%d",&pos);
printf("Before toggling bit, number is
: %d\n",num);
num=num^1<<pos;
printf("After toggling bit, number is
: %d\n",num);
}
Comments
Post a Comment