C language learning process from the exchange of two variable values

Source: Internet
Author: User

At the beginning you use this method
int tmp;tmp = A;a = B;b = tmp;

Then you know the modular programming, you know this is not good, so you start to use the function.
void swap (int *a, int *b); {int tmp;tmp = *a;*a = *b;*b =tmp;} I'm not going to write a function down here.

With the C language learning, later you find that the following code can also complete the value of two variables to exchange any
x = x +y;y = x-y;x = XY;

Then one day, you find that your code does not always work correctly, because the value of X + y may be the same.
Similar to the division and multiplication is not also possible to carry out such exchange can
x = x * Y;y = X/y;x = × X/y;

Smart you'll soon find out that there are two bugs hidden here. If X*y = 0 What to do, x*y overflow out what to do, that is, as long as one of them is 0 can not be processed, the program will be divided by 0 and abnormal exit.
And then you saw a bunch of code.
x = x^y;y = X^y;x = X^y;

Others say it can also exchange values, you do not believe, what is this thing, XOR operation how to exchange two number of values? What's the principle?
Here are a few of the rules of XOR or operation:
1. Any number and 1 XOR result is the inverse of this number and vice versa.
For example:
x = x ^ 1;//equivalent The following statement x = ~x; the principle is as follows: 1 = 1 ^ 0; 1 = 0 ^ 1;0 = 1 ^ 1;

2. The result of any number with the 0 shipping algorithm is he itself, and vice versa.
For example:
x = x ^ 0;//equivalent The following statement x = x; the principle is as follows: 0 = 0 ^ 0; 1 = 0 ^ 1;1 = 1 ^ 0;

3. A number XOR or his own result is always 0.
To deepen understanding, let's write a small program:
/*************************************************************************    > File name:test.c    > Author: Silly Lee    > Mail: [email protected]     > Created time:2014 November 23 Sunday 11:50 03 seconds *************************** /#include <stdio.h>int main () {int x =3,y =4;int t;printf ("(%d,%d) \ n ", x, y); t = × ^ y;x = x ^ t;y = x ^ t;printf (" (%d,%d) \ n ", x, y); return 0;}

int x = 3;int y = 4;int tmp;tmp = x ^ y;y = t ^ y;//y = (x ^ y) ^ y  ->>  y = x ^ y ^ y  ->> y = x ^ 0; ->> y = x;//at this time the value of Y has changed to xx = t ^ x; x = (x ^ y) ^ x; ->> x= x ^ x ^; x = y; Enter values to operate again: TMP = 3 ^ 4;y = tmp ^ y = (3 ^ 4) ^ 4 = 3;x = tmp ^ x = (3 ^ 4) ^ 3 = 4;

Oh, tmp = x + y; Does it look like a macro? You see, he's just an equivalent replacement. There are indeed subtle similarities.
Now it's not hard to understand the code below.
x = x ^ y;y = x ^ y;x = x ^ x;
So the following code is not difficult to understand:
A ^= b ^= a ^= b;//do not write this code in order to prevent it from being killed by a stick.

Seems to be here, you have mastered the exchange of two variables of the various methods, in fact, the road is still far away, the above is just an example of exchanging an integer, encountered ASCII character is OK to say, but if you encounter a floating point, how to do? Do you want to re-write a function, the next thing we have to learn is generic programming, only in this way, we write a program to status quo. Our journey has just begun!!!

C language learning process from the exchange of two variable values

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.