当前位置:网站首页>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.
边栏推荐
- WPS没有在任务栏显示所有窗口选项怎么回事?
- Mysql8创建用户以及赋权操作
- MySQL的on duplicate key update 的使用
- Multi-threaded mutex application RAII mechanism
- “数字化重构系统,搞定 CEO 是第一步”
- 365天挑战LeetCode1000题——Day 044 按公因数计算最大组件大小 并查集
- Weak Banks to data conversion ability?Matt software help solve bank dilemma
- @WebServlet注解(Servlet注解)
- MySQL Workbench 安装及使用
- 网络安全实验环境搭建
猜你喜欢

并发与并行的区别

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

推荐系统:开源项目/工具【谷歌:TensorFlow Recommenders】【Facebook:TorchRec】【百度:Graph4Rec】【阿里:DeepRec和EasyRec】

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

vlookup函数匹配不出来只显示公式的解决方法

推荐系统-排序层:排序层架构【用户、物品特征处理步骤】

WPS怎么独立窗口显示?wps单独窗口显示怎么操作?
![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]

【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码

【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码
随机推荐
线性结构:顺序表和链表
推荐系统-排序层:排序层架构【用户、物品特征处理步骤】
mysqldump导出提示:mysqldump [Warning] Using a password on the command line interface can be insecure
银行数据资产转换能力弱?思迈特软件助力解决银行困境
C语言中指针没那么难~ (1)【文章结尾有资料】
GateWay实现负载均衡
MySQL的Replace用法详解
推荐系统:AB测试(AB Test)
halcon——轮廓线
Flex布局详解
KEIL问题:【keil Error: failed to execute ‘C:\Keil\ARM\ARMCC‘】
多线程的互斥锁应用RAII机制
【Codeforces思维题】20220728
vlookup函数匹配不出来只显示公式的解决方法
Mysql索引特性(重要)
肖特基二极管厂家ASEMI带你认识电路中的三大重要元器件
[c语言]二维数组动态分配内存
Android Studio 实现登录注册-源代码 (连接MySql数据库)
5分钟搞懂MySQL - 行转列
MySQL 删除表数据,重置自增 id 为 0 的两个方式