Super detailed operator explanation

Classification of operators

Arithmetic Operators
Shift Operators
Bit Operators
Assignment Operators Unary
Operators
Relational Operators
Logical Operators
Conditional Operators
Comma Expressions
1. Arithmetic Operators

+    -   *   /   %

(1) % is the remainder character, that is, the remainder, the two operands must be integers, and the return is the remainder after the division. Such as
(10 divided by 3, quotient 3 remainder 1)
10/3=1;
10%3=1;
in addition to the % operator, several other operators can act on integers and floating-point numbers. For the / operator, integer division is performed if both operands are integers. And as long as there are floating-point numbers, floating-point division is performed.

2. Shift operator (all operations are complement)

<< 左移操作符
>> 右移操作符
    
注:移位操作符的操作数必须是整数,移动位数只能是正整数(不能为负数)。3<<12(表示3左移12位)
   3>>12(表示3右移12位)

(1) Shift left (delete on the left, add 0 on the right)
3's complement (inverse, the complement is the same)
000000000000 00000000000000000011
3<<12 (12 bits left)
00000000000000000011 0000000000000

-3's original code
10000000000000000000000000000011

-3's complement (the sign bit of the original code remains unchanged, and the rest are inverted)
11111111111111111111111111111100
-3's complement (one's complement + 1)
111111111111 11111111111111111101
-3<<12 (-3 left shifted by 12 bits) (complement)
11111010 101000110
Converted to the inverse code (ie -1)
11111111111111111100 111111111111
converted to the original code (the sign bit remains unchanged, the rest are inverted)
10000000000000000011000000000000
The original code is the final result

(2) Shift right
(2).1 Logical shift right (fill 0 on the left, discard the extra on the right)
such as 15>>3

15的原码(反码、补码都一样)
000000000000000000000000000001111
15>>3
00000000000000000000000000001000

Such as -15>>3

-15的原码
10000000000000000000000000001111
-15的反码
11111111111111111111111111110000
-15的补码
11111111111111111111111111110001
-15>>3
00011111111111111111111111111110

(2).2 Arithmetic right shift (fill the highest bit on the left, discard on the right)
such as 8>>3

8的原码
00000000000000000000000000001000
8>>3
00000000000000000000000000000001

Such as -16>>3

-16的原码
10000000000000000000000000010000
-16的反码
11111111111111111111111111101111
-16的补码
11111111111111111111111111110000
-16>>3
11111111111111111111111111111110

3. Bit operators (complementary operations are performed)

&   按位与(两个都是1则结果为1,否则为0|   按位或(至少有一个1则结果为1^   按位异或(相同为0,不同为1
注:他们的操作数必须是整数。

Take 8-bit binary as an example
3&5

000000113000001015000000013&5

3|5

000000113000001015000001113|5

3^5

000000113000001015000001103^5

4. Assignment operator (=)

int a = 10;
int x = 0;
int y = 20; a = x = y+1;//连续赋值

compound assignment operator

+=    如a=a+b  可以写成 a+=b
-=      a=a-b          a-=b;
*=      a=a*b          a*=b;
/=       ...           ...
%=       ...           ...
>>=
<<=
&=
|=
^=
这些运算符道理都一样

5. Unary operator

!           逻辑反操作
-           负值
+           正值
&           取地址
sizeof      操作数的类型长度(以字节为单位)
~           对一个数的二进制按位取反
--          前置、后置--
++          前置、后置++
*           间接访问操作符(解引用操作符) (类型)       强制类型转换

! Logical inverse operation
0 becomes non-0, non-0 becomes 0

例如 a=3,b=0;
  则 !a=0,!b=1(或其他非0数)

Prefix ++ and prepend - - (first ++ or - -, then operation)

int main()
{
    
    
    int a = 10;
    int x = ++a;
    //先对a进行自增,然后对使用a,也就是表达式的值是a自增之后的值。x为11。
    int y = --a;
    //先对a进行自减,然后对使用a,也就是表达式的值是a自减之后的值。y为10;
    return 0;
 }

Postfix ++ and postfix - - (operate first and then ++ or - -)

#include <stdio.h>
int main()
{
    
    
    int a = 10;
    int x = a++;
    //先对a先使用,再增加,这样x的值是10;之后a变成11;
    int y = a--;
    //先对a先使用,再自减,这样y的值是11;之后a变成10;
    return 0; 
 }

6. Relational Operators

>
>=
<
<=
!=   用于测试“不相等”
==      用于测试“相等”

7. Logical Operators
&& Logical And
|| Logical Or

&& Logical AND: Concatenates two expressions into one. Both expressions must be true for the entire expression to be true
as in

1&&3 --->1
1&&0 --->0

|| Logical OR
Joins two expressions into one. One or both expressions must be true for the entire expression to be true. As long as one of them is true, the other becomes irrelevant
as in

1||1 -->1
1||0 -->1

8. Conditional operator
exp1 ? exp2 : exp3
such as finding the maximum value of two numbers

if(a>b)
  return a;
 else 
  return b;

replace with conditional expression

(a>b)?a:b
//判断(a>b)是否成立,如果成立返回a的值,否则返回b的值

9. Comma expressions
exp1, exp2, exp3, …expN
comma expressions are multiple expressions separated by commas.
Comma expressions, executed in order from left to right. The result of the entire expression is the result of the last expression.

int a = 1;
int b = 2;
int c=(a=a*b, a=b+10, a, b=a+1);
     a=1*2=2, a=2+10=12,12,b=12+1=13 
     所以结果为13

Guess you like

Origin blog.csdn.net/qq_62316056/article/details/124190151