Combined Operations in Programming: Arithmetic, Relational, Boolean and Bitwise | 编程中的组合运算:算术、关系、布尔与位运算

📚 Combined Operations in Programming: Arithmetic, Relational, Boolean and Bitwise | 编程中的组合运算:算术、关系、布尔与位运算

In A-Level programming, mastering combined operations is essential for building correct and efficient algorithms. This article covers arithmetic, relational, Boolean and bitwise operators, their precedence, and how to confidently use them together in pseudocode and Python.

在 A-Level 编程中,掌握组合运算是构建正确且高效算法的关键。本文涵盖算术、关系、布尔及位运算符,并讲解它们在伪代码与 Python 中的优先级和联合使用技巧。

1. Introduction to Operations and Expressions | 运算与表达式概述

An expression is a combination of values, variables, operators and function calls that produces a result. In Edexcel papers, you will encounter expressions that mix arithmetic with Boolean logic or bitwise masks – recognising how each part is evaluated is the first step to avoiding logic errors.

表达式是数值、变量、运算符及函数调用的组合,能产生一个结果。在 Edexcel 试卷中,你会遇到将算术与布尔逻辑或位掩码混合的表达式——理解每一部分的求值方式是避免逻辑错误的第一步。

The four families of operations – arithmetic, relational, Boolean and bitwise – each have distinct symbols and behaviours, yet they often appear inside the same line of code. A solid grasp of their individual rules makes combined expressions predictable rather than puzzling.

四类运算——算术、关系、布尔和位运算——各有不同的符号和行为,但它们常出现在同一行代码中。扎实掌握各自的规则能让组合表达式变得可预测,不再令人困惑。


2. Arithmetic Operations | 算术运算

Arithmetic operators perform mathematical calculations. In Edexcel pseudocode and Python, the core operators are + (addition), (subtraction), * (multiplication), / (floating‑point division), along with DIV (integer division) and MOD (remainder). Integer division discards the fractional part, and MOD returns the remainder after division.

算术运算符执行数学计算。在 Edexcel 伪代码和 Python 中,基本运算符包括 +(加)、(减)、*(乘)、/(浮点除),以及 DIV(整数除)和 MOD(取余)。整数除舍弃小数部分,MOD 返回除法后的余数。

For example, 17 DIV 5 gives 3 and 17 MOD 5 gives 2. When combining arithmetic with other operators, be mindful of division which can produce a float and affect subsequent operations if type mismatches occur. Use integer division or explicit conversion when an integer result is required.

例如,17 DIV 5 结果为 317 MOD 5 结果为 2。当算术运算与其他运算符组合时,要留意除法可能产生浮点数,如果发生类型不匹配会影响后续操作。需要整数结果时,应使用整除或显式转换。


3. Relational (Comparison) Operations | 关系(比较)运算

Relational operators compare two values and return a Boolean (TRUE or FALSE). The standard set includes = (equals), (not equal, often written as <> or !=), > (greater than), < (less than), (greater than or equal) and (less than or equal). Edexcel pseudocode typically uses =, <>, >, <, >=, <=.

关系运算符比较两个值并返回布尔值(TRUE 或 FALSE)。标准集合包括 =(等于)、(不等于,常写作 <>!=)、>(大于)、<(小于)、(大于等于)和 (小于等于)。Edexcel 伪代码中通常使用 =<>><>=<=

Relational operators have lower precedence than arithmetic operators, so a + b > c is evaluated as (a + b) > c. They are often used in selection and iteration conditions, but can also appear inside Boolean expressions alongside AND/OR, so understanding their position in the precedence hierarchy prevents misinterpretation.

关系运算符的优先级低于算术运算符,因此 a + b > c 会被求值为 (a + b) > c。它们常用于选择和循环条件中,但也可能与 AND/OR 一起出现在布尔表达式内,理解它们在优先层级中的位置可以避免误读。


4. Boolean (Logical) Operations | 布尔(逻辑)运算

Boolean operators act on Boolean values. The three fundamental ones 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 a single Boolean value.

布尔运算符作用于布尔值。三个基本运算符是 ANDORNOTAND 仅当两个操作数均为 TRUE 时返回 TRUE;OR 至少有一个操作数为 TRUE 时返回 TRUE;NOT 反转单个布尔值。

In combined expressions, NOT has the highest precedence among logical operators, followed by AND, then OR. For example, NOT a AND b OR c is interpreted as ((NOT a) AND b) OR c. Edexcel papers often test whether candidates can correctly add parentheses or trace such combinations.

在组合表达式中,NOT 在逻辑运算符中优先级最高,其次是 AND,最后是 OR。例如 NOT a AND b OR c 的计算顺序等价于 ((NOT a) AND b) OR c。Edexcel 试题常考查考生是否能正确添加括号或追踪这类组合。


5. Bitwise Operations | 位运算

Bitwise operators manipulate individual bits within integer representations. Edexcel recognises AND, OR, XOR, NOT (complement) and shift operations. Bitwise AND, OR and XOR compare corresponding bits of two integers; bitwise NOT flips all bits.

位运算符操作整数表示中的单个位。Edexcel 认可的位运算包括 ANDORXORNOT(取反)及移位操作。按位 AND、OR 和 XOR 比较两个整数对应的位;按位 NOT 翻转所有位。

Shift operators SHL (left shift) and SHR (right shift) move bits and fill vacated positions with zeros (for logical shifts). For example, 0011 0110₂ SHL 2 becomes 1101 1000₂. Bitwise operations are essential in low‑level programming, masking, and efficient arithmetic (multiplying or dividing by powers of two).

移位运算符 SHL(左移)和 SHR(右移)移动位并用零填充空位(指逻辑移位)。例如 0011 0110₂ SHL 2 变为 1101 1000₂。位运算在底层编程、掩码和高效算术(乘或除以 2 的幂)中不可或缺。


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

When different operators appear without parentheses, a fixed precedence order determines which operation is performed first. In Edexcel pseudocode, the typical hierarchy from highest to lowest is: parentheses; unary NOT; multiplication/division (including DIV and MOD); addition/subtraction; relational operators; logical AND; logical OR. Assignment has the lowest precedence.

当不同运算符在没有括号的情况下出现时,固定的优先级顺序决定先执行哪个运算。在 Edexcel 伪代码中,从高到低的典型层级是:圆括号;一元 NOT;乘除(含 DIV、MOD);加减;关系运算符;逻辑 AND;逻辑 OR。赋值运算符优先级最低。

Associativity governs the direction of evaluation for operators of the same precedence. Most binary arithmetic and relational operators are left‑associative (evaluated left to right), whereas assignment is right‑associative. A clear mental model of precedence and associativity is vital for tracing code and writing correct conditions.

结合性规定了相同优先级的运算符的求值方向。大多数二元算术和关系运算符是左结合的(从左向右求值),而赋值是右结合的。清晰的优先级与结合性心智模型对跟踪代码和写出正确条件至关重要。

Precedence Level (优先级) Operator Examples (运算符示例)
Highest — Parentheses (圆括号) ( )
Unary (一元) NOT, − (negation)
Multiplicative (乘除) * , / , DIV , MOD
Additive (加减) + , −
Relational (关系) = , ≠ , < , > , ≤ , ≥
Logical AND (逻辑与) AND
Logical OR (逻辑或) OR
Lowest — Assignment (赋值) ← , =

7. Combining Operations in Expressions | 在表达式中组合运算

Real‑world conditions often mix arithmetic, relational and Boolean operators. For instance, IF (score DIV 10) + bonus > threshold AND valid = TRUE combines integer division, addition, comparison and logical AND. The compiler or interpreter follows precedence: DIV and + first, then >, then AND.

实际条件经常混合算术、关系和布尔运算符。例如 IF (score DIV 10) + bonus > threshold AND valid = TRUE 混合了整数除、加法、比较和逻辑 AND。编译器或解释器按优先级处理:先执行 DIV 和 +,然后 >,最后 AND。

When bitwise operators appear with Boolean ones, the expression may behave unexpectedly if types are confused. In languages like Python, bitwise operators (&, |) can act on integers, while Boolean operators (and, or) work on truth values; Edexcel pseudocode keeps them distinct with keywords AND/OR for Boolean and AND/OR/XOR for bitwise depending on context. Always ensure operands are of the correct type.

当位运算符与布尔运算符一起出现时,如果混淆类型,表达式可能出现意外行为。在 Python 等语言中,位运算符(&|)作用于整数,而布尔运算符(andor)作用于真值;Edexcel 伪代码通过上下文区分,使用关键字 AND/OR 表示布尔运算,AND/OR/XOR 表示位运算。务必确保操作数类型正确。


8. Type Compatibility and Implicit Conversion | 类型兼容性与隐式转换

Different operator families sometimes require specific data types. Arithmetic operators expect numeric operands, relational operators can compare numbers or strings, and Boolean operators require Boolean values. Implicit type conversion (coercion) may happen, but relying on it without understanding can lead to subtle bugs.

不同运算家族有时要求特定的数据类型。算术运算符期望数值操作数,关系运算符可以比较数字或字符串,布尔运算符需要布尔值。隐式类型转换(强制转换)可能发生,但不理解就依赖它会导致微妙的错误。

In Edexcel problems, you might see result ← (age > 17) AND (grade ≥ ‘C’). The relational part returns a Boolean, which fits the AND operator perfectly. However, if a non‑Boolean value is accidentally used as a condition, the behaviour becomes language‑dependent; pseudocode often treats zero as FALSE and non‑zero as TRUE, but exam questions usually keep types explicit.

在 Edexcel 题目中,你可能会见到 result ← (age > 17) AND (grade ≥ ‘C’)。关系部分返回布尔值,恰好适合 AND 运算符。但如果非布尔值被误用作条件,行为将依赖于语言;伪代码中常将零视为 FALSE、非零视为 TRUE,但考试题通常保持类型明确。


9. Common Errors and Pitfalls | 常见错误与陷阱

A frequent mistake is omitting parentheses when combining AND and OR in a single condition. Because AND binds tighter than OR, a OR b AND c means a OR (b AND c), which is often not what the programmer intended. Use parentheses to make the logic explicit.

一个常见错误是在同一条件中组合 AND 和 OR 时漏掉括号。由于 AND 比 OR 结合得更紧,a OR b AND c 等价于 a OR (b AND c),通常并非程序员本意。应使用括号明确逻辑。

Another pitfall is confusing integer division with floating‑point division. 5 / 2 in Python yields 2.5, while 5 // 2 yields 2. In pseudocode, 5 DIV 2 gives 2. When the result is later used in a relational expression, an unintended float may cause a type mismatch or rounding issues.

另一个陷阱是混淆整除与浮点除。Python 中 5 / 2 得到 2.5,而 5 // 2 得到 2。伪代码中 5 DIV 2 结果为 2。当结果后续用于关系表达式时,意外的浮点数可能导致类型不匹配或舍入问题。

Bitwise versus Boolean confusion is also common. Using a single & in a conditional test may perform bitwise AND instead of logical, producing a non‑Boolean result that the program still treats as true/false, leading to hard‑to‑spot logical errors.

位运算与布尔运算混淆也很常见。在条件测试中使用单个 & 可能会执行按位 AND 而非逻辑,产生非布尔结果,而程序仍将其视为真/假,导致难以发现逻辑错误。


10. Practical Examples in Pseudocode and Python | 伪代码与 Python 实例

Example 1 (Combined arithmetic and relational):
IF (a + 3) * 2 > b THEN — the addition and multiplication happen first, then the comparison. Always trace step‑by‑step.

示例 1(算术与关系结合):
IF (a + 3) * 2 > b THEN — 加法和乘法先执行,然后才比较。务必逐步追踪。

Example 2 (Boolean operators with relations):
valid ← (temperature > 0) AND (temperature < 100). Both sub‑expressions are relational, returning Booleans that can be combined with AND.

示例 2(关系与布尔结合):
valid ← (temperature > 0) AND (temperature < 100)。两个子表达式都是关系运算,返回布尔值,可与 AND 组合。

Example 3 (Bitmask in pseudocode):
masked ← value AND 0x0F — the bitwise AND clears the high nibble. Shifts can then extract bits: highNibble ← (value SHR 4) AND 0x0F.

示例 3(伪代码中位掩码):
masked ← value AND 0x0F — 按位 AND 清除了高半字节。移位则可提取位:highNibble ← (value SHR 4) AND 0x0F


11. Examination Tips for Edexcel Paper 2 | Edexcel 试卷二的应试技巧

When tracing or writing code, draw a precedence table if you are unsure. Annotate the expression with parentheses mentally or on the question paper. Examiners award marks for correct evaluation order even if the final answer is wrong, so show your working clearly.

在追踪或编写代码时,如果不确定,可以画出优先级表。在脑海中或在试卷上给表达式加上括号注解。即使最终答案错误,正确的求值顺序也能得分,因此清晰展示你的演算过程。

Be explicit with types. In pseudocode, declare variables and note whether a function returns an integer or a Boolean. For combined bitwise/arithmetic questions, convert numbers to binary if necessary, and align bits carefully to avoid off‑by‑one shift errors.

注意类型明确。在伪代码中声明变量,并标注函数返回整数还是布尔值。对于组合位运算与算术的题目,必要时将数字转换为二进制,并仔细对齐位,以避免移位差一的错误。

Practise past paper questions that involve complex IF conditions or WHILE loops with combined operators. Recognise typical traps: missing parentheses around relational expressions before AND/OR, or using assignment (=) instead of equality (= or ==) inside conditions.

练习涉及复杂 IF 条件或带有组合运算符的 WHILE 循环的历年真题。识别典型陷阱:在 AND/OR 前缺少关系表达式周围的括号,或在条件中误用赋值(=)代替相等比较(= 或 ==)。


12. Summary | 总结

Combined operations are simply a matter of knowing each operator’s role, respecting precedence rules, and writing parentheses whenever the default order is not what you intend. Mastering arithmetic, relational, Boolean and bitwise operators individually ensures that when they appear together, your code remains predictable and your exam answers accurate.

组合运算归根到底就是了解每个运算符的作用、遵守优先级规则,并在默认顺序不符合预期时添加括号。分别掌握算术、关系、布尔和位运算符,就能确保它们在结合出现时,代码依然可预测,考试答案准确无误。

Published by TutorHao | Programming Revision Series | aleveler.com

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

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version