One of the problem of [algorithm] bit arithmetic

Source: Internet
Author: User
Tags sca

One, the value of two integers without an additional variable exchange
a=a^b;b=a^b;a=a^b;
      or:
a=a+b;b=a-b;a=a-b;
Two, different from any comparative judgment to find out two of the larger

There are two methods, the method one has certain limitations, the value of a-B may overflow, so that after the overflow symbol changes, the return result is not correct.

And the method two pairs A and b whether the different number is judged, if the same number, then return according to the method, otherwise directly return the positive number on the line.

The value of a-B is treated as greater than 0 if 0 is processed.

//if n is 1, 0 is returned, and if n is 0, 1 is returned.     Public Static intFlipintN) {returnn ^ 1; }    //returns the sign of an integer n, a positive number and 0 returns 1, a negative number returns 0     Public Static intSignintN) {returnFlip ((n >>) & 1); }    //Method One     Public Static intGETMAX1 (intAintb) {intc = A-b; intScA =Sign (c); intScB =Flip (ScA); returnA * ScA + b *ScB; }    //Method Two     Public Static intGETMAX2 (intAintb) {intc = A-b; intSA =Sign (a); intSB =Sign (b); intsc =Sign (c); intDifsab = sa ^SB; intSamesab =Flip (DIFSAB); intReturna = Difsab * SA + Samesab *SC; intReturnb =Flip (Returna); returnA * Returna + b *Returnb; }
How many 1 topics are in binary expressions for integers:

Given a 32-bit integer n, it can be 0, integer, negative. Returns the number of 1 in the integer binary expression.

Let's start with a simple introduction to the shift operator:

The shift operator can only be used to handle an integral type. The left shift operator (<<) moves the operand to the left of the operator to the left by the number of digits specified on the right side of the operator (0 in the low). The "signed" Right Shift operator (>>) moves the operand to the left of the operator to the right by the number of digits specified to the right of the operator. The "signed" right shift operator uses "symbol extension": If the symbol is positive, insert 0 in the high position, and if the symbol is negative, insert 1 at the high. Java adds an "unsigned" right shift operator (>>>) that uses "0 expansion": both positive and negative, are inserted at high levels.

Solution One:

The simplest solution.

Each time an unsigned right shift is made, check that the rightmost bit is 1. In the most complex cases, 32 cycles are performed.

 Public Static int count1 (int  n) {        int res = 0;          while (n! = 0)            {+ = n & 1;             >>>= 1;        }         return res;    }

Solution Two:

The number of cycles is related to the number of 1. Each cycle removes the rightmost 1.

 Public Static int count2 (int  n) {        int res = 0;          while (n! = 0)            {&= (n-1);            Res++        ;        } return res;    }

Solution Three:

In the same way as solution two, just delete the rightmost 1 differently.

 Public Static int count3 (int  n) {        int res = 0;          while (n! = 0)            {-= N & (~n + 1);            Res++        ;        } return res;    }

Solution Four:

Parallel algorithms:

 Public Static int count4 (int  n) {        = (N & 0x55555555) + ((n >>> 1) & 0x55555555); 
    = (n & 0x33333333) + ((n >>> 2) & 0x33333333);         = (n & 0x0f0f0f0f) + ((n >>> 4) & 0x0f0f0f0f);         = (n & 0x00ff00ff) + ((n >>> 8) & 0x00ff00ff);         = (n & 0x0000ffff) + ((n >>> +) & 0x0000ffff);         return N;

Solution Five:

MIT Hackmem

 Public int count5 (int  x) {        int temp = x-(x >>> 1) & 033333333333-(x > >> 2) & 011111111111;         return (temp + (temp >>>3)) & 030707070707%;    }

Solution VI:

Tabular

 Public intStatic4bit (intx) {int[] table = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; intc = 0;  for(; x > 0; x >>>= 4) {C+ = table[x & 0xF]; }        returnC; }    /*** First constructs a table with 256 elements table,table[i] that is the number of 1 in I, where I is a value between [0-255]. * Then for any one 32bit unsigned integer n *, we split it into four 8bit, and then each 8bit in the number of 1, and then add the sum can be, here with the method of shift, each right shift 8 *, and with 0xff phase, get the lowest bit of 8bit *, Add and then continue to shift, so back and forth, until N is 0. So for any 32-bit integer, you need to look up the table 4 times.      Take the decimal number 2882400018 as an example *, its corresponding binary number is 10101011110011011110111100010010 *, corresponding to the four check table process is as follows: Red is the current 8bit, green means the right shift after the high-level 0. * * First time (n & 0xff) 10101011110011011110111100010010 * * Second ((n >> 8) & 0xff) 0000000010101011110 0110111101111 * * Third ((n >>) & 0xff) 00000000000000001010101111001101 * * Fourth time ((n >> 24 ) & 0xff) 00000000000000000000000010101011*/     Public intStatic8bit (intx) {int[] table = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2,                2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3,                4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,                4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2,                3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4,                4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5,                6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,                2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3,                4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5,                5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,                6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5,                4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5,                6, 6, 7, 6, 7, 7, 8,}; intc = 0;  for(; x > 0; x >>>= 8) {C+ = table[x & 0xFF]; }        returnC; }

One of the problem of [algorithm] bit arithmetic

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.