当前位置:网站首页>C language: detailed explanation of operators
C language: detailed explanation of operators
2022-07-30 20:31:00 【so-and-so Xiaolu】
- Operators
- Operator Category:
Arithmetic Operators
Shift operator
Bitwise operators
Assignment operator
Unary operator
Relational Operators
Logical operators
Conditional operator
Comma expression
Subscript references, function calls, and structure members
- Arithmetic Operators
Includes +, -, *, /, %
The special % can only be integers on both sides of the remainder %.A few other floats and integers are ok.
For the / operator Performs integer division if both operands are integers.And as long as there are floating-point numbers, floating-point division is performed.
- Shift operator
& //Bitwise AND 0 is 0, double 1 is 1
| //Bitwise OR 1 is 1, double 0 is 0
^ //Bitwise exclusive OR The same is 0, the difference is 1
Note: Their operands must be integers.
Additionally: the operation rules here are calculated in two's complement
Example: When swapping two numbers (without using a third variable)

- Binary original, two's complement and one's complement code
Binary consists of 0s and 1s
Binary is like this.Take 15 as an example (32-bit):
00000000000000000000000001111; the four 1s here correspond respectively: 2 to the 3rd power, 2 to the 2nd power, 2 to the 1st power, 2 to the 0th power
The conversion from binary to decimal is like this: 2 to the 3rd power*1+ 2 to the 2nd power*1+ 2 to the 1st power*1+ 2 to the 0th power*1=15
The above binary is the original binary code.The first bit is the sign bit: 0 is negative, 1 is positive
The inverse code is except the sign bit, the original code 0 and 1 are interchanged, 0 becomes 1, 1 becomes 0
The complement is the one's complement plus 1, and every two is added by one
It is worth noting that the two's complement of positive integers are the same
- Shift operator
Discard on the left, fill 0 on the right
>> right shift operator
There are two types
.Logical shift
The left is filled with 0, the right is discarded
2 arithmetic shifts
The left side is filled with the sign bit of the original value, and the right side is discarded
Note: The operands of the shift operator can only be integers.
For shift operators, do not shift negative bits, this is not defined by the standard
Additionally: the operation rules here are calculated in two's complement
Example: count the number of 1s in binary

Example: Find the number of different bits in the binary of two numbers

- Assignment operator=
This is a common assignment operator; for example: int i=0;
He has many compound operators +=, -=, *=, /=, %=,
They operate in the same way as +=.For example: a+=6 ==== a=a+6
- Unary operator

One of the more noteworthy is sizeof

In this code sizeof(arr) is, sizeof sees that the entire array of arr is accessed, and the size of int type is 4, so 4*10=40
Whether sizeof (arr[10]) is out-of-bounds access here, in fact, there is no, sizeof is only interpreted as int type, it does not enter, even arr[11] will not be out-of-bounds access
Example:

The first a is OK, it will print 4, the second is not OK, it will report an error
So, sizeof can access variables without parentheses, but cannot access data types without variables.

Why here?
In fact, this is the case. Generally speaking, s=2+5=7, but in fact
There will be truncation here, the things in the sizeof() brackets are not calculated, sizeof finds that s is of type short, returns 2, and then the following s is still 10.
- ~ Reverse the binary bitwise of a number
Reverse each bit bit by bit, including the sign bit
- i++ and ++i
++a
First increment a, then use a, that is, the value of the expression is the value after the increment of a
a++
Use a first, then add
- Relational Operators
= <
You cannot use == to determine whether two strings are equal

Warning:
In the process of programming, == and = are accidentally written incorrectly, resulting in errors.
Example:

The answer should be 1 2 3 4
In this way, a++ uses a first, and because a=0, so 0&&++b, so ++b does not need to be calculated, it is directly equal to 0, and similarly d++ is not calculated.
- Comma expression
exp1, exp2, exp3, …expN
Comma expressions, executed from left to right.The result of the entire expression is the result of the last expression.

Although the comma expression counts the value of the last expression, the preceding expressions are also counted, because the preceding expressions may change the value of the variable
- Arithmetic Conversion
If the operands of an operator are of different types, unless one of the operands is converted to the class of the other operand
type, otherwise the operation cannot be performed.The following hierarchy is called ordinary arithmetic transformations.
边栏推荐
猜你喜欢

【Codeforces思维题】20220728
![Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]](/img/bb/25b0493398901b52d40ff11a21e34c.png)
Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]

MYSQL JDBC图书管理系统

HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界

MySQL——几种常见的嵌套查询

idea plugins搜索不到插件

使用map函数,对list中的每个元素进行操作 好像不用map

WPS怎么独立窗口显示?wps单独窗口显示怎么操作?

C语言中指针没那么难~(2)【文章结尾有资料】
![Recommendation System - Sorting Layer: Sorting Layer Architecture [User and Item Feature Processing Steps]](/img/e7/4000f9f1f091970442ae3ecbc34c9f.png)
Recommendation System - Sorting Layer: Sorting Layer Architecture [User and Item Feature Processing Steps]
随机推荐
Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
时间复杂度与空间复杂度
Zabbix部署与练习
Android Studio 实现登录注册-源代码 (连接MySql数据库)
英文字母间隔突然增大(全角与半角转换)
MySQL BIGINT 数据类型
KEIL problem: [keil Error: failed to execute 'C:\Keil\ARM\ARMCC']
Recommendation System - Sorting Layer - Model (1): Embedding + MLP (Multilayer Perceptron) Model [Deep Crossing Model: Classic Embedding + MLP Model Structure]
Mysql 回表
是对称矩阵的对角化
MySQL (2)
excel数字如何转换成文本?excel表格数据转换成文本的方法
多线程的互斥锁应用RAII机制
PHP低代码开发平台 V5.0.7新版发布
HJ85 最长回文子串
excel数字下拉递增怎么设置?
Swift简介
4年测试经验去面试10分钟就被赶出来了,面试官说我还不如应届生?都这么卷吗...
Recommended system: cold start problem [user cold start, item cold start, system cold start]
bebel系列- 插件开发