Detailed explanation of operators (1)

Hello everyone, today we are going to learn operators in C language.
Insert image description here
First let's look at the types of operators.

Operator classification:

Arithmetic operators
Shift operators
Bit operators
Assignment operators
Unary operators Relational operators Logical operators
Conditional operators Comma expressions Subscripted references, function calls, and structure members



Arithmetic operators:

1. "/" division operator:
Integer division will be performed only if both sides of the division operation are integers. During division operation, if one side of both sides has a floating point number, floating point division will be performed. Division occurs regardless of the type of operands.
[See the following code]

#include<stdio.h>
int main()
{
    
    
	int ret1 = 9 / 2;
	double ret2 = 9 / 2;
	double ret3 = 9 / 2.0;
	printf("%d\n", ret1);
	printf("%lf\n", ret2);
	printf("%lf\n", ret3);
	return 0;
}

Insert image description here
Seeing the results of the program running, we find that floating point division is only performed when the numbers on both sides have floating point types.

2. The “%” remainder operator, as the name implies, is to take the remainder.
[see code]

#include<stdio.h>
int main()
{
    
    
	int a = 9 % 2;
	printf("%d", a);
}

Insert image description here
Since the remainder of dividing nine by two is one, the result of the operation is one.

3. Arithmetic operators also include the "*" multiplication operator and the "+" and "-" addition and subtraction operators. These operators are relatively simple and everyone must know them, so I won't explain them here.

There are a few other things to note about arithmetic operators:

  1. In addition to the % operator, several other operators can operate on integers and floating-point numbers.
  2. For the / operator, if both operands are integers, integer division is performed. As long as there are floating point numbers, floating point division is performed.
  3. Both operands of the % operator must be integers. Returns the remainder after division.

Shift operator:

<<Left shift operator>> Right shift operator
Note: The operand of the shift operator can only be an integer.

When we perform the shift operator, we must pay attention to the fact that we are performing a binary shift. What does this mean? Because ours is an integer. The integer has four bytes and thirty-two bits. These thirty-two bits The bits are all binary, and these thirty-two bits become the original code of the number. The first binary bit is the sign bit, 0 means positive, and 1 means negative. For example, the original code of 1 is 00000000000000000000000000000001, and the original code of -1 is 1000000000000000000000000000001. Because when we perform operator operations, we operate on its complement, so we also convert the original code into a complement. What we should note here is that the complement and complement of a positive integer are its original codes, and the complement of a negative integer must first be converted into a complement. To convert it into a complement, the sign bit only needs to remain unchanged. The other bits are inverted bit by bit, and the complement is the complement plus 1. See the code below:

//a=-5
//原码 10000000000000000000000000000101
//反码 11111111111111111111111111111010
//补码 11111111111111111111111111111011

We already know that the operator operates on its complement. Next, let us understand the rules of the shift operator:

Left shift operator: discard the left side and add 0 to the right side.
Right shift operator:

  1. The left side of the logical shift is filled with 0 and the right side is discarded.
  2. The left side of the arithmetic shift is filled with the sign bit of the original value, and the right side is discarded.

Next let's see the code:

int main()
{
    
    
	int a = 5;
	int b = a << 1;
	// a的补码 00000000000000000000000000000101
	//左移移位 00000000000000000000000000001010
	printf("%d\n", b);
	printf("%d\n", a);
	return 0;
}

Insert image description here
Because it is binary, the value of b is equal to 2 cubed plus 2 cubed, which is equal to 10, but a itself does not change.

int main()
{
    
    
	int a = -5;
	int b = a >> 1;
	//a的原码 10000000000000000000000000000101
   //    反码 11111111111111111111111111111010
  //     补码 11111111111111111111111111111011
   //右移一位 01111111111111111111111111111101 或者11111111111111111111111111111101
	printf("%d\n", b);
	printf("%d\n", a);
	return 0;
}

Insert image description here
We can see that the printed result is -3. Although we have two shifting methods, our compiler uses arithmetic shifting.

Note : For shift operators, do not move negative bits, this is not defined by the standard.

Bit operators:

& //Bitwise AND
| //Bitwise OR
^ //Bitwise XOR
Note: Their operands must be integers.

1.& is based on the location operator
&: all 1s are 1, and 0s are 0

code show as below:

int main()
{
    
    
	int a = 3;
	int b = -2;
	int c = a & b;
	//a的补码 00000000000000000000000000000011
	//b的原码 10000000000000000000000000000010
	//b的反码 11111111111111111111111111111101
	//b的补码 11111111111111111111111111111110
	//根据全1为1的原则
	//得到c的补码为00000000000000000000000000000010=2
	printf("%d", c);
	return 0;
}

Insert image description here

2. | Bitwise OR
|: 1 is 1, all 0 is 0

[See the following code]:

int main()
{
    
    
	int a = 3;
	int b = -2;
	int c = a | b;
	//a的补码 00000000000000000000000000000011
	//b的原码 10000000000000000000000000000010
	//b的反码 11111111111111111111111111111101
	//b的补码 11111111111111111111111111111110
	//根据全1为1的原则
	//得到c的补码为 11111111111111111111111111111111
	//得到c的反码为 11111111111111111111111111111110
	//得到c的原码为 10000000000000000000000000000001=-1
	printf("%d", c);
	return 0;
}

Insert image description here

Here we should pay attention to the complement code obtained through the bit operator. If the sign bit is 0, then its original code is the complement code. If its sign bit is one, then it must first subtract one to get the complement code. If the sign bit is not Perform bitwise inversion to get the original code and calculate the result. Or the original code can be obtained by inverting the bitwise bit without changing the sign bit and adding one.

3.^Bitwise XOR
^: The same is 0, the difference is 1

code show as below:

int main()
{
    
    
	int a = 3;
	int b = -2;
	int c = a ^ b;
	//a的补码 00000000000000000000000000000011
	//b的原码 10000000000000000000000000000010
	//b的反码 11111111111111111111111111111101
	//b的补码 11111111111111111111111111111110
	//根据全1为1的原则
	//得到c的补码为 11111111111111111111111111111101
	//得到c的反码为 11111111111111111111111111111100
	//得到c的原码为 10000000000000000000000000000011=-3
	printf("%d", c);
	return 0;
}

Insert image description here

assignment operator

For example:

int weight = 120;//体重
weight = 89;//不满意就赋值
double salary = 10000.0;
salary = 20000.0;//使用赋值操作符赋值

There are also compound assignment operators:
for example:
+= <<=
-= >>=
/= *=
For example:

int x = 10;
x = x+10;
x += 10;//复合赋值

This is the content of the first section, thank you all.

Guess you like

Origin blog.csdn.net/Lehjy/article/details/132302250