Java one thousand Q: Java-bit computing Classic applications (2)

Continued articles

Third, the intermediate variable exchange without the aid of the value of two variables

Usually, we want to exchange two variables are the following steps:

Java one thousand Q: Java-bit computing Classic applications (2)
This mode of operation is easy to understand, implement key exchange is that the value of the variable intermediate variable c. Now the subject is required to exchange the values ​​of a and b without via an intermediate variable. If the bit is not used in the operation mode, the same can be done without the aid of an intermediate variable exchange two variables, which process is as follows.

Java one thousand Q: Java-bit computing Classic applications (2)
For explanation convenience, we initial values of a and b of the original call and the original a b, line 3 is step 3:
Step 1: the original a b Original added to and stored in a variable, the variable b temporary value has not changed.
Step 2: Subtract with this original and b, then assigned to the variable b, the operation after this step, the variable b on the preservation of the original value of a.
Step 3: The original a, b and the sum of a value obtained by subtracting the original, the original B is obtained, and to store the result into variable a.
Step 3 above is achieved by the exchange of a, b values of two variables without the aid of the intermediate variable. Although this algorithm does not via the intermediate variables, but there is a problem is the maximum value of the sum of two type int and beyond, if a and b are a large number, making the operation time of the first step is likely to occur, out occurs, leading to the back of the operation all wrong.
And we realize exchange bit operation mode, this problem does not exist. Specific code as follows:

Java one thousand Q: Java-bit computing Classic applications (2)
Before about this code, let's review a calculation rule that: A ^ b ^ b calculation result is equal to A . Operation rules doubt this, read " the Java one thousand Q: Java language Bitwise operators explain ," a text. In order to facilitate the presentation, we have referred to the operation of a ^ b "with a pair of encryption b ', are so called, because a and b are carried out after the XOR operation, a new value is obtained, the effect of a encrypted as same. And a ^ b ^ b the action called "restore", so called reason, because the calculation result a ^ b ^ b is equal to a, the following value is as "encryption" a, but also a reduction performed, restore the value of a.
Again we call a and b the initial value is called a primitive and original b. This is how the three lines of code to realize the value of a variable exchange it? We explain row:
the first line of code: a pair of cryptographic operations performed by b, and assigned to the variable, and a, at this time becomes a variable value encrypted by the original data.
The second line: the value of the original encrypted XOR b, to restore the original values of a, we have the value assigned to b, so that the variable b is stored in a value of the original.
Code Line 3: The encrypted value b with the current, which is a variable stored original XOR operation, the original value of b can be obtained, then the original value of b is then assigned to a variable, so that complete exchange of the variables a and b values.

Fourth, find out the single element dogs

The so-called dog to find a single element refers to: an array, a number appears only once, while others have suffered twice the number required to write a program that only appears once the number to find out. The subject of the most complete core principle is: two identical numbers XOR result is 0, and any integer a XOR operation with 0, the result is the number itself. Further, any of the N integers XOR operation, commutative .
According to this idea, we just need to array all the elements to do it again XOR operation, the resulting value is that there was only one number. Because the number appears twice, exclusive or operation between them becomes zero. Even if the two figures are not next to each other, but according to the commutative law XOR we can know: the positional relationship does not affect the result of the operation, so long as the same two numbers are involved in the XOR operation, the end result is 0 . And that number only appears once, XOR operation with 0, the result is still its own value. Therefore, the result of the exclusive OR operation is that only appears once in figures, according to the results of our exclusive OR operation, will be able to find the "single dog" element of. Specific codes are as follows:

Java one thousand Q: Java-bit computing Classic applications (2)
This question has another version, the title is described as: there is an array of integers a and b all the elements of the array, a b appear in the array, but the array than a b a multi-element array, the array write a program to find b more out of this element. Although the array into two, but the idea of ​​the algorithm has not changed, but an array of all the elements involved in the original XOR operation into two elements in the array should participate in XOR realization of the code as follows:

Java one thousand Q: Java-bit computing Classic applications (2)

(To be continued ...)
If you want to learn Java programming system, and welcome to my site in the video lessons.

Guess you like

Origin blog.51cto.com/2266836/2471727