How to write a c program to toogle a bit in c programming ?
Solution:
#include <stdio.h>
void printBinary(int n);
void toggleBit(int *n, int i);
int main()
{
int n = 7;
printBinary(n);
toggleBit(&n, 5);
printBinary(n);
return 0;
}
void toggleBit(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");
}
https://en.wikipedia.org/wiki/Bit_manipulation