Bit computing on some tips

Bit operation on some issues always play some unexpected advantage, but not because they have some understanding for the coder, bit computing to solve the problem is relatively obscure;

I follow the use of functions as we compiled some tips for your reference, usually without too much get to the bottom.

Useful features three hundred forty-five ~~ 

 

First, the 26 English characters uppercase lowercase turn, turn uppercase lowercase, invert case.

     (1) small letter in uppercase: ( 'a' | '') = 'a'; ( 'A' | '') = 'a';

     (2) Lower capital transfer: ( 'a' & '_') = 'A'; ( 'A' & '_') = 'A';

     (3) invert case: ( 'a' ^ '') = 'A'; ( 'A' ^ '') = 'a';

     This is not very useful, very simple principle, that is, through the corresponding ascii code conversion.

 

Second, without the use of third variable exchange values of two variables.

     int a = 1, b = 2;

     a = a ^ b    

     b = a ^ b    

     a = a ^ b

     // a, b = 2, 1

    This method seems not much practical use, the interview may be used, with the python in a, b = b, a reference exchange are considered resolved.

 

Third, it determines whether the number of two different signs .

     int  x = -1, y = 2;

     bool f = ((x ^ y) < 0; //true

     int x = 1, y = 3;

     bool f = ((x ^ y) < 0; //false

     This comparative conventional determination method is quite easy. More recommended small partners a note.

 

Four, n & (n - 1) usage.

     Effect of this operation is to eliminate the number  n  of the binary representation of the last  one.

     For binary Liezi n is 0. 1 0 0 0 0 . 1 0 0 0  

                     After n-1 becomes 0 0 0 0 1 0 0 1 1 1     (above n From low to high first number is not zero by 1, there is the following this case, they want it)

                     After bitwise 0000010000 (Comparative found to eliminate the last n 1)

     After understand the role it spawned some amazing applications.

     (1) determining a number index is not 2 ( both number greater than 0 ).

          Because if a number is a power of two, then it will definitely be the binary representation of only a 1 exist, such as 2 ** 3 == 8 (0000 0100), this is not difficult to understand because the index is every bit 2.

          This can be used directly n & (n-1) to eliminate only a 1, it is determined whether to 0

          16 & e.g. (16--1) == 0 == // 16 4 2 **

 

V. Use ^ (XOR) array is determined only once a number ( this number unique, and other numbers appear even number )

       This is because the use of a separate write 136 questions appear only once on leetcode been fully applied in the digital ~

       nums = [2,5,2,3,3,5,3,3,4,7,7]

       【Java】

  static int singleNumber public (int [] the nums) { 
         int NUM = 0; 
         for (int I = 0; I <nums.length; I ++) { 
             NUM = [I] ^ NUM the nums; 
         } 
         return NUM; 
     } 

[] Python
return reduce (int .__ xor__, nums)

believe that some of the students at the beginning is not easy to think this way, then even if we do not have to know why this line of thought, Why XOR can be achieved.
Because the exclusive-OR operation some regularity, to meet the commutativity and associativity .
Common law: X = X ^ 0;
X ^ X = 0;
binding and commutative:
A ^ A ^ B ^ C ^ C ^ D ^ D == (A ^ A) ^ (C ^ C) ^ (D ^ d) ^ b == 0 ^ 0 ^ 0 ^ b == b


                     

Guess you like

Origin www.cnblogs.com/wikis/p/11321798.html