当前位置:网站首页>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.
边栏推荐
猜你喜欢

7、MySQL Workbench 导出导入数据库

Activiti 工作流引擎 详解

基于人脸的常见表情识别(2)——数据获取与整理

Network layer protocol------IP protocol

倾斜文档扫描与字符识别(opencv,坐标变换分析)

是对称矩阵的对角化

Difference Between Concurrency and Parallelism

canvas基础讲解加示例

推荐系统:实时性【特征实时性:客户端实时特征(秒级,实时)、流处理平台(分钟级,近实时)、分布式批处理平台(小时/天级,非实时)】【模型实时性:在线学习、增量更新、全量更新】

【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
随机推荐
MYSQL 唯一约束
【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码
MYSQL JDBC图书管理系统
FFmpeg —— 裁剪视频(含音视频),不需编解码(附完整源码)
推荐系统:实时性【特征实时性:客户端实时特征(秒级,实时)、流处理平台(分钟级,近实时)、分布式批处理平台(小时/天级,非实时)】【模型实时性:在线学习、增量更新、全量更新】
推荐系统:冷启动问题【用户冷启动、物品冷启动、系统冷启动】
是对称矩阵的对角化
推荐系统-模型:FNN模型(FM+MLP=FNN)
MySQL 高级(进阶) SQL 语句 (一)
@RequestParam使用
mysqldump导出提示:mysqldump [Warning] Using a password on the command line interface can be insecure
想要写出好的测试用例,先要学会测试设计
啊?现在初级测试招聘都要求会自动化了?
MySQL的DATE_FORMAT()函数将Date转为字符串
Based on the face of the common expression recognition - model building, training and testing
Office365无法打开word文档怎么办?Office365无法打开word文档的解决方法
CDH集群spark-shell执行过程分析
Ordinary int main(){} does not write return 0; what will happen?
PHP低代码开发平台 V5.0.7新版发布
FFmpeg —— 将mp4转为gif输出(附源码)