首页 > 其他分享 >Swap two variables without intermediate variable

Swap two variables without intermediate variable

时间:2022-11-09 01:33:23浏览次数:44  
标签:10b variables two without intermediate variable

 

# swap two variables without intermediate variable, but intermediate variable is applicable to various situations(even with complex objects)
# the two methods below only applicable to numerics

# solution 1
a = 10
b = 8

a = a + b
b = a - b
a = a - b

print(f'{a=!r}\t{b=!r}')

# solution 2 exclusive OR
a = 10
b = 8

a = a ^ b # hold info about different bits of the two numbers
b = a ^ b
a = a ^ b
print(f'{a=!r}\t{b=!r}')

标签:10b,variables,two,without,intermediate,variable
From: https://www.cnblogs.com/dissipate/p/16871855.html

相关文章