Chapter V basic operations

Keywords: C++ DirectX

C + + language uses operators and expressions to manipulate data. Operators are symbols used to represent various operations in programs. The high-level language uses a method similar to mathematical calculation to represent various operations, such as y = x + 10; Where y and x represent variables and = and + represent operators. An expression is composed of operators, operating variables and separators according to certain rules. X + 10 above is an expression. We assign the result of its operation to variable y. The concept of operator and expression comes from mathematics. This operation method is quite natural and easy for programmers to understand! The basic operation methods supported by C + + language include arithmetic operation, relational operation, logical operation, assignment operation, bit operation, etc. in fact, they are distinguished according to operators.

1. Assignment operation and assignment expression: save the operand or expression result on the right to the storage space marked by the variable on the left.

	int y = 10;					    // Declare the variable y and give it an initial value of 10
	int x = 20;					    // Declare the variable x and assign the initial value 20
	y = x + 10;					    // If you add 10 to the value represented by x and assign it to y, y will change from 10 to 30
	std::cout << y << std::endl;	// Output 30
	y = y + x;					    // Add the value represented by x and the value represented by Y, and assign it to y, then y becomes 50
	std::cout << y << std::endl;	// Output 50
	y += x;					        // Equivalent to y = y + x;
	std::cout << y << std::endl;	// Output 70

The assignment operation is the = equals sign. In C + +, there are a class of assignment operators that integrate operation and assignment functions, such as + =, - =, * =, / =,% =, < =, > > =, & =, | =, ^ =. What they mean is to perform operations first and then assign values.

Note: variable is the symbol of memory unit storing data. If variable is used to store and access data, the process of variable assignment is the process of putting data into memory unit.

2. Arithmetic operation and arithmetic expression: realize addition, subtraction, multiplication, division, and modular calculation.

	x = 30;
	y = 10;

	y = x - 10;						        // Subtraction operation
	std::cout << y << std::endl;			// Output 20

	y = x * 10;						        // Multiplication
	std::cout << y << std::endl;			// Output 300

	y = x / 10;						        // Division operation
	std::cout << y << std::endl;			// Output 3

	y = x / 4;						        // The result of integer and integer operations is an integer
	std::cout << y << std::endl;			// It should be 7.5, but output 7

	y = x / 4.0;						    // The result of integer and decimal operations is decimal
	std::cout << y << std::endl;			// It should be 7.5, but output 7 because y is an integer type

	float z = 0;
	z = x / 4.0;						    // Use floating point data to receive decimal results
	std::cout << z << std::endl;			// Output 7.5

	y = x % 4;						        // Modulo operation, also known as remainder operation, if you can divide it, the result is 0
	std::cout << y << std::endl;			// Output 2 because the remainder of 30 ÷ 4 is 2

	y++;							        // Equivalent to y = y + 1;
	std::cout << y << std::endl;			// Output 3
	
	--y;							        // Equivalent to y = y - 1;
	std::cout << y << std::endl;			// Output 2

Modulo operation is to calculate the remainder of the division of two integers, such as 3% 2 = 1

The operand of the division operator can be an integer or a real number. An integer divided by an integer gets an integer, a real number divided by a real number gets a real number, and the result of an integer divided by a real number or a real number divided by an integer is a real number. In addition, the divisor cannot be zero.

Self increasing (+ +) and self decreasing (- -) operators are monocular operators. Their operands can only be variables. Their function is to add or subtract 1 on the basis of variables. Operators before and after variables will only affect their return values!

3. Relational operation and relational expression: the operation of comparing the size of operands is relational operation.

	bool m = 1 > 2;				    // Use bool type to receive relational operation results
	std::cout << m << std::endl;	// The output 0 represents false, that is, false
	
	m = 2 > 1;
	std::cout << m << std::endl;	// Output 1 represents true, that is, true

	x = 10;
	y = 20;
	m = x == y;
	std::cout << m << std::endl;	// The output 0 represents false, that is, false

	m = x == y - 10;
	std::cout << m << std::endl;	// Output 1 represents true, that is, true
	
	m = y != (x - 5) * 4;			// In fact, the two are equal, but our operator is not equal
	std::cout << m << std::endl;	// The output 0 represents false, that is, false

Note: relational operators include <, < =, >, > =, = ==

Note: the result of relationship calculation is a logical Boolean value: true or false. True is 1 and false is 0.

4. Logical operation and logical expression:! Yes no (reverse) operation, & & yes and, | yes or operation

	bool n = 1 && 2;				        // Both sides of the operator are true, and the result is true
	std::cout << n << std::endl;			// Output 1

	n = 1 && 0;					            // 0 is false, non-0 is true
	std::cout << n << std::endl;			// Output 0

	x = 9;
	y = 11;
	n = x > 10 && y > 10;			
	std::cout << n << std::endl;			// Output 0

	n = x > 10 || y > 10;				    // One on either side of the operator is true, and the result is true
	std::cout << n << std::endl;			// Output 1

	n = !0;						            // Negative operator, false negative is true
	std::cout << n << std::endl;			// Output 1

	n = !(x > 10) && y > 10;
	std::cout << n << std::endl;			// Output 1

Note: the result of a logical operation is a logical Boolean value: true or false

Note: C + + saves logical Boolean values as integer values. true is 1 and false is 0

Note: & & and | operations are evaluated from left to right&& If one expression in | is false, the whole result is false, and if one expression in | is true, the whole result is true.

5. Bit operation and bit expression: including shift left < < shift right > >, inverse ~, and &, or |, XOR ^ six operators.

char i = 65;
char j = i << 2;
std::cout << (int)j << std::endl;		// Output 4

// 00110011 & 00111100 = 00110000
char s1 = (char)51 & (char)60;
std::cout << (int)s1 << std::endl;		// Output 48
long long s2 = convertDecimalToBinary(s1);
std::cout << s2 << std::endl;		    // Output 110000

// 00110011 | 00111100 = 00111111
s1 = (char)51 | (char)60;
std::cout << (int)s1 << std::endl;		// Output 63
s2 = convertDecimalToBinary(s1);
std::cout << s2 << std::endl;		    // Output 111111

// 00110011 ^ 00111100 = 00001111
s1 = (char)51 ^ (char)60;
std::cout << (int)s1 << std::endl;		// Output 15
s2 = convertDecimalToBinary(s1);
std::cout << s2 << std::endl;		    // Output 1111

6. Other operators

The Sizeof operator gets the number of bytes of the result of a data type or expression.

int a = sizeof(int);
std::cout << a << std::endl;		// Output 4, indicating that the int data type accounts for 4 bytes

a = sizeof(100);
std::cout << a << std::endl;		// Output 4, indicating that the constant number 100 is an int integer

double b = 3.14;
a = sizeof(b);
std::cout << a << std::endl;		// Output 8, indicating that variable b occupies 8 bytes

The typeid operator is used to get the data type or the data type of the result of an expression

int c = 20;
std::cout << typeid(c).name() << std::endl;			    // Output int
std::cout << typeid(c > 10).name() << std::endl;		// Output bool

Conditional operator?: Is the only ternary operator. The usage method is: < expression 1 >< Expression 2 >: < expression 3 > if the resu lt of table < expression 1 > is true, execute < expression 2 >, otherwise execute < expression 3 >

int d = 20;
int e = d >= 20 ? 25 : 15;
std::cout << e << std::endl;	// Output 25

Operators have priority. For example, multiplication and division operators are higher than addition and subtraction operators, which is in the same order as our mathematical arithmetic (multiplication and division before addition and subtraction).

int f = 1 + 2 * 3;				// Multiplication and division before addition and subtraction
std::cout << f << std::endl;	// Output 7

The approximate order of precedence of operators is as follows: self addition / self subtraction >   Multiplication and division > addition and subtraction > relation > Logic > condition > assignment

Suggestion: generally, we will not construct a very complex operation expression, which contains many variables and operators. Instead, we will split it into several sub expressions for independent operation, or use parentheses to isolate it into several sub expressions. In this way, the complex expression can clearly reflect its logical meaning.

Note: This is the first course in our series of game development tutorials. It is mainly the basic learning of programming language. Give priority to learning C + + programming language, then c# language, and finally Java language. Then use C + + and DirectX to introduce some basic theoretical knowledge in game development. The second course in our game development series is the study of Unity game engine. If there are some mistakes in the course, please leave a message and correct them. Thank you very much!

Posted by northcave on Mon, 20 Sep 2021 07:40:49 -0700