Exchange data in C language-something you can think.

Source: Internet
Author: User

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"

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.