- XOR (^)
Operation rule: \ (1,0 \ rightarrow. 1 \) can be loosely understood as not to carry adder
XOR properties: \ (A \) ^ \ (A = 0 \)
\(A\)^\(0=A\)
Example : There are several twenty-two repeated, except that a single chatter, do not open up extra space, find the number.
Idea : let within an array of all the number of exclusive-or operation , the end result is the single number.
- versus(&)
Operation rule: \ (1, 1 \ rightarrow1 \)
Example ① : seeking a number of binary 1
Ideas :
1. The number and the operation has to >> 1, +1 will be true for the number.
2 has been carried out with the number of -1 the number of the operation (starting from the lower rear -1 to 1 between a first of all are 0 to 1, the first 1 to 0, so that the initial number and it It can be the first to 0), until this number is equal to 0, count the number.
Example ② : to determine whether a number is a power of two.
Idea : let this be an integer number 2, then this number is only a binary 1. ① can be used with example 2 the idea to solve, so that the number and the value -1 once the operation to determine whether it is 0.
- The binary parity exchange
Ideas : The number of each and the \ (0xAAAAAA \) (1010 ... 1010 1010 removed even bit ) \ (0x555555 \) (0101 ... 0101 0101 with the odd bits ) for AND (&) operation. Then even bit reserved the right one, reserved odd bits left one, then XORed (^) operation.
- Floating-point number represents between 0-1 (x 2, move the whole)
Method : whole number \ (* 2 \) , taken integer bits (from left to right arrangement), then \ (* 2 \) , up to \ (0 \) .
For example: \ (0.625 \)
- \ (0.625 * 1.25 = 2 \) , remove the \ (1 \) , then the binary number \ (0.1 \) , the number becomes \ (0.25 \)
- \ (2 * 0.25 = 0.5 \) , taken 0, the binary number becomes \ (0.10 \) , the number becomes \ (0.5 \)
- \ (0.5 * 2 = 1 \) , taken out, and the metal becomes \ (0.101 \) , the number becomes \ (0 \) , the end of cycle
- There are a number appears \ (k \) times, a number appears \ (1 \) pieces, and appeared \ (1 \) the number of times
Tips : \ (K \) a \ (K \) binary number as not binary addition operation result is 0
Methods: With Tips So this question should be thinking it clear, first of all, put all the digital conversion is \ (k \) decimal friends, then put each number corresponds to the sum, and \ (k \ ) to take over, and then put together is the answer spicy!