当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
明解C语言第五章习题
MySQL的on duplicate key update 的使用
Zabbix部署与练习
OSS简单上传图片
flyway的快速入门教程
MySQL数据库字段超长问题
如何解决gedit 深色模式下高亮文本不可见?
C语言中指针没那么难~(2)【文章结尾有资料】
“数字化重构系统,搞定 CEO 是第一步”
英文字母间隔突然增大(全角与半角转换)
Office365无法打开word文档怎么办?Office365无法打开word文档的解决方法
7、MySQL Workbench 导出导入数据库
是对称矩阵的对角化
C语言中指针没那么难~ (1)【文章结尾有资料】
WPS表格怎么自动1234排下去?wps表格怎么自动生成序号?
MVC模式和三层架构
Android studio连接MySQL并完成简单的登录注册功能
Mysql 回表
网络层协议------IP协议
一文2500字手把手教你配置Jenkins自动化邮件通知