Several methods for Java to obtain the number of different bits of two integer binary representations __java

Source: Internet
Author: User

We know that integers can be represented by binary, so how do we get the number of different digits of the two integer binary? This article explores this issue, if there are other ideas welcome to add.


first, the two integers are different or, and then use the Java API to obtain the number of 1 of the differences or results:

    public static int Countbitdiff (int num1, int num2) {return
        integer.bitcount (num1^num2);   
    }

Using this method, one line of code can get the result. The 1 number after the num1^num2 represents the different digits of the NUM1 and num2, and then uses the static method of integer bitcount () to compute the number of 1 in NUM1 and num2, thus obtaining the result. So how did Integer.bitcount () do it. See JDK Source code:

    public static int Bitcount (int i) {
        //HD, Figure 5-2
        i = i-((i >>> 1) & 0x55555555);
        i = (I & 0x33333333) + ((I >>> 2) & 0x33333333);
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
        i = i + (i >>> 8);
        i = i + (i >>>);
        return I & 0x3f;
    }

In essence, the binary method, 221 groups, then add four four, then eight eight, and finally get everyone's sum. The first line calculates the number of 1 in each of the two digits, and with the corresponding two-bit to store this number, such as: 01101100-> 01011000, that is, the first two-digit segment 01 10 11 00, respectively, 1 1 2 0, 1, with two-digit binary number is expressed as 01 01 10 00, combined to 01011000. The second line calculates the number of 1 per four digits and stores the number with the corresponding four bits. such as: 01101100 after the first line calculated after 01011000, and then 01011000 per four-digit segment into 0101 1000, paragraph shift added: the front segment 01+01 = 10, the posterior segment 10+00=10, respectively, four-bit binary number for 0010 0 010, together for 00100010. The following lines, and so on, calculate the number of 1 in each 8-bit, 16-bit, 32-bit, respectively. It is easy to understand the form of 0x55555555, 0x33333333, and 0x0f0f0f0f written in binary numbers.

First, the two integers are different or, and then use the tips to calculate the number of digits in 1:

public static int Countbitdiff (int num1, int num2) {       
    int dif=num1^num2;
    int count=0;
    while (dif!=0) {
        dif&= (dif-1);
        count++;
    }
    return count;
}

Here the idea is the same, the first use of NUM1 and num2 to do XOR or operation to get DIF results, dif 1 of the number is NUM1 and num2 in different digits. Here's a little trick, as long as dif is not equal to 0, using the number of dif&= (DIF-1) to compute the number of 1 in dif, dif&= (dif-1) is to turn the last 1 in dif to 0, and we use dif&= (dif-1) each time. And in 0 comparisons, if this is not zero at this point, there is a 1 before the last one, and the number of loops executing dif&= (dif-1) is 1 of the dif.


three, each time the two integral types to the right 1-bit, and then take a low comparison, calculate the number of different digits:

    public static int CountBitDiff4 (int num1, int num2) {
        int count = 0;
        for (int i = 0;i<32;i++) {
            if (num1&1)!= (num2&1)) {
                count++;
            }
            num1>>=1;
            num2>>=1;
        }
        return count;
    }

This idea is actually and we convert the decimal system to binary when the idea is almost, but here each time after the right shift with 1, so as to obtain the lowest bit of the integer, and then compared with the lowest phase of another integral type.


four, or first to two integers or, and then converted to a string, the string of 0 to replace the "", the remaining 1 is the number of different digits:

    public static int CountBitDiff5 (int num1, int num2) {
        String str=integer.tobinarystring (num1^num2);
        Str=str.replaceall ("0", "");
        return Str.length ();
    }

You can see that the idea is very ingenious, the integer or later into a string, using the ReplaceAll method to replace 0 with "", so that the number of the remaining 1 is a different number of digits.


Here are just a few ideas, of course there are other ways, if there are additional welcome comments under the comment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.