The first thought of exchanging data is to use intermediate variables.
Think about addition. If you think about it again, there may be an address or something like that, but have you ever thought about it?
I believe there are 50% people who want to add
We can only think of 1% people who will overflow with addition...
I really didn't think it was lucky to see it, haha
View plaincopy to clipboardprint?
/*************************************** *********************************/
/* Purpose: learn how to swith number with most efficient */
/* Contact: guozhengqian0825@126.com */
/* Data: 2011.08.22 */
/* Author: qianguozheng (Qian guozheng )*/
/*************************************** *********************************/
# Include "stdio. h"
Void main ()
{
Int a = 10;
Int B = 20;
Printf ("before switch: a = % d, B = % d \ n", a, B );
/* Method one: is the best one! */
A = a ^ B; // a = 00001010
B = a ^ B; // B = 00010100
A = a ^ B;
Printf ("after switch: a = % d, B = % d \ n", a, B );
/*
A = 00001010
B = 00010100
A = a ^ B = 00011110
B = 00010100
B = a ^ B = 00001010 = 10
A = 00011110
A = a ^ B = 00010100 = 20
*/
/* Method two */
/* Compare to method one, the shortback is that:
When a is the max of the type and
B is the max of the typw two
It will full overflow (overflow)
*/
A = a + B;
B = a-B;
B = a-B;
Printf ("after switch: a = % d, B = % d \ n", a, B );
/* Method three */
Int tmp;
Tmp =;
A = B;
B = tmp;
/* This is the normal, anyone who is called programme can do that */
/* I will not show others to you, just remember the first one is enough */
}
/*************************************** *********************************/
/* Purpose: learn how to swith number with most efficient */
/* Contact: guozhengqian0825@126.com */
/* Data: 2011.08.22 */
/* Author: qianguozheng (Qian guozheng )*/
/*************************************** *********************************/
# Include "stdio. h"
Void main ()
{
Int a = 10;
Int B = 20;
Printf ("before switch: a = % d, B = % d \ n", a, B );
/* Method one: is the best one! */
A = a ^ B; // a = 00001010
B = a ^ B; // B = 00010100
A = a ^ B;
Printf ("after switch: a = % d, B = % d \ n", a, B );
/*
A = 00001010
B = 00010100
A = a ^ B = 00011110
B = 00010100
B = a ^ B = 00001010 = 10
A = 00011110
A = a ^ B = 00010100 = 20
*/
/* Method two */
/* Compare to method one, the shortback is that:
When a is the max of the type and
B is the max of the typw two
It will full overflow (overflow)
*/
A = a + B;
B = a-B;
B = a-B;
Printf ("after switch: a = % d, B = % d \ n", a, B );
/* Method three */
Int tmp;
Tmp =;
A = B;
B = tmp;
/* This is the normal, anyone who is called programme can do that */
/* I will not show others to you, just remember the first one is enough */
}
This example is the simplest, but it is very important to reflect the truth. After thinking about a solution to a problem, we are excited, I forgot to optimize this solution,
Even if you think about it a little bit, these discoveries are actually very simple, but they are often neglected by stupid people .....
This article is from "Qian guozheng's column"