4.3 Bit manipulation 位操作
Review the exact wording students need for this knowledge point, then open a filtered question list when ready to practise.
Find questions for 9618-AS-04-03Syllabus learning goals 考纲学习目标
完成本节后,学生需要能够:
- 执行 logical、arithmetic 和 cyclic left/right binary shifts。
- 解释 shift 中被丢弃、补入或循环回来的 bits。
- 使用 bit manipulation monitoring/control device。
- 使用 bit mask test、set、clear 或 toggle 某一 bit。
- 执行 bitwise AND、OR、XOR、LSL 和 LSR instructions。
Bit manipulation 必须逐位运算。Bitwise AND/OR/XOR 不是对整个 binary number 做一次 true/false 判断,而是每一 column 独立计算。
1 Logical shifts
Logical shift left (LSL)
- All bits move left by the stated number of places。
- Bits leaving the Most Significant Bit (MSB) end are discarded。
- Zeros enter at the right-hand/Least Significant Bit end。
Example:
11001010 LSL 2
11001010 -> 10010100 -> 00101000
Result: 00101000
在没有 overflow 且按 unsigned value 解释时,每 left shift one place 相当于乘 2。固定 8-bit register 中被移出的 bits 丢失,因此结果可能 overflow,不能继续代表完整乘法结果。
Logical shift right (LSR)
- All bits move right。
- Bits leaving the LSB end are discarded。
- Zeros enter at the left/MSB end。
00010111 LSR 3
00010111 -> 00001011 -> 00000101 -> 00000010
Result: 00000010
对 unsigned integer,每 right shift one place 相当于除 2 并丢弃 remainder。
2 Arithmetic shifts
Arithmetic shifts 用于 signed two's complement values,重点是保留 sign。
Arithmetic shift right
- Bits move right。
- LSB-end bits are discarded。
- Original sign bit is copied into the new MSB positions。
Example:negative 8-bit two's complement value:
10010011 arithmetic shift right 3
10010011 -> 11001001 -> 11100100 -> 11110010
Result: 11110010
因为原 sign bit 是 1,左侧补 1,结果仍保持 negative。Logical right shift 会补 0,从而破坏 negative sign。
Arithmetic shift left
Bits 向左移动,右侧补 0。在 fixed-width two's complement 中 sign may change if significant bits are lost,因此必须检查 overflow。
Mark-secure comparison
A right logical shift inserts zeros into the leftmost positions. A right arithmetic shift copies the sign bit into the MSB, preserving the sign of a two's complement value.
3 Cyclic shifts
Cyclic/rotate shift 不直接丢弃移出的 bit,而是把它放回另一端。
Cyclic left
10010110 cyclic left 1
-> 00101101
原 MSB 1 移到新的 LSB。
Cyclic right
10010110 cyclic right 1
-> 01001011
原 LSB 0 移到新的 MSB。
| Shift | New empty positions | Bits shifted out |
|---|---|---|
| Logical | Filled with 0 | Discarded |
| Arithmetic right | Filled with original sign bit | Discarded |
| Cyclic | Filled with bits from opposite end | Wrapped around |
4 Bitwise AND, OR and XOR
| A | B | A AND B | A OR B | A XOR B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Example:
11110001
XOR 00010010
------------
11100011
逐 column 比较:相同得 0,不同得 1。
Operand formats
#ndenotes a denary number,例如#63。Bndenotes binary,例如B00111111。&ndenotes hexadecimal,例如&3F。<address>means use the contents of that memory address。
#63、B00111111、&3F 都表示相同 8-bit mask。计算前先把 operand 转成与 ACC 相同位数的 binary。
5 Bit masks
Bit mask 是刻意设计的 binary pattern,用来只影响或检查特定 bits。
假设从右到左编号 bit 0 到 bit 7,要操作 bit 3,mask 为:
B00001000
Test a bit using AND
ACC AND B00001000
- Target bit 为
1→ result 是00001000,non-zero。 - Target bit 为
0→ result 是00000000。 - Other bits 因 mask 为
0而被清除,不影响 test result。
标准步骤:AND with mask,然后 compare result with zero。
Set a bit using OR
ACC OR B00001000
Target mask bit 为 1,因此 result 的 bit 3 一定为 1;其他 mask bits 为 0,原值保持。
Clear a bit using AND
要 clear bit 3,mask 在目标位写 0,其他位写 1:
ACC AND B11110111
Target bit 与 0 AND 后变 0;其他 bits 与 1 AND 后保持原值。
清空整个 8-bit register:
AND B00000000
所有 bits 与 0 AND,结果全为 0。可再 compare with zero 验证 register 已清空。
Toggle a bit using XOR
ACC XOR B00001000
Target bit 与 1 XOR,因此 0→1、1→0;其他 bits 与 0 XOR 后保持。
Mask summary
| Goal | Operation | Target mask bit | Other mask bits |
|---|---|---|---|
| Test | AND | 1 | 0 |
| Set | OR | 1 | 0 |
| Clear | AND | 0 | 1 |
| Toggle | XOR | 1 | 0 |
6 Monitoring and controlling devices
Hardware interface register 常用每一 bit 表示一个 independent sensor/input 或 actuator/output 状态。
Monitoring an input register
Example input byte:
Bit 0 = door sensor
Bit 1 = motion sensor
Bit 2 = over-temperature flag
Bit 3 = emergency button
要 test over-temperature flag,只需:
AND B00000100
CMP #0
Result non-zero 表示 bit 2 was set。Masking avoids changing/considering unrelated sensor bits。
Controlling an output register
Example output byte:
Bit 0 = warning LED
Bit 1 = motor
Bit 2 = alarm
- Turn alarm on:OR with
B00000100。 - Turn motor off:AND with
B11111101。 - Toggle warning LED:XOR with
B00000001。
程序必须把 modified byte 写回对应 output register/address,硬件才会看到新的 control bits。
Why bit manipulation is useful
- Many independent states fit inside one register。
- One device bit can be changed without altering the others。
- Sensor status can be tested efficiently using one mask/instruction。
- Memory and communication bandwidth are used efficiently。
7 Worked bitwise examples
AND with denary operand
ACC = 11100011
AND #63
63 = B00111111
11100011
AND 00111111
------------
00100011
XOR with hexadecimal operand
ACC = 11110001
XOR &12
&12 = B00010010
11110001
XOR 00010010
------------
11100011
OR mask
11100011
OR 00010000
------------
11110011
8 Writing instructions
根据题目 instruction table 选择 exact mnemonic:
- Shift left two places:
LSL #2。 - Shift right three places:
LSR #3。 - Test bits:
AND B...followed by appropriate compare/jump if required。 - Set bits:
OR B...。 - Toggle bits:
XOR B...。
若题目给 <address> form,例如 AND 101,它表示与 memory address 101 的 contents 运算;AND #101 才表示 denary immediate value 101。
9 Common mistakes 常见失分点
- Logical right shift 左侧补
1;正确是补0。 - Arithmetic right shift 对 negative value 补
0;正确是复制 sign bit1。 - Shift 后保留已移出 fixed-width register 的 bits。
- Cyclic shift 丢弃 bit,而不是 wrap 到另一端。
- Bitwise operation 没有把 operands 对齐成相同位数。
- 把 hexadecimal operand 当 binary 字符直接计算。
- Test mask 中目标位写
0;AND test 的目标位必须写1。 - Clear 一个 bit 时使用 OR;clear 应使用目标位为
0的 AND mask。 CMP被误认为改变 ACC;compare 通常只改变 condition/status。- 修改 output byte 后没有 store/write back 到 device register。
10 Exam-answer checklist 真题检查表
- 是否确认 register width 并保持相同位数?
- Logical shift 的空位是否补
0? - Arithmetic right 的空位是否复制 sign bit?
- Cyclic shift 的移出 bit 是否回到另一端?
- Decimal/hex operand 是否先正确转换为 binary?
- Bitwise AND/OR/XOR 是否逐 column 运算?
- Test/set/clear/toggle 是否选择正确 operation 和 mask?
- Test bit 后是否与 zero 比较?
- Device control 是否只改变目标 bit 并保持其他 bits?
- Instruction 中
#、B、&或 address 是否写对?
本材料依据 9618 syllabus 4.3 的三项要求,并综合本页所列 2021-2025 past-paper questions 及 mark schemes。示例重点覆盖 shifts、operand conversion、bit masking 和 device status/control。
Practice This Knowledge Point
Open the question bank with this knowledge code already filled in. The filtered list will show matching questions for this note.
Open filtered practice