C Language_Operator Actual Combat_Several Use of Operators to Solve Problems (Explanation)

1. Exchange two numbers without creating temporary variables

Enter the value of a and b here, you cannot create a temporary variable tmp to exchange

The first:


#include<stdio.h>
int main()
{
    
    
	int a, b = 0;
	scanf("%d%d", &a, &b);
	a = a + b;
	b = a - b;//此时b=a+b-b=a
	a = a - b;//此时a=a+b-a=b实现了交换a ,b的目的
	printf("a=%d b=%d",a,b);
	return 0;
}

Insert picture description here
(Note that there is a problem with this algorithm, because when the numbers of a and b are large, the value of a+b may exceed the range of int type shaping, and the value of a+b will be lost)

The second type:

#include<stdio.h>
int main()
{
    
    
	int a, b = 0;
	scanf("%d%d", &a, &b);
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
	printf("a=%d b=%d",a,b);
	return 0;
}

Insert picture description here
Here is an example with a=-1, b=4

Insert picture description here
Insert picture description here
Insert picture description here
Achieve exchange

2. Find the number of digits 1 in binary

Question source

method 1:

The number %2 gets the binary digit, and then divides the number by 2 to find the next digit,

Until the last digit is 0 after /2, the loop will stop if false

#include<stdio.h>
int main()
{
    
    
	int count = 0;
	int n = 0;
	scanf("%d",&n);
	while (n)
	{
    
    
		if (n % 2 == 1)
			count++;
		n = n / 2;
	}
	printf("二进制位中1的个数为%d",count);
	return 0;
}

Insert picture description here

But there is a flaw here, the binary digits of negative numbers cannot be calculated

Insert picture description here
Negative number %2 will not execute count++ if the remainder is -1

Method 2:

We know the & operator

In binary, 1&0=0, only 1&1=1

1 Stored in the computer is 000000000...1

So we will enter the binary number &1

If you get 1, it means that the last bit of the binary bit is 1, and then the variable >>1

To check whether the next binary digit is 0, a total of 32 times.

#include<stdio.h>
int main()
{
    
    
	int a = 0;
	scanf("%d",&a);
	int count = 0;
	for (int i = 0; i < 32; i++)
	{
    
    
		int j = 1;
		if ((a & j) == 1)
		{
    
    
			count++;
		}
		a = a >> 1;
	}
	printf("二进制中1的个数%d",count);
	return 0;
}

Insert picture description here

At this time, the number of negative binary bits 1 can also be calculated

Method three:

Use two adjacent data for bitwise AND operation

#include<stdio.h>
int main()
{
    
    
	int a = 0;
	scanf("%d",&a);
	int count = 0;
	while (a)
	{
    
    
		a = a & (a - 1);
		count++;
	}
	printf("二进制位为1的个数为%d",count);
	return 0;
}

Insert picture description here
Insert picture description here

When a=3

The binary digit of a is 0 1 1
a-1 The binary is 0 1 0
--------a&a-1 is 0 1 0 (count=1)

After that, the binary digit of a is 0 1 0
------the binary of a-1 is 0 0 1
--------------a&a-1 is 0 0 0 (count=2)
At this time a=0 is a false jump out of the loop

When there is more than one 1 in a binary bit, a&(a-1) must not be 0, because the same binary bit must exist.

When the last bit is 1, the last bit of a&a-1 becomes 0, assigning it to a is equivalent to moving a bit.
When the last bit is 0, the last bit of a&a-1 is 0, and assigning it to a is equivalent to discarding A

When there is only one 1 in the binary digit, a&a-1 must be 0, and the count increases by 1 and leaves the loop. The final count is the number of binary 1 in a

3. Find the number of different numbers in two binary digits

Question source

First of all, we know that there are two situations for binary digits, 0 and 1

We first use the exclusive OR operation to change the different numbers into 1 and store them in the variable c

In calculating the number of binary 1s in the variable c, the number of binary 1s will not be repeated here.

#include<stdio.h>
int main()
{
    
    
	int a = 0;
	int b = 0;
	scanf("%d%d",&a,&b);
	int c = a ^ b;
	int count = 0;
	for (int i = 0; i < 32; i++)
	{
    
    
		int j = 1;
		if ((c & j) == 1)
		{
    
    
			count++;
		}
		c = c >> 1;

	}
	printf("%d",count);
	return 0;
}

Insert picture description here

4. Expression evaluation

Part of expression evaluation is performed in the order of precedence and associativity of operators

The higher priority is calculated first. When the expression priority is the same, the calculation direction is based on the associativity, so I won’t repeat it here.

At the same time, sometimes the operands of the expression may be transformed into other types

Shaping conversion (invisible type conversion)

eg:

#include<stdio.h>
int main()
{
    
    
	char a = 3;
	char b = 137;
	char c = a + b;
	printf("%d",c);
	return 0;
}

Insert picture description here
Found that this is not what we thought 140,

This is because the size of the char type is one byte, and an integer is 4 bytes

char—8 bits,

int—32 bits,

Integer promotion is promoted according to the sign bit. The sign bit is the first bit of the binary bit.

The + operator operates on the int type, so the char type is first filled to 32 bits

a=0 0 0 0 0 0 1 1 (complement, sign bit is 0)

a=0000…0 1 1 (32 bits)

b= 1 1 1 1 1 1 1 1 (complement)

b=0 0 0 0 0 0 1 1 1 1 1 1 1 (32 bits)

a+b=0 0…1 0 0 0 0 0 1 0 (32 bits)

c=a+b (c is a character type and can only store 8 bits, so truncation will occur)

c=1 0 0 0 0 0 1 0

printf("%d") so c needs to be plasticized and promoted

The sign bit of c is 1, so add 1 to 32 bits

c after the integer promotion

1 1 1 1 1 1…1 0 0 0 0 0 1 0 (32 bits) (complement code)
1 1 1 1 1 1…1 0 0 0 0 0 0 1 (32 bits) (complement code)
1 0 0 0 0 0…0 1 1 1 1 1 1 0 (32 bits) (original code)

The sign bit is 1 means it is a negative number
. The original code is -116

If some numbers are unsigned digits, integer promotion will add zero.
Integer promotion only occurs when there are binary digits and the size is smaller than int

Insert picture description here
Integer promotion has occurred.

This will not happen when converting from int to a type larger than int, and will not be truncated

The computer is not necessarily unique in calculating expressions

eg:

           a*b+c*d+e*f

You can calculate a b, c d, e*b, and then add the three together,

Or take a b+c d as a whole and calculate first, then e*f, and then add it up,

Therefore, the operation of the expression may not always be unique, and care must be taken to avoid this situation.

eg:int c=(++a)+(++a)+(++a);

#include<stdio.h>
int main()
{
    
    
	int a = 4;
	int c = (++a) + (++a) + (++a);
	printf("%d",c);

}

Insert picture description here
The calculation here is to calculate ++a three times first, a=7 and then the sum is 21

But different compilers have different calculation order. You can also calculate ++a twice (taking the first two expressions as a whole), and then calculate the + (++a) after the calculation.

The calculation formula is too complicated, so there is no need to go into it

Guess you like

Origin blog.csdn.net/dodamce/article/details/113243797