Exchange two numbers without another variable
来源:互联网 发布:淘宝货到付款钱给谁 编辑:程序博客网 时间:2024/06/09 20:13
Usually we exchange two numbers we need another temp variable ,it is possible to exchange without the temp.We can use bit compute.
Two variables A =1,B=2;
now do these:
A=A^B;//A=0001^0010=0011;
B=B^A;//B=0010^0011=0001(A)
As the same:
A=B^A;
# include <iostream>using namespace std;int main(){int a=1;int b=2;cout<<"Before exchange:"<<"a="<<a<<" , b="<<b<<endl;a=a^b;b=a^b;a=a^b; cout<<"After exchange:"<<"a="<<a<<" , b="<<b<<endl;}
- Exchange two numbers without another variable
- Exchange the values of two variables without using an extra variable
- How to swap two numbers without using temp or third variable
- add two numbers without using +
- swap two values without temporary variable
- exchange two numbers in each line
- Add two numbers without using arithmetic operators
- Divide Two Numbers Without Symbol And Also Minus And Multiply
- Compare two numbers Without if-else and comparison
- Leetcode 503,Add two numbers without using arithmetic operators
- read numbers from file and exchange their positions then write into another file
- Use a variable reference “inside” another variable
- Two Numbers
- Exchange the numbers of row and array of a two-dimensional array, and form a new two-dimensional ar
- To find sum of two numbers without using any operator. Only use of printf() is allowed.
- LeetCode 2.Add Two Numbers & 3.Longest Substring Without Repeating Characters
- SSH to another linux machine without password
- Two Sum & Add Two Numbers
- Win7下Opencv+VS2010的安装
- poj 1470 Closest Common Ancestors(LCA)
- Hibernate程序性能优化的考虑要点
- 计算数组长度
- 【javascript】解决setTimeout不能传参的问题
- Exchange two numbers without another variable
- COCOS2D(4) 使用过渡场景在多个场景的切换
- Java的沙箱机制原理入门
- 第二场热身赛A(简单水题)
- 类对象的方法with和in
- 字符串全排列 C语言实现
- Ruby中的inject
- 关于scanf的一点心得。
- C关键字assert详解