Detailed explanation of operators (1)

1. Classification of operators:

Arithmetic Operators
Shift Operators 
Bitwise Operators Assignment
Operators
Unary Operators
Relational Operators
Logical Operators
Conditional Operators Comma
Expressions
Subscript References, Function Calls, and Structure Members

2. Arithmetic operators

+ - * / %

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

3. Shift operator

<< 左移操作符
>> 右移操作符
注:移位操作符的操作数只能是整数。

 Shift operators move bits in binary.

There are three binary representations of integers: original code, complement code, and complement code.

Original code: According to the positive or negative of the value, the binary sequence written directly is the original code, an integer has 4 bytes, 32 bits, and the binary sequence of an integer is 32 bie bits. For signed integers, the highest bit is the sign bit, 0 for positive numbers and 1 for negative numbers. For unsigned integers, there is no sign bit and all bits are significant.

Inverse code: The sign bit of the inverse code remains unchanged, and the other bits are reversed bit by bit.

Complement: The binary + 1 of the one's complement is the complement.

For positive integers, the original code, inverse code, and complement code are the same, so no calculation is required.

For negative integers, the original code, inverse code, and complement code need to be calculated.

Regardless of whether it is a positive integer or a negative integer, what is stored in memory is a two's complement binary sequence.

Integers also use two's complement when calculating.

10
原码:00000000 00000000 00000000 00001010
反码:00000000 00000000 00000000 00001010
补码:00000000 00000000 00000000 00001010
-10
原码:10000000 00000000 00000000 00001010
反码:11111111 11111111 11111111 11110101
补码:11111111 11111111 11111111 11110110

3.1 Left shift operator

Shifting rules:
discard on the left and add 0 to the right.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	//左移操作:左边丢弃,右边补0
	//00000000000000000000000000000111
	int m = 7;
	int n = m << 1;
	//00000000000000000000000000001110
	printf("%d\n", m);
	printf("%d\n", n);

	return 0;
}

 After the left shift, the value of m is still 7, m is only involved in the operation, and the << operator has a similar effect of *2.

negative number:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	//左移操作:左边丢弃,右边补0
	//原码:10000000000000000000000000000111
	//负数要计算补码
	//反码:11111111111111111111111111111000
	//补码:11111111111111111111111111111001
	int m = -7;
	int n = m << 1;
	//左移后:11111111111111111111111111110010
	//打印出来的是原码,所以我们计算左移后的原码,-1取反得到原码
	//11111111111111111111111111110001
	//10000000000000000000000000001110
	printf("%d\n", m);
	printf("%d\n", n);

	return 0;
}

  

The left shift operator can make some binary numbers go where we want. 

3.2 Right shift operator

Shift rules:
First, there are two types of right shift operations:
1. Logical shift
, fill with 0 on the left, and discard on the right.
2. Arithmetic shift,
fill with the sign bit of the original value on the left, and discard on the right.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int a = -10;
	//原码:10000000 00000000 00000000 00001010
	//反码:11111111 11111111 11111111 11110101
	//补码:11111111 11111111 11111111 11110110
	int b = a >> 1;
	//逻辑位移:01111111 11111111 11111111 11111011
	//算数位移:11111111 11111111 11111111 11111011
	//反码:11111111 11111111 11111111 11111010
	//原码:10000000 00000000 00000000 00000101-> -5
	printf("a=%d\n", a);
	printf("b=%d\n", b);
	return 0;
}

  

Whether to use logical shift or arithmetic shift depends on the compiler. Most of them are arithmetic right shift, and logical right shift is too simple and rude.

Shifting right has the effect of dividing by 2.

Warning ⚠:
For shift operators, don't shift negative bits, this is undefined by the standard.

For example:

int num = 10;
num>>-1;//error

4. Bitwise operators

Bitwise operators are:

& //按位与
| //按位或
^ //按位异或
注:他们的操作数必须是整数。

 The & operation uses the complement code to perform the operation. If the corresponding binary bit is 0, it is 0, and if both are 1, it is 1.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int a = 3;
	int b = -5;

	int c = a & b;//按(2进制)位与
	//00000000000000000000000000000011 --- 3的补码
	//10000000000000000000000000000101 
	//11111111111111111111111111111010
	//11111111111111111111111111111011 --- -5的补码
	//00000000000000000000000000000011 --- 3的补码
	//00000000000000000000000000000011
	printf("%d\n", c);
	return 0;
}

 The feature of & is to get a certain bit you want. Any number &1 gets the last bit of its binary digits. Cooperate with the shift operator to shift the binary number we want to the last bit, and &1 can get this bit.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int a = 3;
	//如果想要得到3的最低位
	//a&1
	//00000000000000000000000000000011 --- 3的补码
	//00000000000000000000000000000001 ---1的补码
	//遇到0则为0,3的最低位之前的所有位都会变成0,如果结果是0,表示3的最低位是0,如果结果是1,3的最低位就是1
	return 0;
}

| Operation:

Computation is performed with two's complement, and the corresponding binary bit is 1 if it is 1, and it is 0 if both are 0.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int a = 3;
	int b = -5;
	//00000000000000000000000000000011 --- 3的补码
	//10000000000000000000000000000101 
	//11111111111111111111111111111010
	//11111111111111111111111111111011 --- -5的补码
	int c = a | b;
	//00000000000000000000000000000011 --- 3的补码
	//11111111111111111111111111111011 --- -5的补码
	//11111111111111111111111111111011 
	//11111111111111111111111111111010
	//10000000000000000000000000000101   -5


	printf("%d\n", c);

	return 0;
}

^ operator:

Same as 0, different as 1.\

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int a = 3;
	int b = -5;
	//00000000000000000000000000000011 --- 3的补码
	//10000000000000000000000000000101 
	//11111111111111111111111111111010
	//11111111111111111111111111111011 --- -5的补码
	int c = a ^ b;
	//00000000000000000000000000000011
	//11111111111111111111111111111011
	//11111111111111111111111111111000
	//10000000000000000000000000000111
	//10000000000000000000000000001000
	//
	printf("%d\n", c);

	return 0;
}

Next, let's look at an interview question: without creating a temporary variable, realize the exchange of two numbers.

 There is a flaw in this code, if the value of the variable is too large, there will be problems.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int a = 3;
	int b = 5;
	printf("a=%d b=%d\n", a, b);
	a = a + b;
	b = a - b;
	a = a - b;
	printf("a=%d b=%d\n", a, b);
	return 0;
}

This code is implemented using the ^ operator, but this method is not very efficient, creating a temporary variable is the best. 

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	int a = 3;
	int b = 5;

	int c = 0;//中间变量
	printf("a=%d b=%d\n", a, b);
	
	a = a ^ b;//a = 3^5
	b = a ^ b;//b=3^5^5 b=3
	a = a ^ b;//a= 3^5^3^5^5 a=5

	printf("a=%d b=%d\n", a, b);
	return 0;
}

5. Assignment operator

The assignment operator is a great operator that allows you to get a value that you were not happy with before. That is, you can reassign yourself.

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 assignment operator

+=
-=
*=
/=
%=
>>=
<<=
&=
|=
^=

These operators can all be written as composite effects.
for example:

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

6. Unary operators

6.1 Introduction to unary operators

Unary operators have only one operand.

! 逻辑反操作
- 负值
+ 正值
& 取地址
sizeof 操作数的类型长度(以字节为单位)
~ 对一个数的二进制按位取反
-- 前置、后置--
++ 前置、后置++
* 间接访问操作符(解引用操作符)
(类型) 强制类型转换

! You can turn false into true and true into false.
& is an address operator, which is stored in a pointer variable.

* is a dereference operator, and *x knows the object pointed to by x through the address stored in x.

In fact, we have seen sizeof before, and we can find the size of the space occupied by variables (types).

Front ++: Calculation formula: +1 first, then use.

Rear ++: Mantra: Use first, then +1.

-- is the same as ++.

6.2 sizeof and arrays

The array parameter is the address of the first element, which is a pointer. The size of the address is 4 bytes on the 32-bit platform, and 8 bytes on the 64-bit platform.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdio.h>
void test1(int arr[])
{
	printf("%d\n", sizeof(arr));//(2)
}
void test2(char ch[])
{
	printf("%d\n", sizeof(ch));//(4)
}
int main()
{
	int arr[10] = { 0 };
	char ch[10] = { 0 };
	printf("%d\n", sizeof(arr));//(1)
	printf("%d\n", sizeof(ch));//(3)
	test1(arr);
	test2(ch);
	return 0;
}


 

 7. Relational Operators

>
>=
<
<=
!= 用于测试“不相等”
== 用于测试“相等

These relational operators are relatively simple, there is nothing to talk about, but we should pay attention to the pitfalls when using some operators.
Warning:
In the process of programming, == and = are accidentally written wrong, resulting in errors.

8. Logical operators

&& 逻辑与
|| 逻辑或

&& both are true to be true, || one is true to be true.

360 test questions

#include <stdio.h>
int main()
{
int i = 0,a=0,b=2,c =3,d=4;
i = a++ && ++b && d++;
//i = a++||++b||d++;
printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d);
return 0;
}

a++, a is 0 at first, so this expression is false, and the following ones don’t need to be counted, so only a+1 is left, and the others remain unchanged, i=0. 

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdio.h>
int main()
{
	int i = 0, a = 0, b = 2, c = 3, d = 4;
	/*i = a++ && ++b && d++;*/
	i = a++||++b||d++;
	printf(" a = %d\n b = %d\n c = %d\n d = %d\n i = %d\n", a, b, c, d,i);
	return 0;
}

Here, if a number is not 0, it is true, so all calculations are required, i=1.


Today's sharing is over here! Thank you guys for reading, see you next time.

Guess you like

Origin blog.csdn.net/2301_79035870/article/details/132392139