1, Arithmetic operator
Arithmetic operators + - * /%
//% - Take mold
//& modulo operators can only be used for integer types
int main() { int ret1 = 9 / 2; printf("%d\n", ret1); // 4 // For both sides of / (division sign) are integers, integer division is performed double ret2 = 9 / 2.0; printf("%lf\n", ret2); // 4.500000 return 0; }
2, Shift left operator shift right operator
1. Binary sequence
Shift operator, which moves binary bits
There are three binary representations of integers: original code, inverse code and complement code
- Positive integer - the original code, inverse code and complement are the same
- negtive integer:
Source code - a binary sequence written directly according to the positive and negative of a number
Inverse code - the sign bit of the original code remains unchanged, and the other bits are reversed by bit
Complement - inverse + 1
Integers are stored in memory as binary complements
int main() { int a = 5; // 4byte = 32bit int b = a << 1; //0000000000000101 - positive integers are the same as the original inverse complement //00000000000000000000000000000101 //00000000000000000000000000000101 int c = -1; int d = c << 1; //1000000000000001 -- original code of 1 //11111111110 - 11111111111110 //11111111111111111111111111111111111111 - complement return 0; }
2. Shift left operator
Discard on the left and fill 0 on the right
int main() { int a = 5; int b = a << 1; printf("%d\n", b); // 10 int c = -1; int d = c << 1; printf("%d\n", d); // -2 print the value of the original code //11111111111111111111111111111111111111111 -- 1 Complement // (1) 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 //111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 //11111111111111111111111111111111111101 - reverse //1000000000000010 - Original - 2 return 0; }
3. Shift right operator
3.1 logic operation / arithmetic operation
There are two kinds of shift right operations:
-Logical shift – discard on the right and fill 0 on the left
-Arithmetic shift – discard the right and fill the original symbol bit on the left (VS2019 uses arithmetic shift right)
int main() { int a = -1; int b = a >> 1; printf("%d\n", b); //11111111111111111111111111111111111111111 -- 1 Complement //()1111111111111111111111111111111(1) // If it is a logical shift, fill in 0 // If it is an arithmetic shift, complement 1. Whether the result is - 1 (currently an arithmetic shift) return 0; }
3.2. For the shift operator, do not move the negative digit, which is not defined in the standard
int main() { int a = 15; int b = a >> -1; // err - defines the behavior for the standard return 0; }
3.3. For shift operator, the operand must be an integer
int main() { float c = 4.5f; c >> 1; // err }
3, Bit operator
Their operands must be integers
&Bitwise AND
|Bitwise OR
^Bitwise XOR
1. & bitwise AND: as long as there is 0, it is 0, and both are 1 at the same time
int main() { int a = 3; int b = -2; int c = a & b; printf("%d\n", c); // 00000000000000000000000000000011 - 3 // 100000000000010 -- original code of 2 // 11111111111111111111111111111111111111101 -- the inverse of 2 // 11111111111111111111111111111111111111111110 -- the complement of 2 // 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 11 - 3 Complement // 11111111111111111111111111111111111111111110 -- the complement of 2 // 00000000000000000000000000000010 - a&b // %d - indicates that we want to print the value of c in signed form // 00000000000000000000000000000010 - a&b // The positive number is the same as the original inverse Complement 2 return 0; }
2. | bitwise OR: as long as there is 1, it is 1
int main() { int a = 3; int b = -2; int c = a | b; printf("%d\n", c); // 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 11 - 3 Complement // 11111111111111111111111111111111111111111110 -- the complement of 2 // 11111111111111111111111111111111 - a|b // Print in% d // The complement is calculated, and the highest bit is negative. The original code should be calculated // 11111111111111111111111111111111111 - a|b complement // 11111111111111111111111111111111111110 - reverse // 1000000000000001 - Original - 1 return 0; }
3. ^ bitwise XOR: 0 for the same, 1 for the different
int main() { int a = 3; int b = -2; int c = a ^ b; // Binary XOR printf("%d\n", c); // 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 11 - 3 Complement // 11111111111111111111111111111111111111111110 -- the complement of 2 // 11111111111111111111111111111101 - a^b // %d print negative calculation original code // 11111111111111111111111111111111111111101 - a^b supplement // 11111111111111111111111111111100 - reverse // 1000000000000011 - Original - 3 return 0; }
4. Use:
4.1. What is the lowest bit of the binary sequence of a
int main() { int a = 15; a & 1; // 00000000000000000000000000001111 - 15 // 00000000000000000000000000000001 - 1 // 00000000000000000000000000000001 - a&1 return 0; }
4.2. The number of complement binary sequence bits in memory can be counted 1
int main() { // You can count the number of 1 cycles 32 times of complement binary sequence bits in all memory int a = 15; a & 1; a = a >> 1; // Let everyone have a chance to reach the lowest position return 0; }
5. A perverse interview question
You cannot create a temporary variable (the third variable) to exchange two numbers.
5.1. Use variables:
int main() { int a = 3; int b = 5; int tmp = 0; tmp = a; a = b; b = tmp; printf("a=%d b=%d\n", a, b); return 0; }
5.2. Consumption and reduction
There is a problem of overflow
int main() { int a = 3; int b = 5; a = a + b; b = a - b; // And minus b is a, where b is a a = a - b; // And minus b (now a) is a printf("a=%d b=%d\n", a, b); return 0; }
5.3 bitwise exclusive or
int main() { int a = 3; int b = 5; a = a ^ b; // 011 // 101 b // 110 a^b a b = a ^ b; // 110 a // 101 // 011 a^b b a = a ^ b; // 110 // 011 b // 101 a // a^b gets a password and the original a ^ gets the original b password and the original b ^ gets the original a printf("a=%d b=%d\n", a, b); return 0; }
The readability of XOR code is not good enough, and it is only suitable for shaping
Conclusion:
The binary bits corresponding to a^a=0 are the same and are all 0
0^a=a 000 011–011
aab=b a^a=0 0^b=b
aba=b
XOR supports commutative law
4, Assignment operator
1. The assignment operator can assign values continuously (not recommended)
int main() { int a = 10; int x = 0; int y = 20; a = x = y + 1; // continuous assignment // Clear and easy to debug x = y + 1; a = x; return 0; }
2. Compound assignor
+=
-=
*=
/=
%=
>>=
<<=
&=
|=
^=
int main() { int a = 10; a = a >> 1; a >>= 1; // Compliance assignment a = a + 10; a += 10; return 0; }
5, Monocular operator
An operator with only one operand
! Logical reverse operation
Negative - + positive
&Get address
Type length of sizeof operand in bytes
~Bitwise negation of a number
Front, rear --++
Indirect access operator (dereference operator)*
(type) cast
1,! True to false false to true
int main() { int a = 5; int b = !a; printf("%d\n", b); // 0 int c = 0; int t = !c; printf("%d\n", t); // 1 return 0; }
int main() { int a = 10; if (a) // a is true print hehe { printf("hehe\n"); } if (!a) // a is false print haha { printf("haha\n"); } return 0; }
2. - negative + positive
int main() { int a = 10; a = -a; printf("a=%d\n", a); // -10 a = -a; printf("a=%d\n", a); // 10 return 0; }
3. & get address * dereference operator
int main() { int a = 10; int* p = &a; *p = 20; printf("%d\n", a); return 0; }
*p left value right value
Lvalue - space pointed to
Right value - content in space
int main() { int a = 10; int* p = &a; int b = *p; // The value pointing to that space is assigned to b *p = 20; // Find the space of a by dereferencing and change the space to 20 printf("%d\n", a); return 0; }
Arrays can also be used&
int main() { int arr[10] = { 0 }; arr+1; // Take the address of the first element &arr[0]+1; // Take the address of the first element &arr+1; // Fetch the address of the array // These three addresses are the same, but have different meanings // Add 1, the first two add 4, and the last one add 40 return 0; }
4,sizeof
sizeof is an operator, not a function
sizeof is the memory size of the calculation variable or type creation variable. The unit is bytes. It has nothing to do with what data is stored in memory
int main() { char arr[10] = "abc"; printf("%d\n", sizeof(arr)); // 10 printf("%d\n", strlen(arr)); // 3 is the length of the string. It calculates the number of characters that appear before \ 0 return 0; }
int main() { int a = 10; printf("%d\n", sizeof(a)); // 4 printf("%d\n", sizeof(int)); // 4 return 0; }
The expression inside sizeof does not participate in the operation
int main() { int a = 5; short s = 10; printf("%d\n", sizeof(s = a + 2)); printf("%d\n", s); // The expression inside sizeof does not participate in the operation! // The source file sizeof becomes an executable program test Exe will be compiled and linked Run after exe // Finally, depending on the type of short, the size is 2. When the link is already 2, there is no chance to execute at run time // 2 10 return 0; }
5. ~ bitwise negation of a number (all bits)
int main() { int a = 0; int b = ~a; printf("%d\n", b); // 00000000000000000000000000000000 // 11111111111111111111111111111111111111 -- the binary complement of 1 is all 1 // Print% d to original code // 11111111111111111111111111111111111110 - reverse // 1000000000000001 - Original - 1 return 0; }
Change the number of binary bits and then change it back
int main() { int a = 13; // 0000000000000000000000 1101 - the positive number is the same as the original inverse complement a |= (1 << 1); printf("%d\n", a); // 1 shift left 1 000000000000000000000000000000 10 // Press bit or wait until a 0000000000000000000000001111 - 15 // 0000000000000000000000001111 - 15 a at this time // 11111111111111111111111111111111111111111101 just use this & and you can change it back // This binary sequence is obtained by bitwise inversion of 1 < < 1 a &= (~(1 << 1)); printf("Change back%d\n", a); return 0; }
6. - + + pre, post --++
Post + +, use first, then++
Pre + +, first + +, then use
int main() { int a = 10; int b = a++; // Post + +, use first, then++ printf("%d\n", b); // 10 printf("%d\n", a); // 11 int c = 10; int e = ++c; // Pre + +, first + +, then use printf("%d\n", e); // 11 printf("%d\n", c); // 11 return 0; }
Wrong writing
//err int main() { int a = 1; int b = (++a) + (++a) + (++a); // err printf("b=%d\n", b); // The results are different on different compilers return 0; }
7. (type) cast
int main() { int a = (int)3.14; // By default, the floating-point number written out is double printf("%d\n", a); return 0; }
sizeof and array what does this code output
#include <stdio.h> void test1(int arr[]) { printf("%d\n", sizeof(arr));//(2) } void test2(char ch[]) { printf("%d\n", sizeof(ch));//(4) } int main() { int arr[10] = { 0 }; char ch[10] = { 0 }; printf("%d\n", sizeof(arr));//(1) printf("%d\n", sizeof(ch));//(3) test1(arr); test2(ch); return 0; }
The array name is placed separately inside sizeof. The array name represents the entire array, and the size of the entire array is calculated
&Array name. The array name represents the entire array, and the address of the entire array is taken out
10 shaping 40 bytes. 10 characters 10 bytes
The address of the first element of the array parameter is essentially void test1(int* arr), which is 4 / 8 of the pointer size
40 4 10 4
6, Relational operator
>
>=
<
<=
!= Used to test "inequality"
==Used to test equality
- For variables of the same type
- Note the error caused by = = and = misspelling
7, Logical operator
&&Logic and
||Logical or
1. Only focus on true and false
The true of logic and judgment is represented by 1 and the false is represented by 0
Logic and both are true is 1 logic or as long as one is true is 1
int main() { int a = 0; int b = 5; int c = a && b; // Logic and int e = a || b; // Logical or printf("c=%d\n", c); // 0 printf("e=%d\n", e); // 1 return 0; }
Distinguish logical and bitwise AND
Distinguish logical or and bitwise OR
1&2 ----0
1&&2---->1
1 | 2----->3
1 || 2---- > 1
2. Characteristics of logic and and or:
360 written questions
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; }
//a + + is used first and then + +, so 0 is false and no operation is performed later. a + + is 1 bcd and does not change 1 2 3 4
//For logic and, the left is false, and the right does not need to be calculated
int main() { int i = 0, a = 1, b = 2, c = 3, d = 4; i = a++ && ++b && d++; printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d); // Change a=1, 2, 3, 5 return 0; }
int main() { int i = 0, a = 1, b = 2, c = 3, d = 4; i = a++ || ++b || d++; printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d); // A + + is false. Don't forget it. Only a + + has 2 2 3 4 return 0; }
int main() { int i = 0, a = 0, b = 2, c = 3, d = 4; i = a++ || ++b || d++; printf("a = %d\n b = %d\n c = %d\nd = %d\n", a, b, c, d); // 1 3 4 AB self increasing return 0; }
8, Conditional operator (trinocular operator)
exp1 ? exp2 : exp3
Use conditional expressions to find the larger of two numbers
int main() { int a = 3; int b = 5; int m = 0; m = (a > b ? a : b); // If a > b, assign a to m, otherwise assign B to M // (a > b) ? (m = a) : (m = a); }
9, Comma expression
exp1, exp2, exp3, ...expN
Comma expressions are multiple expressions separated by commas.
Comma expression, executed from left to right. The result of the entire expression is the result of the last expression.
int main() { //Code 1 int a = 1; int b = 2; int c = (a > b, a = b + 10, a, b = a + 1);//comma expression //Code 2 if (a = b + 1, c = a / 2, d > 0) // The last one is the judgment condition of if //Code 3 a = get_val(); count_val(a); while (a > 0) { //Business processing a = get_val(); count_val(a); } //If you use a comma expression, override: while (a = get_val(), count_val(a), a > 0) { //Business processing } }