CareerCup-5.5

Write a function to determine the number of bits required to convert integer A to integer B
Input: 31, 14
Output: 2

#include <iostream>
using namespace std;

int convertCount(int a, int b)
{
    int t = a ^ b;
    int count = 0;
    while(t != 0)
    {
        t = t & (t - 1);
        count ++;
    };
    return count;
};
	
int main()
{
    cout<<convertCount(31, 14)<<endl;
    system("pause"); 
};

Comments

comments powered by Disqus