binary comparation

Is there any function in c++ to convert decimal number to binary number without using divide algorithm? I want to count different bits of binary format of 2 numbers. like diff(0,2) is 1 bit. or diff(3,15) is 2 bit. I want to write diff function. thanks

Answers


You can find the number of different bits by counting the bits in the xor of the two numbers. Something like this.

int count_bits(unsigned int n) {
    int result = 0;
    while(n) {
        result += 1;
        // Remove the lowest bit.
        n &= n - 1;
    }
    return result;
}

int diff(unsigned int a, unsigned int b) {
    return count_bits(a ^ b);
}

Need Your Help

Matplotlib and timestamp are causing troubles

matplotlib plot timestamp unix-timestamp

Why is this not working? How to fix it? I am trying to plot a figure where x-axis is the temporal axis and y values are the second column in csv file. I want the x plot labels to be in a more human

How to unmarshall when you have xml namespace?

java xml jaxb marshalling xml-namespaces

I am trying to unmarshall an xml using a namespace to create an object from it. I am getting exceptions that the element is not expected. Here is what I've got.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.