Science, asked by jgzg3066, 11 months ago

Please give an example for: Switch the integer values stored in two registers without using any additional memory?

Answers

Answered by FlynnXD
0
Say, the two "registers" storing one integer value each, are under the name "a" and "b", by a series of operations, it is possible to swap their values without creation of a new register/additional memory.
The operations are as shown below:
a=(a+b)
b=(a-b)
a=(a-b)

"a" and "b" under the operations mean that the corresponding integer values are used to compute the set of operations.
Let us see how it works by using numerical values, say a=5 and b=7.
a=a+b => a=5+7=12
b=a-b => b=12-7=5
a=a-b => a=12-5=7

Thus, now a=7 and b=5 and we have successfully swapped their values.
Similar questions