C Program to Count the Number of Bits to be Flipped to Convert X to Y

«
»

This is a C Program to count the number of bits needed to be flipped to integer X to generate integer Y.

Problem Description

This C Program counts the number of bits needed to be flipped to integer X to generate integer Y.

Problem Solution

Take input from the user and performs binary right shift operation as shown in the program below.

Program/Source Code

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
 */
#include <stdio.h>
#include <stdlib.h>
#define NUM_BITS_INT (sizeof(int)*8)
 
void main()
{
    int n, m, i, count = 0, a, b;
 
    printf("Enter the number\n");
    scanf("%d", &n);
    printf("Enter another number\n");
    scanf("%d", &m);
    for (i = NUM_BITS_INT-1;i >= 0;i--)
    {
        a = (n >> i)& 1;
        b = (m >> i)& 1;
        if (a != b)
            count++;
    }
    printf("flip count = %d\n", count);
}
Program Explanation

In this C Program, we are reading the two integer values using ‘n’ and ‘m’ variables respectively. For loop is used to count the number of bits needed to be flipped to integer X to generate integer Y.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Binary Right shift operation, the left operand’s value is moved right by the number of bits specified by the right operands then copy a bit to the result if it exists in both operands.

If condition statement is used to check that the value of bit is not equal. If the condition is true, then execute the statement and print the number of bits to be flipped to integer X to generate integer Y.

Runtime Test Cases
 
$ 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.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.