Summary of c language operators (1)

Arithmetic operator

         +      -      *     /        %

1. Except for the% operator, other operators can work on integers and floating-point numbers.
2. For the / operator, if both ends are integers, the integer division is performed, as long as one end is a floating-point number, it is the decimal division
3.% Both ends of the operator must be integers, and what is returned is the remainder after division

Shift operator

1. Left shift operator

Left shift operator movement rules: discard on the left, fill 0 on the right

Shifting to the left actually moves the binary complement stored in memory.

Let’s take a look at the positive number

#include<stdio.h>
int main()
{
    
    
	int a = 5;
	int b = a<<1;
	printf("%d",b);
} 
// 输出结果为 10


Look at the negative number again

#include<stdio.h>
int main()
{
    
    
	int a = -1;
	int b = a<<1;
	printf("%d",b);
} 
// 输出结果为 -2

2. Right shift operator

Right shift operators are divided into two categories:

1. Arithmetic shift right:

Discard on the right, complement sign on the left

2. Logic shift right

Discard on the right, padded with 0 on the left

Which method is used depends on the compiler.

#include<stdio.h>
int main()
{
    
    
	int a = -1;
	int b = a >> 1;
	printf("%d",b);
}
// 输出结果为-1


Warning: For shift operators, do not move negative digits. This is undefined by the standard.

#include<stdio.h>
int main()
{
    
    
	int num = 10;
	num>> -1;// error
}

Bit operator

1. Bitwise and

&: According to the binary (complement) bit and, it is 1 if both are 1, otherwise it is 0

#include<stdio.h>
int main()
{
    
    
	int a = 3;
	int b = 5;
	int c = a & b;
	printf("%d\n",c);
}
// 输出结果为1

2. Bitwise OR

|: According to binary (complement) bit or, one bit is 1 or 1, and all 0 is 0.

#include<stdio.h>
int main()
{
    
    
	int a = 3;
	int b = 5;
	int c = a | b;
	printf("%d\n",c);
}
// 输出结果为7

3. Bitwise XOR

^: XOR according to binary (complement) bits, the same is 0, and the different is 1.

#include<stdio.h>
int main()
{
    
    
	int a = 3;
	int b = 5;
	int c = a ^ b;
	printf("%d\n",c);
}
// 输出结果为6


Warning: & | ^ can only be used for shaping

4. Application of bitwise AND and bitwise XOR

1. Application of bitwise XOR

Everyone must have done swapping the values ​​of two numbers. The most commonly used method is to create a temporary variable.

law

#include<stdio.h>
int main()
{
    
    
	int a = 3;
	int b = 5;
	int tmp = a;
	a = b;
	b = tmp;
	printf("a = %d,b = %d", a, b);
}
// 输出结果 a = 5, b = 3

Now it is required not to create temporary variables and exchange the values ​​of two integers. Some people may think of the following approach

#include<stdio.h>
int main()
{
    
    
	int a = 3;
	int b = 5;
	a = a + b;
	b = a - b;
	a = a - b;
	printf("a = %d,b = %d", a, b);
}
// 输出结果 a = 5, b = 3

But this approach is flawed. If our a and b are relatively large, but not beyond the scope of shaping, a + b will be sent

An overflow occurs, leading to incorrect results.

Now give the method of bitwise XOR

#include<stdio.h>
int main()
{
    
    
	int a = 3;
	int b = 5;
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
	printf("a = %d,b = %d", a, b);
}
// 输出结果 a = 5, b = 3

2. Application of bitwise AND

Find the number of binary 1s of an integer stored in memory

#include <stdio.h>
int main()
{
    
    
	 int num = -1;
	 int i = 0;
 	 int count = 0;//计数
	 while(num)
     {
    
    
		 count++;
		 num = num&(num-1);
 	 }
 	printf("二进制中1的个数 = %d\n",count);
    return 0;
}

Assignment operator

Assignment operator is a great operator, he can let you get a value you were not satisfied with. That means you can

Re-assign yourself.

#include<stdio.h>
int main()
{
    
    
	int weight = 120;
	weight = 89;
	double salary = 10000.0;
	salary = 20000.0;
	// 赋值操作符可以连续使用
	int a = 10;
	int x = 0;
	int y = 20;
	a = x = y + 1; // 连续赋值
	// 相当于以下代码
	x = y + 1;
	a = x;
}

Compound operator

+=

-=

*=

/=

%=

》=

《=

&=

|=

^=

int x = 10; 
x = x + 10;
 x += 10;//复合赋值
//其他运算符一样的道理。这样写更加简洁

Guess you like

Origin blog.csdn.net/DR5200/article/details/113137367