当前位置:网站首页>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.
边栏推荐
猜你喜欢
idea plugins搜索不到插件
从离线到实时对客,湖仓一体释放全量数据价值
Face-based Common Expression Recognition (2) - Data Acquisition and Arrangement
推荐系统:评估指标【离线评估指标:RMSE(均方根误差)、AUC、准确率、召回率、F1】【在线评估:A/B测试】【一般要求响应时间<0.5s】
MySQL 高级(进阶) SQL 语句 (一)
【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码
Maxwell 一款简单易上手的实时抓取Mysql数据的软件
网络层协议------IP协议
Common Expression Recognition Based on Face (1) - Basic Knowledge of Deep Learning
To the operation of the int variable assignment is atom?
随机推荐
线性结构:顺序表和链表
一文2500字手把手教你配置Jenkins自动化邮件通知
MySQL 高级(进阶) SQL 语句 (一)
英文字母间隔突然增大(全角与半角转换)
365天挑战LeetCode1000题——Day 044 按公因数计算最大组件大小 并查集
Ordinary int main(){} does not write return 0; what will happen?
mysql 递归函数with recursive的用法
MySQL的主从复制
canvas基础讲解加示例
excel数字如何转换成文本?excel表格数据转换成文本的方法
使用map函数,对list中的每个元素进行操作 好像不用map
基于Apache Doris的湖仓分析
倾斜文档扫描与字符识别(opencv,坐标变换分析)
TensorFlow2: Overview
网络层协议------IP协议
【无标题】多集嵌套集合使不再有MultipleBagFetchException
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
OSS简单上传图片
“数字化重构系统,搞定 CEO 是第一步”
PPT如何开启演讲者模式?PPT开启演讲者模式的方法