| English | 意味 |
|---|---|
| bit/bɪt/ | ビット |
| mask/mæsk/ | マスク |
| logical shift/ˈlɒdʒɪkl ʃɪft/ | 論理シフト |
| sign bit/saɪn bɪt/ | 符号ビット |
| arithmetic right shift/ˌærɪθˈmetɪk raɪt ʃɪft/ | 算術右シフト |
| cyclic shift/ˈsaɪklɪk ʃɪft/ | 巡回シフト |
Multiplying on a chip that cannot multiply
- The processor in the 1989 Game Boy had no multiply instruction at all. Every score, every coordinate that needed doubling was doubled by sliding its bits one place to the left.
- A shift takes one clock tick. Multiplication built from shifts and additions is how those games ran on a few kilobytes and a handful of milliwatts.
- The same tricks control single wires in an embedded device: one bit of a register per sensor or actuator, tested and set with a mask 掩码.
- This lesson is the three kinds of shift and the four mask operations, in the exam's own instructions.
Logical shifts
- A logical shift 逻辑移位 moves every bit left or right by some places and fills the vacated positions with 0.
LSL #1moves the bits left and a 0 enters on the right: for an unsigned number that is × 2.LSR #1moves them right and a 0 enters on the left: integer ÷ 2.- Shifting by $n$ places multiplies or divides by $2^{n}$.
00001011(11) afterLSL #1is00010110(22); afterLSR #1it is00000101(5, the remainder lost).

Logical left (× 2), logical right (÷ 2) and arithmetic right (keeps the sign bit)
バイトのビットをシフトしてマスク適用する
演算子を選び、各結果ビットを見る。左シフト (<<) moves every bit up one place (×2); a right shift (>>) は値を下へ移動させ(÷2)、マスクとのANDで不要なビットをクリアする。
8ビット値 00001011 (11) を1つ左にシフトする (LSL #1)。新しい十進数値は何か?
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
1つ左シフトすると2倍になる: $11 \times 2 = 22$ (00010110)。
符号なし数値を3桁左にシフトすると、元の数値を何倍するか?
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
$n$ 桁シフトすると $2^n$ 倍になるので、3桁シフトすると $2^3 = 8$ 倍になる。
Worked example: when × 4 stops being true
- Bits shifted off the end are lost, so the multiplication is only correct while they were zeros.
LSL #2on the two's complement byte11001010gives00101000. The two 1s that fell off the left are gone, the sign bit has changed, and the result is no longer four times the original.LSL #2on00001011(11) gives00101100(44), which is correct, because only zeros were lost.- The exam asks for both: the shifted pattern, and a comment on whether the value is still right.
Arithmetic right shift
- A plain logical right shift puts a 0 in the top bit, which would turn a negative two's complement number positive.
- An arithmetic right shift 算术右移 copies the sign bit 符号位 into each vacated place, so a negative number stays negative and the shift still divides by 2.
10011110shifted arithmetically right by 3 places is11110011;01011100gives00001011.

Logical and arithmetic right shift on the same byte: only the entering bit differs
算術右シフト論理右シフトと異なるのはなぜか:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
符号ビットを保持するため、負の符号付き数値を2の累乗で割っても負のままになる。
論理右シフトは常に上位ビットに0を入れるため、負の符号付き数値を正に変えることができる。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
これがまさに符号付き除算が算術右シフトを必要とする理由であり、符号ビットをコピーするからである。
10011110 を3桁右シフトした結果の8ビットパターン ____ 。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
左側の空いた3箇所は符号ビットのコピーである1で埋められ、右端の3ビット 110 は外れる。
Cyclic shifts
- A cyclic shift 循环移位, or rotate, feeds the bit that drops off one end back in at the other end, so no bits are lost.
- A cyclic left shift of 1 on
10000110gives00001101: the leading 1 reappears on the right. - Logical shifts fill with zeros, arithmetic shifts fill with the sign bit, cyclic shifts fill with the bit that left. That is the whole difference between the three.
10000110 に1桁左回転シフトを適用した。結果は何か?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
先頭の1が左から離れて右から再入力されるため、ビットは失われない。00001100 は論理シフト would be となる(※原文の文脈から「論理シフトならこうなる」という意味)。
Worked example: 240 or minus 16?
- Take
11110000. Read as unsigned it is 240; read as two's complement it is −16. LSR #1brings in a 0 and gives01111000= 120, the correct half of 240.ASR #1copies the sign bit and gives11111000= −8, the correct half of −16.- Neither is wrong. Each halves the value under one reading, which is why a processor needs both instructions.
バイト 11110000 に関する記述として正しいものはどれか?すべて選択せよ。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
2つの結果は左側に入力されるビットだけで異なる:論理シフトは0、算術シフトは符号ビット。
Bit masking
- Embedded devices often use one bit 位 of a register per signal. A mask is a pattern combined with the register so that only the chosen bit changes.
- Set bit $n$:
ORwith a mask that has a 1 in position $n$. Clear bit $n$:ANDwith a mask that has a 0 there and 1s everywhere else. - Toggle bit $n$:
XORwith a mask that has a 1 there. Test bit $n$:ANDwith that mask, thenCMP #0: not equal means the bit was set.

Set with OR, clear with AND, toggle with XOR, each using a mask
各ビット操作に対応するビット演算子(およびマスク)を対応させよ。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
ORはセットし、ANDはクリアし、XORは反転させ、AND+非ゼロ検出はビットを読み取る——これら4つがマスク操作の基本だ。
特定のビットを1にセットするには、レジスタと以下のどの組み合わせを用いるか:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
そのビットが1のマスクとのORは、そのビットを1に強制し、他のビットは変更しない。
Worked example: the instructions on one byte
- The ACC holds
10101100. The mask may be written#ndenary,Bnbinary or&nhexadecimal. AND B00001111gives00001100: only the low four bits survive.OR #1gives10101101: the least significant bit is set and nothing else moves.XOR &FFgives01010011: every bit inverted.AND B00001000thenCMP #0tests bit 3; the result00001000is not zero, so bit 3 was set.LSL #2gives10110000, losing the top two bits;LSR #3gives00010101.
ACCの最下位ビットを1にし、他のビットを変更しない命令は OR ____ である。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
最後の一桁のみが1のマスクとのOR。十進数 #1、二進数 B00000001、十六進数 &1 は同じマスクである。
ACCのビット3がセットされているかをテストする手順を並べ替えよ。
難易度 3/5 · 200 コイン + 今日初回完了時に小ボーナス付与
マスク、比較、ジャンプ。ANDの結果は 00001000 または 00000000 のどちらかになり、ゼロとの比較で区別する。
Monitoring and control, one bit at a time
- In a monitoring device one bit of a register per sensor means a single
ANDchecks whether a particular sensor is on. - In a control device one
ORswitches an actuator's control bit on without disturbing the others, and oneANDswitches it off. - It is fast, it uses almost no memory, and one byte holds eight independent on/off states. That is the "why" the exam asks for.
Marks that slip away
- A left shift is × 2 per place only while the bits that fall off are zeros. Say so when a 1 is lost.
- A logical right shift brings in 0; an arithmetic right shift copies the sign bit. Choose by whether the number is signed.
- To clear a bit the mask needs a 0 at that bit and 1s everywhere else. A mask of all zeros clears the whole register.
XORtoggles; it does not set. To set useOR, to test useANDand compare with zero.
You've got it
- logical shift fills with 0: left × 2 per place, right ÷ 2, and lost 1s break the arithmetic
- arithmetic right shift copies the sign bit; a cyclic shift wraps the bit round
- masks: OR sets · AND clears · XOR toggles · AND then CMP #0 tests
- one bit per sensor or actuator makes monitoring and control fast and tiny; masks are written
#n,Bnor&n