Here is source code of the C Program to count the number of bits needed to be flipped to integer X to generate integer Y. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to Count the Number of Bits needed to be Flipped
* to Integer X to Generate Integer Y
#define NUM_BITS_INT (sizeof(int)*8)
int n, m, i, count = 0, a, b;
printf("Enter the number\n");
printf("Enter another number\n");
for (i = NUM_BITS_INT-1;i >= 0;i--)
a = (n >> i)& 1;
b = (m >> i)& 1;
if (a != b)
printf("flip count = %d\n", count);
$ cc flip.c $ a.out Enter the number 127 Enter another number 125 flip count = 1 $ a.out Enter the number 127 Enter another number 128 flip count = 8 $ a.out Enter the number 42 Enter another number 21 flip count = 6
Sanfoundry Global Education & Learning Series – 1000 C Programs.