当前位置:首页 > 技术心得 > 正文内容

C语言交换两个数

xjtudll15年前 (2011-05-13)技术心得10660

可能大家都认为交换两个数如此的简单,没必要写,没新意。例如,交换a和b两个数,教科书上一直这样写:
temp=b;
a=temp;
b=a;

确实很简单。但是最近看到一段代码,觉得十分高超,是这样的:
a^=b;
b^=a;
a^=b;

同样也是三句代码,但是却未使用局部变量,巧妙吧。

值得注意的是,只有满足以下条件,才能使用上述代码

1、a,b均为无符号数
2、a,b长度一致

这个在单片机中还是有作用的哦,尤其是RAM不足的情况下~~~~

扫描二维码推送至手机访问。

版权声明:本文由鸟的天空发布,如需转载请注明出处。

本文链接:http://xjtudll.cn/Exp/177/

标签: C
分享给朋友:

“C语言交换两个数” 的相关文章

金蝶K/3修改帐套启用期间

不要乱修改 select * from t_SystemProfile where fkey = 'startyear' select * from t_SystemProfile where fkey = 'startperiod' 如果一个帐套已使用,要再修改会计期间个数或启用期间或会计期间开始...

静电实验平台的搭建及要求

静电实验平台的搭建及要求

Test setup 1、Cables: 2m, with 470KΩ * 2 ---EUT and Metal HCP (Horizontal Coupling Plate); ---HCP and Metal Plate connected to the ground 2、Insu...

source insight导入配置文件

source insight导入配置文件

Source insight的配置文件默认路径如下:\Documents\Source Insight\Settings 中文版本的在: \我的文档\Source Insight\设置 option -- load configuration可以选择source insight使用的配置文件。 &...

C#里面如何判断一个Object是否是某种类型

第一种方法 var isA = oldObject.GetType() == typeof(Dictionary<string, string>) 第二种方法 var isB = oldObject.GetType().FullName.IndexOf("Dictionary&...

NPOI导出Excel表 如何实现短日期格式

https://q.cnblogs.com/q/43888/ 问题: 因为导出EXCEL 档日期格式永远都是 2012-11-22 14:51:11 ,而我们希望结果永远只是 2012-11-22, 我修改完的代码如下: case "System.DateTime": //...

android bluetoothGatt  :onClientRegistered() - status=133 clientIf=0

android bluetoothGatt :onClientRegistered() - status=133 clientIf=0

参考资料: http://stackoverflow.com/questions/25330938/android-bluetoothgatt-status-133-register-callback http://stackoverflow.com/questions/28894111/andro...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。