"Java" does not use a third-party variable to exchange values of two variables

Source: Internet
Author: User

When it comes to language learning and programming, the most common way to exchange two variables is by using the new variable temp, the following code:

-----------------------------------------

<--Standard Algorithm--

-----------------------------------------

int a = 3, b = 4;

int temp = A;

A = b;

b = temp;

-----------------------------------------

This method is easy to understand, especially suitable for beginners to understand the characteristics of computer programs, is the classic application of assignment statements, in the actual development, this method is simple and clear, will not produce ambiguity, easy to communicate.

But the disadvantage of this algorithm is the need to use a third-party variable-temporary variable, the efficiency is not very high.

-----------------------------------------

Here's how to convert without a temporary variable

1. Arithmetic algorithm

In simple terms, it is the normal + and-operation to implement the code as follows:

-----------------------------------------

int a = 3, b = 4;

A = A+b;

b = a A;

A = a-B;

-----------------------------------------

Arithmetic principle is: The A, b as a point on the axis, around the distance between two points to calculate.

Specific process:

The first sentence "A = a+b" to find the sum of the distance AB two points and save it in A;

The second sentence, "B = A-a", evaluates the distance from a to the origin and saves it in B;

The third sentence, "A = A-B", evaluates the distance from the origin to the point and saves it in a.

Complete the interchange.

Compared with the standard algorithm, this algorithm has more than three computational processes, but no temporary variables

2, XOR or algorithm

The exchange of variables can also be achieved through XOR or operation, which is perhaps the most magical of all, see the following code:

-----------------------------------------

int a = 3, b = 4;

A = A^b;

b = a^b;

A = A^b;

-----------------------------------------

This algorithm can be implemented by the characteristics of the XOR operation is determined by the XOR operation can make some bits in the data flip, the other bits unchanged. This means that any number is continuously different or two times with any given value, and the value does not change. That

A^b^b=a. The a=a^b into the b=a^b is b=a^b^b=a, the same can be achieved a=b^a^a=b; Easy to complete the exchange.

The greatest benefit of XOR operation is that the binary data operation is carried out directly, and the time efficiency of the conversion is greatly saved.

-----------------------------------------

Two words of nagging:

The small skills in mathematics have a considerable impact on program design, and the use of them will have unintended effects.

"Java" does not use a third-party variable to exchange values of two variables

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.