Day 5 of Learning C/C++: Operators in C Language

1. Arithmetic Operators: +, -, *, /, % (modulus also refers to remainder)All the above arithmetic operators are binary operators (the operator is located between two operands).It is important to note that in C language, integer division is always integer division, returning only the integer part and discarding the decimal part, as shown in the figure below. Regardless of the type of value you define, it will only return the integer part;The % operator returns the remainder of the division of two integers; this operator can only be used with integers and not with floating-point numbers.Day 5 of Learning C/C++: Operators in C Language2. Assignment Operator: =When a variable is created with an initial value, it is called initialization; assigning a value to an already created variable is called assignment.The expression a += 3 in the figure below is called a compound assignment operator, which is equivalent to a = a + 3.Day 5 of Learning C/C++: Operators in C Language3. Increment Operators: ++ and —++ is an increment operator, which can be further divided into prefix ++ and postfix ++; — is a decrement operator, also divided into prefix — and postfix –.Prefix ++: increment by 1 first, then assign; Postfix ++: assign first, then increment by 1; — follows the same principle, as shown in the figure below:Day 5 of Learning C/C++: Operators in C Language4. Shift Operators: Left Shift (<<), Right Shift (>>)Shift operators move the binary representation of data left or right by a specified number of bits.Left Shift: When shifting left, the binary bits move left by one position, and 0 is filled in on the right.For example: a = 5, binary representation is: 0000 0101After left shifting by 1 bit: 0000 1010, which is a = 10;After left shifting by 2 bits: 0001 0100, which is a = 20;In summary, left shifting is equivalent to multiplication: a << n is equivalent to a × 2ⁿ.Right Shift: When shifting right, the binary moves right by one position, and 0 is filled in on the left.For example: a = 10, binary representation is: 0000 1010After right shifting by 1 bit: 0000 0101, which is a = 5;After right shifting by 2 bits: 0000 0010, which is a = 2;In summary, right shifting is equivalent to division: a >> n is approximately equivalent to a ÷ 2ⁿ (rounded towards zero),as shown in the figure below:Day 5 of Learning C/C++: Operators in C Language5. Bitwise Operators: &, |, ^, ~Bitwise operators operate directly on the binary bits (bits) of integers in memory.Bitwise AND (&): The result is 1 only when both corresponding bits of two numbers are 1; otherwise, it is 0;Bitwise OR (|): The result is 1 if at least one corresponding bit of two numbers is 1; it is 0 only when all corresponding bits are 0;Bitwise XOR (^): The result is 0 if the corresponding bits of two numbers are the same; it is 1 if they are different;Bitwise NOT (~): Inverts each bit of a number in its binary form.For example: a = 5, b = 3, a = 0000 0101, b = 0000 0011a & b = 0000 0001 = 1; a | b = 0000 0111 = 7;a ^ b = 0000 0110 = 6; ~a = 1111 1010 (in two’s complement),0000 0110 (in original form), the sign bit is 1, so ~a = -6. As shown in the figure below:Day 5 of Learning C/C++: Operators in C Language6. Relational Operators: >, >=, <, <=, == (equal to), !=Relational operators are used to compare the relationship between two values and return a boolean value (true or false); they are binary operators. As shown in the figure below:Day 5 of Learning C/C++: Operators in C Language7. Logical Operators: Logical AND (&&), Logical OR (||), Logical NOT (!)Logical operators are used to combine multiple conditions and return a boolean value (true or false).Logical AND (&&): The result is true only when all operands are true.Logical OR (||): The result is true if at least one operand is true.Logical NOT (!): Inverts the logical value of the operand (true becomes false, false becomes true).8. Comma Operator: (,)The comma operator (,) connects multiple expressions, and it is a binary operator. It executes the expressions in order from left to right and returns the result of the last sub-expression.Day 5 of Learning C/C++: Operators in C Language

Leave a Comment