Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways in which Java does not use third-party variables to exchange the values of two variables

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the method that Java does not use third-party variables to exchange the values of two variables". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Question: name several ways to exchange the values of two variables without using third-party variables.

When faced with the problem of exchanging the value of a variable, what we usually do is to define a new variable and use it to complete the exchange.

The code is as follows:

T = a Tian a = b; b = t

But the crux of the problem is "do not use third-party variables", which becomes "cute". After thinking about it, come up with the following four ways to solve the problem:

The variable itself exchanges values.

Arithmetic operation

Pointer address operation

Bit operation

The variable itself exchanges values.

B = (a + b)-(a = b)

First perform the a + b operation, and then assign b to a, then b = a + b-b = a, which completes the interchange operation of ab.

Arithmetic operation

As shown in the figure:

OA = a

OB = b

AB = b-a

First, we assign the distance b-a between AB to a, where AB = a, OB = b.

In order to achieve the purpose of ab exchange, OA should be equal to b, and the distance of OA is b-a, so b-a has to be assigned to b, where OA = b, AB = a.

It is easy to see from the figure that the distance of OB is b + a, so we only need to assign b + a to a to exchange the two.

To sum up, our steps are

Int a = 10 | int baked 15 * a = b-a; / / baked 15 * 5 * b = b-a; / / baked 10 *

This algorithm can only be used for integer types.

Pointer address operation

We can think of an and b as address values in memory, assuming that an is 0x01ff5e70, b is 0x01ff5e90, and b-a represents how many bytes apart the two variables are stored in memory. So theoretically we can also exchange the values of two variables according to the logic of arithmetic operation.

The code is as follows (in this case, the c language)

/ / where an and b are pointer variables, in which the addresses int* a = new int (10) of 10 and 20 are stored; / / a=0x01ff5e70, which represents the address int* b = new int (20) stored in a; / / b=0x01ff5e90, where the address / / pointer variable stored in b is subtracted by how many bytes between the addresses of 20 and 10, and then converted to the pointer variable a = (int*) (bmera); / / b=0x01ff5e90 A=0x8b = (int*) (bmura); / / bong0x01ff5e70posia0x8a = (int*) (b+long (a)); / / bicon0x01ff5e70transfera0x01ff5e90

B-a = 0x01ff5e90-0x01ff5e70 = 0x20Jing 0x20 is converted to decimal to 32 bits, because an int occupies 4 bits, so this is 0x8.

The above is only the execution process in the theoretical state, and the exchange can not be realized if it is executed directly. Because the above code ignores a problem: after the code is compiled, the variables are stored in memory, and the memory area has a base address.

The base address can be understood as the starting point of a block of memory. The above data are offset on the basis of the base address.

Address of variable = base address of variable + offset address of variable

When we do the b-an operation, the result is 8, and then when it is converted to a pointer variable, the base address is automatically added to 8, and the result is not 0x8, so it will lead to an error.

In addition, the address operation can not have a negative number, that is, when the address of an is greater than the address of b, b-a < 0, the system automatically uses the form of complement to represent the negative displacement, which will also produce errors.

In order to solve this problem, we only need to ensure that the results obtained by b-an are not affected by the base address, so the following solutions are given.

Int * a = new int (10); int * b = new int (20); cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report