1. Operators1. Arithmetic OperatorsThe commonly used arithmetic operators are: + – * / %, except for the % operator, the other operators are applicable to both floating-point and integer types. The rule for the / (division) operator is: if either side has a decimal, the result is a decimal; if both sides are integers, the result is also an integer, taking the quotient. The % operator is the modulus operator, which takes two integer operands, divides the left operand by the right operand, but returns the remainder instead of the quotient.2. Shift OperatorsThere are two shift operators, the left shift operator << and the right shift operator >>, which shift the operand on the left to the position specified by the operand on the right.Shift operators essentially operate directly on the binary gears, sliding the bit pattern as a whole to the left or right. Left shift always means “fill 0 on the right, discard overflow on the left.” Right shift can be more dramatic; it can be logical right shift (filling 0 on the left), or it can bearithmetic right shift (filling the “sign bit” on the left, maintaining the sign). Arithmetic right shift behaves like pulling the sign bit along, ensuring that the negative sign does not get lost along the way when dealing with negative numbers.3. Bitwise OperatorsBitwise operators include: & | ~, three in total, and they require the operands to be integers, performing specified operations on the corresponding bits of the operands.4. Assignment OperatorsThe assignment operator is represented by a single =. space = value, in principle, the types on both sides of = should be consistent; if they are not, the left side prevails. Assignment is an expression, not a statement, and should not be used with the assignment operator in conditional statements.5. Relational OperatorsThe commonly used relational operators are: >, >=, <, <=, !=, ==,The result of relational operator operations: 1 for true, 0 for false.The != operator is used to test “not equal,” while the == operator tests for “equal.” They are typically used as test expressions in if or while statements.6. Logical OperatorsLogical operators include: && || !.
&&: Logical AND expression: means simultaneously, and. Format: expression1 && expression2.
Rule: If expression1 is true, evaluate expression2; if 2 is also true, the entire expression is true.
If expression1 is true, evaluate expression2; if 2 is false, the entire expression is false.
If expression1 is false, expression2 is not executed, and the result is directly false.
“All true is true; if there is a false, it is false.”
||: Logical OR expression: means or. Format: expression1 || expression2
Rule: If expression1 is true, there is no need to evaluate expression2; the entire expression is true.
If expression1 is false, or expression2 is true, the entire expression is true.
If both expression1 and expression2 are false, the entire expression is false.
“All false is false; if there is a true, it is true.”
!: Logical NOT expression: takes the opposite value. Format: !expression.
Rule: If the expression is true, it outputs false.
If the expression is false, it outputs true.
“True becomes false, false becomes true.”
7. Other Types of Operators
Commonly, there are also increment and decrement operators, ternary operators, and compound assignment operators.
7.1 ++ and –: Increment and Decrement OperatorsThese two operators will increase or decrease the value of a variable by 1.There are two forms when used: prefix (++a) and postfix (a++).
① As a standalone statementthere is no difference between prefix and postfix; both directly change the variable by 1.
② As part of an expressionprefix means increment first, then use; postfix means use first, then increment.
7.2 Ternary Operator ?:Format:<span><span>expression1 ? expression2 : expression3</span></span>The rule is: first check if expression1 is true;If true → the result is expression2;If false → the result is expression3.It is commonly used to replace simple conditional checks instead of if-else.
7.3 Compound Assignment OperatorsCombine operations and assignments into one step, for example:Arithmetic types:<span><span>+= -= *= /= %=</span></span>Bitwise types:<span><span>&= |= ^= <<= >>=</span></span>
The effect is equivalent to:<span><span>a += 5</span></span> is equivalent to <span><span>a = a + 5</span></span>, but the syntax is more concise and the efficiency is more intuitive.