Analysis of algorithms for several Java bit operations

Source: Internet
Author: User

Question one:

Given a positive integer n, the first bit 1 in the binary form (order from low to high) is obtained.

For example, given a positive integer of 12, its low 8-bit binary representation is: 00001100

From low to high order, the first 1 appears in the third place.

Version One:

The lowest bit begins with the (&) operation judged for each bit if it is 1 until the first 1 is encountered.

Algorithm implementation (version one):

     Public Static intGetFirstBit1 (intN) {
intMask = 1; intpos = 1; while(Mask <=N) { if((n&mask) = =mask) { Break; }Else{Mask<<= 1; POS++; } } returnPOS; }

Version two:

Assuming that the first 1 appears in the POS bit, note that tmp= (n^ (N-1)) +1 actually represents the POS bit and its right-hand all 1 and the left of the POS 0 of the number, the value of the POS problem translates to the question of how many 1 of the TMP is asked.

     Public Static int getFirstBit1 (int  N) {        int tmp = (n^ (N-1)) +1;// Represents the POS bit and its right all 1 and the left of the POS all 0 of        that number int pos = 0;          while (tmp > 1) {            >>= 1;            POS++        ;        } return pos;    }

Question two:

Given a positive integer n, the number of bits in the second binary form is 1.

Version One:

Check whether each bit is 1 and count from low to high.

    /*Conventional Algorithms*/     Public Static intGetNumberOfBit1 (intN) {        intCount = 0; intMask = 1;  while(Mask <=N) {System.out.println ("OK"); if((n&mask) = =mask) {Count++; } Mask<<= 1; }        returncount; }

Version two:

Notice that the result of (N-1) &n is bound to eliminate the rightmost 1 of N, with an example of a low 8-bit n=12:

12:00001100

12-1:00001011

12&11:00001000

The result is that the rightmost 1 of 12 is erased.

loop so that it loops until N is 0, and how many loops, representing how many 1 are in the original number n.

    /* a more efficient algorithm */     Public Static int getNumberOfBit1 (int  N) {        int count = 0;          while (N! = 0) {            count+ +            ; = (N-1) &N;            System.out.println ("OK");        }         return count;    }

Question three:

There are several positive integers in an unordered array, one of which has an odd number of occurrences, and the remaining integers appear an even number of times to find the integer that appears in odd number of times.

For example, given an unordered array:

int [] ary = {2,3,6,3,4,9,4,2,6};

An integer that appears odd number of times is: 9

Analysis:

The difference or operation satisfies the commutative nature, for example the result of 2^3^2^3 is the same as the result of 2^2^3^3.

Notice an even number of times, so the XOR operation can be considered: when both phases return 0, the difference is 1.

This law is shown in: For an integer, repeated even several times, the result of the XOR operation will necessarily be 0;

Further, the superposition of the above phenomena, manifested in: to multiple integers, each integer has an even number of times, the result of the XOR operation is bound to be 0, and its appearance is not related to the order of;

Algorithm implementation:

     Public Static int Find (int[] ary) {        int tmp = 0;          for (int i = 0; i < ary.length; i++) {            = tmp^ary[i];//sequentially xor        }          return  tmp;    }

Issue four (extension of issue three):

There are several positive integers in an unordered array, and if there are two integers that appear odd, the remaining integers appear even several times to find the two integers that appear odd and return them and the number.

For example, given an unordered array:

int [] ary = {2,3,6,3,4,9,4,2,1,6};

Two integers with odd number of occurrences are: 9 and 1, which return them and 10.

Analysis:

Suppose that two integers with odd occurrences are A and b;

One central idea is to separate A and B into different two groups, at which point the situation reverts to the situation of question three. Each of the two groups of the elements in order to do the XOR or operation, will be a and B respectively.

Now the question is how to group?

According to the idea of question three: The result of the team original array ary in turn will be equivalent to the result of a^b, and note that A and B are not equal, so the result of A^b must not be 0.

Since a^b is not zero, then the second binary representation must exist in 1, that is, because of this 1 existence, you can distinguish between A and B, because only A and B in this bit value is not the same, the corresponding bit of its XOR result is 1.

Select the binary representation of the a^b result in any one of the 1 (for example, select the rightmost 1) as a grouping condition, when traversing the ARY array, the first group to determine, after grouping in their respective or operation can be.

Algorithm implementation:

     Public Static intFindint[] ary) {        intTMP = 0, Mask; intTargetone = 0,targettwo = 0;  for(intele:ary) {tmp= tmp^Ele; }        intpos = GETFIRSTBIT1 (TMP);//1 as the rightmost variable of TMP as a grouping conditionMask = 1<< (pos-1);//The mask here is actually a bit mask, only the POS bit is 1, the remaining bits are 0         for(inti = 0; i < ary.length; i++) {            if((ary[i]&mask) = = mask) {//Description Ary[i] The POS bit is 1Targetone = targetone^Ary[i]; }Else{//Description Ary[i] The POS bit is 0Targettwo = targettwo^Ary[i]; }        }        returnTargetone+Targettwo; }

Reprint Annotated original address: https://i.cnblogs.com/EditPosts.aspx?postid=7689800

Analysis of algorithms for several Java bit operations

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.