当前位置:网站首页>C language: realize the exchange of two numbers without creating temporary variables
C language: realize the exchange of two numbers without creating temporary variables
2022-07-28 03:47:00 【Gao You Wu Shao】
First, let's talk about XOR ^ Principle : Corresponding binary bit , Same as 0, Dissimilarity is 1
such as a=3,b=5, The corresponding binary complements are 011 101
a= 011
b= 101
a^b= 110
Then the principle of realizing the exchange of two numbers by XOR here is
a^a=0, Because your two numbers are the same , The corresponding bits must be all the same , Same as 0 Well
0^b=b, for instance :
c= 000
b= 101
c^b=101=b
Then through the a ^ a=0 ,0 ^ b=b
We can derive a ^ a ^ b=0 ^ b= b
Different or satisfying the commutative law ( Students can verify by themselves )
in other words a ^ a ^ b=a ^ (a ^ b)= b
b ^ (b ^ a)= a
#include<stdio.h>
// Do not create temporary variables to exchange two numbers
int main()
{
int a = 0;
int b = 0;
printf(" Please enter two numbers to exchange :");
scanf("%d %d", &a, &b);
printf(" Exchange before a=%d b=%d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf(" After exchanging a=%d b=%d\n", a, b);
// for instance :a=3,b=5
// Positive numbers have the same original and inverse complement
//a repair :011
//b repair :101
//a=a^b:110
//b=a^b:011
//a=a^b:101
// thus , complete ab Two number exchange
return 0;
}

边栏推荐
- MangoPapa 的实用小脚本(目录篇)
- What is tor? What is the use of tor browser update?
- 2022 summary of the latest Android handler related interview questions
- test case management tool
- Greed 45. Jumping game II
- Build an "industrial brain" and improve the park's operation, management and service capabilities with "digitalization"!
- 动态规划——1049. 最后一块石头的重量 II
- 高等数学(第七版)同济大学 习题3-4 个人解答(前8题)
- [openvx] VX for basic use of objects_ lut
- Integrate SSM to realize search of addition, deletion, modification and query
猜你喜欢

Unity simply implements the dialog function
![[paper notes] mobile robot autonomous navigation experimental platform based on deep learning](/img/6a/7f0c2b2a53332636f3172bc3b0b74d.png)
[paper notes] mobile robot autonomous navigation experimental platform based on deep learning

Data mining-02

构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!
![[prototype and prototype chain] get to know prototype and prototype chain~](/img/8a/d6362fdd50dc883ff817a997ab9e1e.png)
[prototype and prototype chain] get to know prototype and prototype chain~

Leetcode58. 最后一个单词的长度

WordPress simple mkblog blog theme template v2.1

test case management tool
![[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix](/img/6c/b5fd3350886fd74557439f5361e7f8.png)
[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix

Unity backpack system
随机推荐
Common weak network testing tools
[openvx] VX for basic use of objects_ lut
Practical scripts of mangopapa (contents)
Appnium -- app automated test tool
Leetcode58. 最后一个单词的长度
Collection | 0 basic open source data visualization platform flyfish large screen development guide
【OPENVX】对象基本使用之vx_matrix
动态规划——509. 斐波那契数
[openvx] VX for basic use of objects_ convolution
贪心——53. 最大子数组和
数据挖掘-01
收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南
Greed 122. The best time to buy and sell stocks II
【OPENVX】对象基本使用之vx_pyramid
静态博客搭建工具汇总
高等数学(第七版)同济大学 习题3-6 个人解答
【OPENVX】对象基本使用之vx_convolution
deepstream 检测结果截图
ES6 from entry to mastery 07: Deconstruction assignment
《剑指offer》| 刷题小记