<> catalogue

* Operator
1.1 Arithmetic operator
1.2 Shift operators
1.3 Bitwise operators
1.4 Assignment operator
1.5 unary operator
1.6 Relational operator
1.7 Logical operator
1.8 Conditional Operator
1.9 comma expression
1.10 comma expression
* summary
<> one , Operator

Definition of operators Simply put, an operator tells the compiler to perform a specific mathematical operation , Logical operation , Symbols for bit operations

There are many kinds of operators :

<>1.1 arithmetic operator
+( plus ) -( reduce ) *( ride ) /( except ) %( Residual )
C In language , Arithmetic operator “%” Representative module ( Residual ) Operation namely

5 Divide and 2, merchant 2 more than 1. because % Is residual , So the output result is 1. Note that the operands at both ends of the remainder operator ( As in the example 5 and 2 namely % Left and right operands of ) Must be an integer . Cannot execute if one or both of them are floating point numbers . for example :

application /( division sign ) It should be noted that when both ends are integers , Integer division is performed . Namely

When one or both ends are floating point numbers , To divide floating point numbers . Namely

<>1.2 Shift operators
>>( Shift right ) <<( Shift left )
<<( Shift left ) Is to move all bits of a number to the left by several bits . for example
int x=16; printf("%d\n",x<<1);
First x Convert to binary 10000, Read the last bit (0, Or according to whether there has been a shift ), output 100000, mean 32;
Shift left , If the number of digits is moved K Equal to or greater than data length N, Usually only move K mod N digit (mod Function is a complement function , So a number mod n, Is this number divided by n Remainder after .)
Therefore, shifting one bit to the left is equivalent to multiplying the value by 2
( Shift right ) Is to move all bits of a number to the right by several bits . for example
int x=16; printf("%d\n",x>>1);
First x Convert to binary 10000, Don't read the last digit , output 1000, mean 8;
One shift to the right is the value divided by 2

<>1.3 Bitwise operators
&( Bitwise AND ) ^( Bitwise XOR ) |( Bitwise OR )
c language & Symbols have three meanings :

1,& Symbol refers to the address character , Is a unary operator .
When you define a variable a, I want to know the address of the variable in memory , You can use this symbol . This symbol refers to addressing , stay C Language pointers are very common in this chapter .
2,& Symbols are bitwise operators , express “ Bitwise AND ”, Is a binocular operator .
& Symbols can also represent “ Bitwise AND ”. When calculating the address , It's all binary numbers , If the value on this bit of both operands is 1, So, where the result is calculated, the result is 1, Otherwise, the result is 0.
3, When two & Symbols used at the same time , Namely && Symbol , Represents the logical and in logical operations .

&& Symbols can represent logical and in logical operations . stay “ And or not ” in ,& It refers to two comparison objects , If one true and one false , The result is 0, If both comparisons are true , That is 1, If both are false , The result is 0.

C In language ,0 Representative leave , wrong 0 Represents truth .

1 & 1 = 1, 1 | 1 = 1, 1 ^ 1 = 0

1 & 0 = 0, 1 | 0 = 1, 1 ^ 0 = 1

0 & 1 = 0, 0 | 1 = 1, 0 ^ 1 = 1

0 & 0 = 0, 0 | 0 = 0, 0 ^ 0 = 0

stay c In language ^ It means bitwise XOR . Mainly used in binary . First convert two integers to binary digits .
9^5=000010001^00000101=00001100.// The result is 12 #include<stdio.h> int main() { int a=9;
int b=5; printf("%d\n",a^b); return 0; }

In each corresponding digit , Only the upper one of the two bits is 1 perhaps 0, And the other must be the opposite , Then the result of this bit operation is 1. Otherwise, the result is 0.
Bit operators are all about binary , Xiao Mu hasn't learned binary yet , It cannot be explained , These will be answered in detail later .

<>1.4 Assignment operator
= += -= *= /= &= ^= |= >>= <<= int a=0;// initialization a=20;// assignment a=a+3;// Can be written as a+=3// here a=23
a=a-3;// Can be written as a-=3// here a=17 // and so on , You can generally understand the usage and definition of assignment operators

These will be explained in detail later .

<>1.5 unary operator

A unary operator is simply an operator that has only one operand . for example a+b Medium + There are two operands , Is a binocular operator .
! Logical reverse operation - negative + positive & Fetch address sizeof Type length of operand ( In bytes ) ~ Bitwise negation of a number -- Preposition , Post -- ++
Preposition , Post ++ * Indirect access operator ( dereference operator ) ( type ) Cast type
Indicates logical non , Is a logical operator , Operand is true or false .

If indicated a Not equal to 0, Just write a!=0; hypothesis a be equal to 1, that a!=0; It's true ,true.

Simply put, it means turning the truth into the false , Make the fake come true .C In language 0 Indicates false , wrong 0 Indicates true .

-( negative ),+( positive )

sizeof Is also an operator , Is a monocular operator , Operators can remove parentheses from variables , Not for type , Functions are not allowed for variables and types .

int arr[10]={1};// because 1 Only one place , remainder 9 Locations default to 0 printf("%d\n",sizeof[0]);// The result is 4 Byte size
printf("%d\n",sizeof(arr)/sizeof(arr[0]));//10-- Number of elements of the array
Post ++
int a=10; int b=a++;// Post ++, First a The value of is assigned to b, again a=a+1 printf("%d\n",b);//10 printf("%d\n"
,a);//11

Preposition ++
int a=10; int b=++a;// Preposition ++, before a=a+1, after b=a printf("%d\n",b);//11 printf("%d\n",a);
//11

So post – And pre – And post ++ And pre ++ Same usage , Just a yes - One is +, Self verifiable .
( type )– Cast type
int a=3.14;// This is wrong //3.14 Literal floating point number , The compiler is understood by default as double type int a=(int)3.14; printf("%d\n",
a);// Print 3

<>1.6 Relational operator
> greater than >= Greater than or equal to < less than <= Less than or equal to != For testing “ Unequal ” == For testing “ equal ”

These will be explained in detail later .

<>1.7 Logical operator
&& Logic and || Logical or #include<stdio.h> int main() { int a=10; int b=20; if(a&&b) {
printf(" Give me a compliment !\n”); } return 0; }// Can print out Give me a compliment !// because a and b All true ( wrong 0)
&& Is true when both conditions are true , All other cases are false ;
|| Is true when at least one of the two conditions is true , False only if both conditions are false .
#include<stdio.h> int main() { int a=0; int b=20; if(a||b) { printf(" Give me a compliment !\n");
} return 0; }// Can print out Give me a compliment !// because a and b One of them is true
<>1.8 Conditional Operator

The conditional operator is also called the trinocular operator , That is, there are three operands .
exp1 ? exp2 : exp3
for example

<>1.9 comma expression
exp1, exp2,exp3,..expN
A comma expression is a string of expressions separated by commas , The result of a comma expression is the result of the last expression . for example :

<>1.10 Subscript reference , Function calls and structure members
[] () . -> #include<stdio.h> int main() { int arr[10] = { 1,2,3,4,5,6,7,8,9,10
};// When creating an array arr[] of [] Must be constant //arr[3]=20//[] Is the subscript reference operator ,arr and 3 namely [] Operand of
//a+b,a yes + Left operator of ,b yes + Right operator of int n = 3; arr[n] = 20;// Variables can be used to access elements return 0; } #
include<stdio.h> int x, y; int Add(intx, inty) { return x + y; } int main() {
int sum = Add(2, 3);//() Is the function call operator ,Add,2,3 All operators printf("%d\n", sum); return 0; }
I will explain it in detail later .

<>2. summary

I didn't think it would be easy to code an operator , I learned a lot today , Including many fallible points , For example, when creating an array arr[] Must be constant ,%( Residual ) The two operands of must be integers, etc . So that's all for today's notes . See you next time !

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage Python Study notes ( one )evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possibleRISC-V_GD32VF103-TIMER0 timer interrupt java Array subscript variable _Java Basic grammar : array be based on stm32 Control four-wheel trolley motor drive ( one ) be based on redis Design of liking function Software engineering career planning mysql Query random data by conditions _MySQL Random query of several qualified records centos7 install RabbitMq