← 返回资讯屏轩教育 · 行业资讯

Programming Operators: Arithmetic, Relational, Logical and Bitwise | 编程操作符:算术、关系、逻辑与位运算

2026-08-02 15:23 · 本文部分内容由AI辅助创作

📚 Programming Operators: Arithmetic, Relational, Logical and Bitwise | 编程操作符:算术、关系、逻辑与位运算

In A-Level Computer Science, operators are the building blocks of expression evaluation. They define how data is manipulated, compared and combined. An operator works on one, two or three operands to produce a result. Understanding their behaviour, precedence and side effects is crucial for writing correct and efficient programs. Whether you are calculating a remainder, testing a complex condition or shifting bits inside a register, operators link your variables into meaningful instructions.

在 A-Level 计算机科学中,操作符(运算符)是表达式求值的基本构件,它们定义了数据的操作、比较与组合方式。一个操作符作用于一个、两个或三个操作数,产生一个结果。理解操作符的行为、优先级和副作用,对于编写正确高效的程序至关重要。无论你是在计算余数、测试复杂条件,还是对寄存器中的位进行移位,操作符都将变量连接成有意义的指令。


1. Introduction to Operators | 操作符简介

sified by the number of operands they require: unary (one operand, e.g. NOT or negation), binary (two operands, e.g. + or ==) and ternary (three operands, mostly the conditional operator in some languages). Edexcel specifications focus on arithmetic, relational, Boolean logical, bitwise and assignment operators, alongside their precedence rules.

操作符是一个符号或关键字,指示计算机执行特定的数学、关系或逻辑操作。按所需操作数的数量,操作符可分为:一元操作符(一个操作数,如逻辑非、负号)、二元操作符(两个操作数,如加号、等于)和三元操作符(三个操作数,主要是某些语言中的条件操作符)。Edexcel 大纲重点考察算术、关系、布尔逻辑、位操作符和赋值操作符,以及它们的优先级规则。

In high-level programming languages such as Python, C#, Java or pseudocode used in exams, the same operator may behave differently depending on operand types. For instance, the plus sign can denote addition for numbers or concatenation for strings. Operators are fundamental to constructing algorithms, controlling flow and manipulating data at the bit level.

在 Python、C#、Java 等高级语言或考试所用的伪代码中,同一个操作符可能根据操作数的类型表现出不同的行为。例如,加号可以表示数字相加,也可以表示字符串拼接。操作符是构建算法、控制流程和进行位级数据操作的基础。


2. Arithmetic Operators | 算术操作符

vision (÷) and modulus (MOD). Integer division (DIV) is also significant when both operands are integers, yielding a truncated quotient. In most programming languages, the forward slash (/) may return a float, while // or DIV gives integer division.

算术操作符执行标准的数学计算。核心集合包括加法(+)、减法(−)、乘法(×)、除法(÷)和取模(MOD)。当两个操作数都是整数时,整除(DIV)也很重要,它给出截断的商。在大多数编程语言中,正斜杠(/)可能返回浮点数,而 // 或 DIV 则执行整数除法。

mber to a power. Arithmetic operators are binary except for the unary minus (negation). Care must be taken with division by zero, which causes a runtime error.

取模运算返回整数除法后的余数。例如,17 MOD 5 的计算结果为 2。指数运算通常写作 ** 或 ^,用于计算幂。除了一元负号(取反)外,算术操作符都是二元的。必须小心零除问题,这会导致运行期错误。

中文对应:加法 5 + 3 → 8;减法 5 − 3 → 2;乘法 5 × 3 → 15;实数除法 5 ÷ 2 → 2.5;整除 5 DIV 2 → 2;取模 5 MOD 2 → 1。


3. Relational (Comparison) Operators | 关系(比较)操作符

eturn a Boolean true or false. The standard set includes equal to (==), not equal to (≠ or ! perators are used extensively in condition checking, loop termination and data validation.

关系操作符比较两个值,返回布尔值 true 或 false。标准集合包括等于(==)、不等于(≠ 或 !=)、大于(>)、小于(<)、大于等于(≥ 或 >=)和小于等于(≤ 或 <=)。这些操作符广泛用于条件判断、循环终止和数据验证。

In pseudocode and many languages, equality is tested with a double equals sign to distinguish it from assignment. Comparing different data types may cause an error or implicit conversion. For example, comparing an integer to a float usually promotes the integer to float before comparison. Relational operators have lower precedence than arithmetic operators but higher than logical operators.

在伪代码和许多语言中,相等比较使用双等号,以区别于赋值。比较不同的数据类型可能导致错误或发生隐式转换。例如,整数与浮点数比较时,通常先将整数提升为浮点数。关系操作符的优先级低于算术操作符,但高于逻辑操作符。

OperatorMeaningExample (result true)
==Equal to7 == 7
!= or ≠Not equal to7 != 3
>Greater than9 > 5
<Less than2 < 6
>= or ≥Greater than or equal5 >= 5
<= or ≤Less than or equal4 <= 10

4. Boolean Logical Operators | 布尔逻辑操作符

Boolean logical operators act on Boolean values (true/false) to produce a Boolean result. The primary operators are AND, OR and NOT. AND returns true only if both operands are true; OR returns true if at least one operand is true; NOT inverts the Boolean value. In programming, logical AND is often represented as && or AND, OR as || or OR, and NOT as ! or NOT.

布尔逻辑操作符作用于布尔值(true/false),产生布尔结果。主要操作符有 AND(与)、OR(或)和 NOT(非)。AND 仅当两个操作数都为 true 时才返回 true;OR 只要至少一个操作数为 true 就返回 true;NOT 对布尔值取反。在编程中,逻辑与常表示为 && 或 AND,逻辑或表示为 || 或 OR,逻辑非表示为 ! 或 NOT。

Short-circuit evaluation is an important feature: for AND, if the left operand is false, the right operand is not evaluated. For OR, if the left operand is true, evaluation stops. This improves efficiency and can be used to guard against errors, such as checking that an index is within bounds before accessing an array element. Exam questions may ask you to trace code that relies on short-circuit behaviour.

短路求值是一个重要特性:对于 AND,如果左操作数为 false,则不再计算右操作数。对于 OR,如果左操作数为 true,则求值停止。这提高了效率,并可用于防止错误,例如在访问数组元素前检查索引是否在界内。考试题目可能会要求你追踪依赖短路行为的代码。

XOR (exclusive OR) is another logical operator that returns true only when exactly one operand is true. While not always built-in as a logical operator, it can be constructed using AND, OR and NOT: A XOR B ≡ (A AND NOT B) OR (NOT A AND B).

XOR(异或)是另一个逻辑操作符,仅当恰好一个操作数为 true 时返回 true。虽然它不一定作为内置逻辑操作符提供,但可以用 AND、OR、NOT 构造:A XOR B ≡ (A AND NOT B) OR (NOT A AND B)。

Truth table for AND: 0∧0=0, 0∧1=0, 1∧0=0, 1∧1=1
OR: 0∨0=0, 0∨1=1, 1∨0=1, 1∨1=1
NOT: ¬0=1, ¬1=0


5. Bitwise Operators | 位操作符

r values, performing operations bit by bit. The common bitwise operators are AND (&), OR (|), XOR (^), NOT (~ in low-level programming, hardware interaction, encryption and performance optimisations.

位操作符作用于整数值的二进制表示,逐位执行操作。常见的位操作符有按位与(&)、按位或(|)、按位异或(^)、按位非(~)、左移(<<)和右移(>>)。这些在底层编程、硬件交互、加密和性能优化中至关重要。

Bitwise AND compares each corresponding bit; the result bit is 1 only if both bits are 1. Bitwise OR results in 1 if at least one bit is 1. Bitwise XOR gives 1 where bits differ. Bitwise NOT flips all bits (1's complement). Left shifting by n positions is equivalent to multiplying by 2n for unsigned integers, while right shifting approximates division by 2n (floor division for unsigned, logical vs arithmetic shift for signed).

按位与比较每个对应位:仅当两个位都是 1 时结果位才为 1。按位或只要至少一个位为 1 结果位就为 1。按位异或当位不同时为 1。按位非将每个位取反(反码)。对无符号整数左移 n 位等价于乘以 2ⁿ,右移近似除以 2ⁿ(无符号整数下取整,有符号数区分逻辑右移和算术右移)。

(0011) = 1 (0001). Left shift: 5 << 1 = 10 (1010). Right shift: 12 >> 2 = 3 (1100 → 0011).

示例:5 (0101) 与 3 (0011) 按位与得 1 (0001)。左移:5 << 1 = 10 (1010)。右移:12 >> 2 = 3 (1100 → 0011)。


广告 / Sponsored

6. Assignment Operators and Compound Assignment | 赋值与复合赋值操作符

ent is a statement, not an expression, but in some it can be used inside expressions. Compound assignment operators combine an arithmetic or bitwise operation with assignment, such as +=, −=, *=, /=, MOD=, &=, |= etc.

基本赋值操作符(=)将右侧表达式的值存储到左侧变量中。在许多语言中,赋值是语句而非表达式,但在某些语言中可以用在表达式内部。复合赋值操作符将算术或位运算与赋值组合,例如 +=, −=, *=, /=, MOD=, &=, |= 等。

= 5 is equivalent to x = x + 5. Understanding the subtle difference between x = x * 2 + 1 and x *= 2 + 1 is vital: compound assignment applies the entire right-hand expression to the left variable. Thus x *= 2 + 1 expands to x = x * (2 + 1), not x = x * 2 + 1. Exam pseudocode normally evaluates the right-hand side fully before applying the operation.

复合操作符使代码更简洁,有时会稍微高效,因为操作数只被求值一次。例如,x += 5 等价于 x = x + 5。理解 x = x * 2 + 1 与 x *= 2 + 1 的细微差别至关重要:复合赋值将整个右侧表达式应用于左侧变量。因此 x *= 2 + 1 展开为 x = x * (2 + 1),而不是 x = x * 2 + 1。考试伪代码通常在应用运算前先完整求值右侧。


7. Operator Precedence and Associativity | 运算符优先级与结合性

When an expression involves multiple operators, precedence determines the order of evaluation. Higher precedence operators are applied before lower precedence ones. Arithmetic operators generally have higher precedence than relational operators, which in turn have higher precedence than logical operators. Unary operators sit near the top.

当一个表达式包含多个操作符时,优先级决定了求值顺序。优先级高的操作符先于优先级低的被应用。算术操作符通常高于关系操作符,关系操作符又高于逻辑操作符。一元操作符位于顶层附近。

Associativity decides the direction of evaluation for operators of equal precedence. Most binary operators are left-associative, meaning they are evaluated from left to right. For example, 10 − 5 − 2 is evaluated as (10 − 5) − 2. Assignment and unary operators are right-associative. In some languages exponentiation is right-associative: 232 ≡ 232.

结合性决定相同优先级操作符的求值方向。大多数二元操作符是左结合的,即从左向右求值。例如,10 − 5 − 2 按 (10 − 5) − 2 计算。赋值和一元操作符是右结合的。在某些语言中指数运算是右结合:232 ≡ 232。

Parentheses can override default precedence, forcing a subexpression to be evaluated first. Writing clear expressions with parentheses is recommended to avoid ambiguity and bugs.

括号可以覆盖默认优先级,强制子表达式先求值。建议使用括号写出清晰的表达式,避免歧义和错误。

CategoryOperatorsAssociativity
Unary+, −, NOT, ~Right
Multiplicative*, /, DIV, MODLeft
Additive+, −Left
Shift<<, >>Left
Relational<, <=, >, >=Left
Equality==, !=Left
Bitwise AND&Left
Bitwise XOR^Left
Bitwise OR|Left
Logical ANDAND, &&Left
Logical OROR, ||Left
Assignment=, +=, −= etc.Right

8. String Concatenation and Other Operators | 字符串连接与其他操作符

When the + operator is used with strings, it performs concatenation instead of arithmetic addition. This operator constructs a new string by joining two strings end to end. In many languages, if one operand is a string and the other is not, the non-string operand is first converted to its string representation.

当 + 操作符用于字符串时,它执行的是连接操作而非算术加法。该操作符通过将两个字符串首尾相接来构造一个新的字符串。在许多语言中,如果一个操作数是字符串而另一个不是,非字符串操作数会先转换为其字符串表示形式。

This dual behaviour can lead to subtle bugs if not handled carefully. For instance, 5 + 3 equals 8, but "5" + "3" yields "53". In some pseudocode languages, a dedicated concatenation operator such as & or + is used. Understanding string operations is important for handling user input and output formatting.

如果不小心处理,这种双重行为可能会导致细微的错误。例如,5 + 3 等于 8,但 "5" + "3" 得到 "53"。在某些伪代码语言中,会使用专用的连接操作符,如 & 或 +。理解字符串操作对于处理用户输入和输出格式非常重要。

de member access (e.g., dot notation), range operators, and ternary conditional (condition ? ary operator is a compact way to assign based on a condition, but must be used judiciously to preserve readability.

其他操作符可能包括成员访问(例如点号)、范围操作符和三元条件(条件 ? true_value : false_value)。三元操作符是基于条件赋值的紧凑方式,但必须适度使用以保持可读性。


9. Type Conversion and Casting in Expressions | 表达式中的类型转换与强制转换

When operators are applied to operands of different types, implicit type conversion (coercion) may occur. For example, an integer added to a float promotes the integer to float, and the result is a float. In assignment, a float stored into an integer variable may be truncated, possibly losing data.

当操作符应用于不同类型的操作数时,可能发生隐式类型转换(强制提升)。例如,整数与浮点数相加时,整数会被提升为浮点数,结果为浮点数。在赋值中,将一个浮点数存储到整数变量可能会被截断,可能导致数据丢失。

Explicit type casting allows the programmer to control conversion. In pseudocode, functions like INT(), REAL() or STRING() convert values. Casting can be essential when dividing two integers: 5/2 may yield 2.5 in many languages, but using DIV explicitly ensures integer division. Always consider promotion rules: smaller types are promoted to larger types to avoid loss of precision.

显式类型转换允许程序员控制转换。在伪代码中,像 INT()、REAL() 或 STRING() 这样的函数用于转换值。当两个整数相除时,强制转换可能很关键:5/2 在许多语言中可能得到 2.5,但使用 DIV 能确保整除。始终考虑提升规则:较小类型被提升为较大类型以避免精度损失。

In the exam, check the data type of operands before predicting an expression's result. Assigning a real to an integer typically truncates towards zero, but language-specific behaviour should be noted.

在考试中,预测表达式结果前应检查操作数的数据类型。将实数赋值给整数通常向零截断,但应注意特定语言的行为。


10. Common Pitfalls and Exam Tips | 常见错误与考试技巧

ther is mismatched operator precedence: for instance, writing x > 5 AND y < 3 may be read incorrectly without parentheses. Edexcel papers often provide precedence tables; use them to systematically evaluate expressions stepwise.

一个常见的错误是混淆赋值(=)与相等(==)。另一个是操作符优先级不匹配:例如,写 x > 5 AND y < 3 若不加括号可能被错误理解。Edexcel 试卷通常会提供优先级表;利用它来逐步系统地求值表达式。

Integer division and modulus can trip candidates: know that negative numbers follow language-specific rules. Python, for example, floors the result of integer division, while C# truncates towards zero. In pseudocode, the operation is normally defined clearly; stick to the given definition. Watch out for bitwise operations that may produce unexpected negative numbers due to two's complement representation.

整除和取模可能会难住考生:要知道负数遵循特定于语言的规则。例如,Python 的整除向负无穷取整,而 C# 向零截断。在伪代码中,通常会明确定义运算;请严格遵守给定的定义。注意位操作可能因补码表示产生意想不到的负数。

Short-circuit evaluation questions often ask: 'What will be printed?' or 'Is an error raised?' Understand that if the first operand of an OR is true, the second is not executed. This can prevent division-by-zero or array index out-of-bounds errors if structured correctly. Finally, always double-check whether an operator responds to the data type; concatenation instead of addition is a classic pitfall.

短路求值题目常问:“将输出什么?”或“是否会引发错误?”要明白,如果 OR 的第一个操作数为 true,则第二个不会执行。如果结构正确,这可以防止除零或数组索引越界错误。最后,始终仔细检查操作符对数据类型的响应;连接而非加法是一个经典的陷阱。

Published by TutorHao | Computing Revision Series | aleveler.com

广告 / Sponsored
英文原版 Collins International Primary Computing Student's Book…
计算机 备考教辅 · 正版现货 · 与本文同科同级别
查看详情 / View on Taobao

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao