当前位置:网站首页>C语言:不创建临时变量实现两数交换
C语言:不创建临时变量实现两数交换
2022-07-28 03:42:00 【高邮吴少】
先来说一下异或^的原理:对应二进制位,相同为0,相异为1
比如a=3,b=5,对应二进制补码分别为011 101
a= 011
b= 101
a^b= 110
然后我们这里用异或的方法实现两数交换的原理就是
a^a=0,因为你两个数相同,对应位肯定全相同,相同为0嘛
0^b=b,举个例子:
c= 000
b= 101
c^b=101=b
那么通过a ^ a=0 ,0 ^ b=b
我们可以推出a ^ a ^ b=0 ^ b= b
而异或又是满足交换律的(同学们可自行验证)
也就是说a ^ a ^ b=a ^ (a ^ b)= b
b ^ (b ^ a)= a
#include<stdio.h>
//不创建临时变量实现两数交换
int main()
{
int a = 0;
int b = 0;
printf("请输入两个要交换的数:");
scanf("%d %d", &a, &b);
printf("交换前a=%d b=%d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("交换后a=%d b=%d\n", a, b);
//举个例子:a=3,b=5
//正数原反补码均相同
//a补:011
//b补:101
//a=a^b:110
//b=a^b:011
//a=a^b:101
//至此,完成ab两数交换
return 0;
}

边栏推荐
- 【P4】 查看库文件两个历史版本的区别
- ES6 from entry to mastery 07: Deconstruction assignment
- Interface automation test, complete introduction
- 接口自动化测试,完整入门篇
- Golang gets the tag of the loop nested structure
- [openvx] VX for basic use of objects_ matrix
- ES6 from getting started to mastering 08: extended object functions
- Super nice PHP program source code of nteam official website
- Mouse operation and response
- AIRIOT答疑第6期|如何使用二次开发引擎?
猜你喜欢

The open source of "avoiding disease and avoiding medicine" will not go far

20220726 at command test of Bluetooth module hc-05 of Huicheng Technology

695. Maximum area of the island

tensorboard使用记录

Prefix-Tuning: Optimizing Continuous Prompts for Generation

Illustrated: detailed explanation of JVM memory layout

Integrate SSM to realize search of addition, deletion, modification and query
![2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb](/img/24/fbf63272f83b932e0ee953f8d4db75.png)
2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb

Vertical align align the elements in the row are vertically centered
D2dengine edible tutorial (4) -- draw text
随机推荐
服务器内存故障预测居然可以这样做!
玩一玩WolframAlpha计算知识引擎
How to solve MySQL deep paging problem
203. Remove linked list elements
Do you regret doing automated testing?
A treasure simulates login and reduces the method of secondary verification
LabVIEW loads and uses custom symbols in tree control projects
Integrate SSM to realize search of addition, deletion, modification and query
动态规划——1049. 最后一块石头的重量 II
Responsive high-end website template source code Gallery material resource download platform source code
贪心——55. 跳跃游戏
[paper notes] mobile robot autonomous navigation experimental platform based on deep learning
ES6 从入门到精通 # 09:Symbol 类型
[wrong question]mocha and railgun
2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb
数据挖掘-02
Practical scripts of mangopapa (contents)
Airiot Q & A issue 6 | how to use the secondary development engine?
Daily practice ----- realize the lottery function of two-color ball. Rules: Six non repeating numbers are randomly selected from 36 red balls, and one from 15 basketball is randomly selected to form a
贪心——53. 最大子数组和