How to write a c program to clear a bit in c programming ?
Solution:
#include <stdio.h>
void printBinary(int n);
void clearBit(int *n, int i);
int main()
{
int n = 7;
printBinary(n);
clearBit(&n, 2);
printBinary(n);
return 0;
}
void clearBit(int *n, int i)
{
*n &= ~(1<<i);
}
void printBinary(int n)
{
unsigned int i;
for(i = 1<<31; i >= 1; i >>= 1)
{
if(i & n) printf("1");
else printf("0");
}
printf("\n");
}