Interpretation of c language operator system

Summary outline

Insert picture description here

Arithmetic Operator

Arithmetic operator arithmetic take the remainder
Note:

  1. The remainder can only be used for integers

	//3 % 2;
	//3 % 2.0;下面两行都是错误写法 只可以整数取模 
	//3.0 % 2;
  1. / If the division sign does not have a floating point number, the quotient is an integer. If there is a floating point number, use floating point division
    Insert picture description here
  2. The order of addition, subtraction, multiplication and division follows mathematical rules

Shift operator

<< left shift operator left rule : positive number of source leftmost one to give up the right of a fill vacancies 0

Insert picture description here
Insert picture description here
The leftmost bit of the complement of a negative number is discarded and the rightmost bit is added to 0
Insert picture description here

Insert picture description here
note

  1. Positive numbers directly operate on the source code. Negative numbers need to operate his complement
  2. The source code is inverted by bit to get the complement, and the complement is added by 1 to get the complement
  3. The first sign bit remains unchanged when bitwise inverted
  4. After the operation, convert the complement code into the source code to get the result of the operation

**>>**Right shift operator
right shift rule :

  1. Logical shift is filled with 0 on the left and discarded on the right

  2. Arithmetic shift left filled with the sign bit of the original value, discarding the right
    Insert picture description here
    Insert picture description here
    note

  3. Arithmetic shift right

  4. Negative numbers are the same as shifting to the left. Always use the complement operation to
    warn
    that negative digits cannot be moved! ! ! ! ! !

Bit operator

Bitwise operators are & bitwise and, | bitwise or, bitwise exclusive or ^

& Bitwise and

Bitwise and only need to compare each bit of the binary one by one
as shown in the figure
Insert picture description here

Position and skill

  1. Odd and even discrimination
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()

{
    
    
	 int a = 3;
	 int b = 4;
	 int c = a & 1;
	 int d = b & 1;
	printf("%d\n", c); 
	printf("%d\n", d);
	return 0;
}

Conclusion
Odd number&1==1 Even number&1==0

  1. Take the k-th bit of the int type variable a (k=0,1,2...size of(int), counting from the right side of the binary form of a)
    formula a>>k&1 ;
    eg, find the third bit of 8
    Insert picture description here

It is seen from FIG request bits to be counted from the leftmost 0 .

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()

{
    
    
	 int e = 8;
	 int f=e >> 3 & 1;
	
	printf("%d\n", f);
	return 0;
}

Insert picture description here

  1. Use bit and & operation to determine whether an integer is a power of 2.
    The bit weight of a binary number is a power of 2 as the base. If an integer m is 2 to the power of n, then only the highest bit is 1 after conversion to binary, and the remaining positions are 0. Then observe the conversion of m-1 to binary The form and the result of m&(m-1)

Example:

int func(int num)
{
    
    
    return ((num > 0) && ((num & (num - 1)) == 0));//2的n次幂大于0
}

| Bitwise OR

Insert picture description here

Algorithm

The essence of bit-OR operation is to perform logical OR operation on the two data involved in the operation according to the corresponding binary number bit by bit. For example: the expression for bitwise OR operation of int type constants 5 and 7 is 5|7, and the result is as follows:
5= 0000 0000 0000 0101

7= 0000 0000 0000 0111
5|7= 0000 0000 0000 0111

Bitwise or technique

  1. Set a designated bit of data.
    For example, the integer a=321, the operation of setting the lower eight bits of data to 1 is a=a|0XFF. 321= 0000 0001 0100 0001 | 0000 0000 1111 1111=0000 0000 1111 1111.

^ Bitwise XOR

Insert picture description here
Operation rules
The essence of bitwise XOR operation is to perform logical XOR operation bit by bit for the two data involved in the operation according to the corresponding binary number. The result of the corresponding bit is true only when the binary numbers of the corresponding bit are mutually exclusive.

Bitwise XOR technique

  1. Exchange the two variables without introducing other variables
a=a^b;

b=b^a;

a=a^b;
  1. Positioning flip

Positioning flip: set a designated bit of data, change 1 to 0, and 0 to 1. For example, the integer a=321, the operation of flipping the lower eight bits of the data is a=a^0XFF;

Assignment operator

Assignment operation is one of the most commonly used operations in programming. C language provides a total of 11 assignment operators, all of which are binary operators, of which only one is the basic assignment operator =, and the remaining 10 are compound assignment operations Symbol, namely: the
basic assignment operator: =.
Compound assignment operators: += (addition assignment), -= (subtraction assignment), *= (multiplication assignment), /= (division assignment), %= (remainder assignment), <<= (left shift assignment), >>= (right shift assignment), &= (bitwise AND assignment), |= (bitwise OR assignment), *A= (bitwise XOR assignment).

The priority of assignment operation is lower, only higher than the comma operator.
Basic assignment =
such as int a=5; means assigning 5 to the integer variable a, which cannot be read as "a equals 5". The left value of the assignment number must be an lvalue, and the right value of the assignment number can be a constant, variable, or expression. The following assignments are correct.

int a,b; //定义整型变量a和b
a=3; //把常量3赋值给a,右值为常量
b=a; //把变量a的值赋给b,右值为变量
b=a+3; //把求和表达式a+3的值赋给b,右值为表达式
以下赋值均是错误的。
int a=2;
3=a; //错误,常量3不能作为左值
const int b=5; //定义整型常变量只读变量b,并初始化为5,其值不能被改变
b=1; //错误,企图改变常变量的值,即常变量不能作左值
复合赋值:+=-=*=/=、%=
a+=b; 等价于 a=a+b;
a-=b; 等价于 a=a-b;
a*=b; 等价于 a=a*b;
a/=b; 等价于 a=a/b;

For example:
int a=5;
a+=3; //Equivalent to a=a+3;
Because the priority of the assignment operator is very low, only higher than the comma operator, the assignment is done at the end.

a+=3+2; equivalent to a=a+(3+2);

Through the following examples, master the above four compound assignment operators.

[Example 1] Analyze the following program and output its running results.

#include<stdio.h>
int main (void)
{
    
    
    int a=l,b=2,c=3; //定义三个整型变量,并初始化
    float d=10.2f; //定义float变量d,用浮点常量10.2初始化
    a+=1; //相当于 a=a+1;即 a=1+1=2
    b-=a+5;
    c*=a-4;
    printf ("%d,%d,%d,%f",a,b,c,d/=a);
    return 0;
}

Code analysis:

  1. float d=10.2f; If it is changed to float d=10.2; Although there is no syntax error, it can run normally, but the general compiler will prompt warning (warning), because the compiler will treat 10.2 and other constants as double constants by default. The type float of d is inconsistent, so a warning appears. Therefore, it can be made clear that 10.2 is a float constant by adding f.

  2. a+=1; Equivalent to a=a+1; Find a to be 2.

  3. b-=a+5; Since the priority of the assignment operator is lower than the arithmetic sum operator, this statement is equivalent to b=b-(a+5);, that is, b=2-(2+5); , Get b=-5;. In the same way, c*=a-4; that is, c=3*(2-4);, so c=-60

  4. printf("%d,%d,%d,%f",a,b,c,d/=a); Since a, b, and c in the output list are all int variables, the output format placeholders are all Is %d; the fourth item in the output list is an expression, the value of which is d=d/a=10.2f/2=5.1, which is a floating-point type, and the output format placeholder is %f, in VC++ 6.0 In the environment, the float type retains 6 digits after the decimal point.

The running result is:
2,-5,-6,5.100000

Unary operator

Logical inverse operation

! The usefulness is that the real becomes fake and the fake becomes real

sizeof

Detailed explanation of sizeof

Logical operator

Logical and &&

  1. When both conditions are true, the result is true;
  2. If one is false, the result is false;
  3. When the first condition is false, the following conditions are no longer judged

Note: When the value participates in the logical AND operation, the result is true, then the second true value will be returned; if the result is false, the first false value will be returned

Logical OR ||

1 As long as one condition is true, the result is true;
2 When both conditions are false, the result is false;
3 When one condition is true, the following conditions are no longer judged

Note: When the value participates in the logical OR operation, the result is true, and the first value that is true will be returned; if the result is false, the second value that is false will be returned;

Guess you like

Origin blog.csdn.net/qq_45849625/article/details/113730238