My experiences on how to exchange two numbers in C Language

Source: Internet
Author: User
Tags bitwise operators

My experiences on how to exchange two numbers in C Language

There are currently five methods for exchanging two numbers in C:

1: Exchange Directly Using intermediate variables;

2: the pointer is used to pass in the function address and exchange the intermediate variable inside the function;

3: Perform addition and subtraction operations on the two numbers to be exchanged;

Note: Because the C language has floating point numbers, it cannot be considered multiplication, division, or exchange.

4: two numbers can be exchanged or computed;

5: bitwise operations can be performed on two numbers, or two numbers can be exchanged.

 

Next, I will analyze the following three types:

The first two are too common and are not described here:

Third: addition, subtraction, and exchange

Assign the sum of two numbers to the first number, then use the new first number minus the second number to obtain the old first number, and assign the value to the second number.

And then use the new first number minus the new second number (the old first number) to obtain the old second number and assign it to the first number.

The code for exchanging two numbers is as follows:

 1 #include<stdio.h> 2  3 int main(){ 4     int a=10,b=20; 5     a+=b; 6     b=a-b; 7     a=a-b; 8  9     return 0;10 }

Note: Defects in this method

When the two numbers to be exchanged point to the same underlying array element, this method is invalid.

Please refer:

 

1 # include <stdio. h> 2 3 int main () {4 int a [5] = {1, 2, 3, 4, 5}; 5 int I = 2, j = 2; 6 a [I] + = a [j]; // because I = j; a [I] = a [j] = 2 [I]; 7 a [j] = a [I]-a [j]; // from the upper point, a [I] = a [j] = 0; 8 a [I] = a [I]-a [j]; // both are 0; exchange failed
9 return 0; 10}

In addition, when using this method, you should note that the sum of the two numbers cannot exceed the maximum number expressed by the defined type, otherwise the exchange will fail.

  

 

Type 4: XOR operation for exchange

Here, I will first talk about the principle of the exclusive or operation: two numbers perform an exclusive or operation, convert the two numbers into binary values, compare them by bit, get 0 for the same person, and get 1 for different people; details are as follows:

0 1 0 1 1 0 0 1

1 0 0 1 0 1 0 0

1 1 0 0 1 0 1 1 1 1 1

 

0 1 0 1 1 0 0 1

The Code is as follows:

1 0 0 1 0 0 0 0 0

 

 1 #include<stdio.h> 2  3 int main(){ 4     int a = 10,b = 20; 5     b ^= a; 6     a ^= b; 7     b ^= a; 8      9     return 0;10 } 

Note: Like the previous method, this method is also subject to the same restrictions when using arrays, but you only need to make one before using the array to determine whether it points to a different number,

So I think this method should be the simplest and quickest.

Fifth: bitwise operations for switching

This method is also Binary-based.

First, let us know two bitwise operators:> the arrow is pointed to the right, so the right shift operator <the arrow is pointed to the left, so the Left Shift Operator

For example, 5> 1 indicates that 5 is shifted to the right by one digit --> that is, the binary value of 5 is 0 0 0 0 1 0 1 0 1 0 0 0 0 at the end of the original 1 removed, 0 is added for high positions; the same is true for left shifts.

Note: Because the right side is the cursor bit of the data, after the right shift, the Left shift cannot be restored to the original number, but the left shift is different, and the rest of the Left is 0, they all exist.

If the number of characters is small enough, the original number can be restored by right shift after the Left shift.

When you want to use bitwise operations to swap two numbers, you should first make a simple estimation of the actual binary digits of each number, and then determine the number of moving digits. Below is an example of switching two numbers

1 # include <stdio. h> 2 3 int main () {4 int a = 5, B = 10; 5 a <= 8; // move a to the high position 6 a + = B; // Add the value of B to the low position after a is moved. 7 B = a> 8; // obtain the original value of a. 8 a = a & 0xff; // take the newly moved B. The high position is directly assigned 0 9 10 return 0; 11} with the operation value}

Note: The & operator Operation Principle in the above Code is similar to or. If both of them are 1, the value is 1; otherwise, the value is 0;

When two numbers are exchanged using this method, the size of the two numbers is more limited than that of the third method, and the number of shifted digits is hard to determine. We do not recommend using this method.

 


For the first time I wrote a blog, I know there must be a lot of problems. I hope you can give me a lot of advice. Thank you!

 

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.