C language -- operators and expressions

Keywords: C Back-end visualstudio

Operator

arithmetic operator

+

plus
-reduce
*ride
/except
%Remainder (operand must be an integer)

Arithmetic conversion:

char -> int -> float -> double

Shift operators

Data storage: binary complement

<<Shift left (discard on the left and fill zero on the right)
>>

Shift right

(1) logical shift: fill zero on the left and discard it on the right

  2. Arithmetic shift: fill the sign bit on the left and discard it on the right)

  Cyclic shift:

If it exceeds 32 bits, take the module of 32 bits

>>33   ->   >>1

Negative digits cannot be moved. This behavior standard is undefined! (in compiler)

Negative digit absolute value plus moving actual value = 32

>>-1   ->  >>31

>>-5   ->   >>27

void shift(int ls,int rs)
{
	printf("Shift left——%d\n", ls << 1);
	printf("Shift left unassigned——%d\n", ls);
	ls = ls << 1;
	printf("Left shift assignment——%d\n", ls);

	printf("After moving right——%d\n", rs >> 1);
	printf("Shift right unassigned——%d\n", rs);
	rs = rs >> 1;
	printf("Assignment after shift right——%d\n", rs);
}

Bitwise operators

Operand must be an integer

&Bitwise and (1 if the same is 1)
|Bitwise OR (0 if both are 0)
^Bitwise exclusive or (0 for the same, 1 for the different)
void Anoperator(int a, int b) // a--1 b--2
{
	printf("Bitwise AND——%d\n", a & b);  //0
	printf("Bitwise OR——%d\n", a | b);  //3
	printf("Bitwise XOR——%d\n", a ^ b); //3
}

You cannot create a temporary variable (the third variable) to exchange two numbers.

//You cannot create a temporary variable (the third variable) to exchange two numbers.
//This function can be called by address when changing the argument. This function is called by value
void swap(int a, int b)         
{
	printf("a = %d b = %d\n", a, b);
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
	printf("a = %d b = %d\n", a, b);
}

  Calculates a number in a binary number

int count_One(int n)   //Calculates a number in a binary number
    int count;
    while(n)
    {
        n&=(n-1);
        count++;
    }
    return count;
}

 

Assignment operator

=assignment

Compound assignor

+=Add etc
-=Subtraction
*=Multiply, etc
/=Divide and so on
%=Surplus, etc
>>=Shift right, etc
<<=Shift left, etc
&=Bitwise and equal
|=Bitwise or equal
^=Bitwise XOR
int x=2;
//equivalence
x+=10;
x=x+10;

unary operator

!Logical inversion
-negative
+positive
&Get address
sizeofThe type length of the operand in bytes
~Bitwise negation of a number
--

Front, rear--

++Front, rear++
*Introduction access operator (reference operator)
(type)Cast type

++,-- 

FrontOperands increase (decrease) automatically before use
PostOperands are used first and then increased (decreased) automatically

~Bitwise inversion

-10. The negative value by bit is - 11

Negative numbers are stored as complements in the computer.

-Original code of 10: 10000000 0000000 0000000 00001010

After inversion      : 11111111 11111111 11111111 11110101         The sign bit remains unchanged and its bit is reversed

The computer considers it as a complement and returns it as the original code when printing (complement of negative number: original code - > inverse code + 1 = complement)

Print value      :   ten million   00000000 00000000 00001011  

Relational operator

>greater than
>=Greater than or equal to
<less than
<=Less than or equal to
!=Unequal
==equal

Note:

        == For equality test

        = Assign value to  

Logical operator

&&Logic and
||Logical or

  Short circuit expression:

        &&: When the left is false, the right is not executed

        ||: When the left is true, the right is not executed

#include <stdio.h>
int main()
{
    int i = 0,a=0,b=2,c =3,d=4;
    i = a++ && ++b && d++;
    //i = a++||++b||d++;
    printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d);
    return 0;
}

        i = a++ && ++b && d++;

    i = a++||++b||d++;

Conditional Operator

exp1? exp2:exp3If exp1 is true, execute exp2, otherwise exp3

int max(int a, int b)  // a--1  b--2
{
	return a > b ? a : b;
}

comma expression

exp1, exp2, exp3, ...expN

Multiple expressions are separated by commas and executed from left to right. The result of the whole expression is the value of the last expression.

  When performing multiple operator operations, use () to determine the operation order to avoid different results in different compilers!

Subscript references, function calls, and structure members

Subscript reference []

Array name + subscript

(array section: Array article)

Function call ()

Function name + parameter

(function: Function article)

Structure member

Structure. Member name

Structure pointer - > member name

Expression evaluation

Implicit conversion

The integer arithmetic operation of C is always performed at least with the precision of the default integer type.

To achieve this precision, the characters and short operands in the expression are converted to normal integers before use. This conversion is called integer promotion.

Significance of integer lifting:

The integer operation of the expression shall be executed in the corresponding operation device of the CPU. The byte length of the operand of the integer operator (ALU) in the CPU is generally the byte length of int and the length of the general register of the CPU.

Therefore, even if the addition of two char types is executed by the CPU, it must actually be converted to the standard length of integer operands in the CPU. General purpose CPU is difficult to directly implement the direct addition of two 8-bit bytes (although there may be such byte addition instructions in machine instructions).

Therefore, all integer values whose length may be less than int in the expression must be converted to int or unsigned int before they can be sent to the CPU for operation.

//The shaping and promotion of negative numbers, the sign bit remains unchanged, and the high bit is supplemented by one;

char c1 = -1;

There are only 8 bits in the binary bit (complement) of variable c1: 1 1111111

The result after promotion is: 1 1111111   eleven million one hundred and eleven thousand one hundred and eleven   eleven million one hundred and eleven thousand one hundred and eleven   eleven million one hundred and eleven thousand one hundred and eleven

//Positive integer lifting

char c2 = 1;

There are only 8 bits in the binary bit (complement) of variable c2: 00000001

The result after promotion is: 0 0000000   00000000   00000000   00000001

//Unsigned shaping lifting, high complement 0

void intImprove()
{
	char a = 182;//(Oxb6200) beyond the range of char tables, the integer is negative after promotion;
	short b = 46592;
	int c = 10000000;
	if (a == 100)
		printf("a\n");
	if (b == 46592)
		printf("b\n");
	if (c == 10000000)
		printf("c\n");
}

void intImprove2()
{
	char c = 10;
	printf("%u\n", sizeof(c));
	printf("%u\n", sizeof(+c));
	printf("%u\n", sizeof(!c));
	printf("%u\n", sizeof(c));
}

1

4

1

1

c as long as it participates in the expression operation, the integer is promoted.

Arithmetic conversion

Operators operate on different types of operands, from low type to high type;

The conversion should be reasonable

         Precision loss occurs when the high type is changed to the low type

float f=1.23;
int i=f;   //i=1;

Posted by deezzer on Sat, 23 Oct 2021 01:59:35 -0700