Please give an example for: Switch the integer values stored in two registers without using any additional memory?
Answers
Answered by
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.
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
Social Sciences,
7 months ago
Math,
7 months ago
Computer Science,
7 months ago
Biology,
1 year ago
Political Science,
1 year ago