This is the 17th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021″


Recently, I want to review C language, so I will update an article about C language in nuggets every day! Freshmen who are just learning C, and those who want to review C /C++, don’t miss it! Solid foundation, slow down is fast!


1. Operator classification

Arithmetic operator: + - * / %Copy the code
Shift operator: << >>Copy the code
Operator: & | ^Copy the code
The assignment operator: = += -= *= /= %= &= ^= & *Copy the code
Unary operators:sizeof ! ++ -- 
Copy the code
Relational operators: > >= <= <! = = =Copy the code
Logical operators: && | |Copy the code
Conditional operators:? : the only three-wood operatorCopy the code
Comma expression:.Copy the code
Subscript references, function calls, and structure members: [] (). ->Copy the code

/ – The divisor operator

For the divisor operator:

1. Integers on both sides: Perform integer division regardless of the saved type

int main(a)
{
	int a = 0;
	int b = 0;
	int c = 0;
	printf("Enter two operands :->\n");	/ / 5 2
	scanf("%d %d", &a, &b);
	c = a / b;
	printf("Result: %d\n", c);	/ / 2
	return 0;
}
Copy the code
// If the received type is double
  int main(a)
{
	int a = 0;
	int b = 0;
	double c = 0;
	printf("Enter two operands :->\n");	/ / 5 2
	scanf("%d %d", &a, &b);
	c = a / b;
	printf("Result: %lf\n", c);	// 2.000000 is not 2.500000
	return 0;
}
Copy the code

% – The modulo operator

For the modulo operator: % the elements on either side of the operator can only be integers

int a = 10;
int b = 3;
int c = a % b;
Copy the code

int a = 10;
double b = 3;	
int c = a % b;//err
Copy the code

int a = 10;
int b = 3;
double c = a % b; 	// The receive type can be other types
Copy the code

A % B: The final value is less than B

So:

Rand (): a function that generates random numbers – usually used with the srand() function: random number generator

(See Branch and loop number guessing games for details)

–>srand(unsigned int)time(NULL

Generated random number range: 0-32767

So to get numbers from 0 to 99: rand() %100

To get numbers from 1-100: rand()%100 +1


<< >> Left-shift right-shift operator

<< left-shift operator

>> Right shift operator

The bits that move are binary bits (complements) — only for integers


Left-shift operator <<

Discard the left and add a 0 to the right

To the left is the same thing as multiplying by 2

int c = 4;
c << 1;
printf("%d\n",c);	//4 or 8?
//4 Cause: c << 1 is not received

// If you want the result bit to be 8
 c  = c<< 1;
printf("%d\n",c); 	/ / 8
Copy the code

Right shift operator >>

situation1: logic shift right: left used0Fill, discard case on the right2: arithmetic right shift: sign bits are added on the left and discarded on the rightCopy the code

Currently VS2019 uses arithmetic right shift

int a = 1;
// 00000000 00000000 00000000 00000001 -- Source code
// Positive numbers: the original complement is the same

// If you take the logical right shift
// 00000000 00000000 00000000 00000000 - Complement
// The highest bit is the sign bit: 0
// The source code is 00000000 00000000 00000000 00000000 - The printed result is 0
a = a >> 1;
printf("%d\n",a);	/ / 0
Copy the code

/ / err
int a = 15;
int b = a >> - 1;		// The C standard is undefined
Copy the code

Note: Left/right shift operands can only be integers, not floating-point numbers

float c = 4.5f;
c >> 1;	//err
Copy the code

The complement is full 1 -> %d prints -1

Complement for complement -> source code


About & | ^

Can only be used in shaping data (positive or negative)!

& click with

 1001
&1111
-----
 1001   
If both bits are 1, the result is 1
Copy the code

| or by location

 1001
|1111
-----
 1111  
If both bits are 0, the result is 0
Copy the code

^ Bitwise xOR

 1001
^1111
-----
 0110
// Perform the xOR operation on the corresponding bit bit. The same bit bit is 0 and different bits are 1
Copy the code

A binary sequence that flips a particular bit, that is, xor on which that particular bit is 1 and the other bits are 0

Such as: X:1100 0011Flips the third to last bit1100 0011Exclusive or on0000 0100= = >1100 0011
    ^0000 0100
    ----------
     1100 0111So that flips the third from the bottom of XCopy the code
To flip a particular bit: the bit that corresponds to X to flip, the corresponding bit of this number is1And the other bits are0, the number of times and X corresponding xor can be such as: X =10101110Make X is low4Byte bit reversal, X ^0000 1111Can = = >1010 1110 
    ^0000 1111= = >1010 0001- > x is low4A turned overCopy the code

2. How to get the last bit of binary sequence is 1 or 0

First, the integer is stored in memory as a complement

Method: You can know whether the last bit is 0 or 1 by setting the bit to 1


If the last bit is 1: result 1; otherwise, 0

Binary sequence of 1:00000000 00000000 00000000 00000001

Sign bit is 0, positive: the original complement is the same

The other bits are 0, so the result is 0

int a  =15;
// 00000000 00000000 00000000 00001111 -> A's complement
//&00000000 00000000 00000000 00000001 ->1's complement
--------------------------------------
// 00000000 00000000 00000000 00000001 -> Result is 1
    // The last bit of a is 1
Copy the code
int main(a)
{
    int a = 0;
    int b = 0;
    scanf("%d",&b);
    a = b &1;
    printf("The last bit of the %d complement is %d",b,a);
}
Copy the code

That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!