Bit operation skills in ACM

I heard that bit arithmetic is fun, so this section summarizes the bit arithmetic techniques that may be used in ACM. ( May be updated )

XOR operation is extremely important! ! (Over [LC136] (number that appears only once-LeetCode ): each number in the array appears twice, only one appears once, and the number that appears once is found)

1. If we XOR the 0 and the binary bit, we still get the binary bit
a⊕0 = a
2. If we XOR the same binary bit, the returned result is 0
a⊕a = 0
3. XOR satisfies the exchange law and the combination law
a⊕b⊕a = (a⊕a) ⊕b = 0⊕b = b

<< One time is equivalent to * 2, >> One time is equivalent to / 2

use:

  1. Judging odd and even numbers (number & 1 is 1 odd number; 0 even number)
  2. Get binary is 1 or 0 (& operation)
  3. Swap the positions of two integer variables
  4. No need to judge the sentence, find the absolute value of integer

 

Guess you like

Origin www.cnblogs.com/Black-treex/p/12677501.html