仅使用两个变量交换两个数字 [英] Swapping two numbers using only two variables

查看:94
本文介绍了仅使用两个变量交换两个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它如何执行交换?


a = a + b

a=a+b

b = a + b

b=a+b

a = b + a

a=b+a

I

书籍选项包括 a和b的值的补码,取反和b。希望这些选项也不令人满意?

The book options include "complements of values of a and b" ,"negate and b".Hope these options aren't satisfying it too???

推荐答案

交换使用XOR执行,通常在XOR中用加号表示一个圆圈例如:

The swap is performed using XOR, which is typically written as a plus within a circle; for example:

a := 5
b := 7

a := a xor b (2)
b := a xor b (5)
a := b xor a (7)

这篇关于仅使用两个变量交换两个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆