Binary shifts and bit manipulation
| English | Chinese | Pinyin |
|---|---|---|
| logical shift | 逻辑移位 | luó jí yí wèi |
| arithmetic right shift | 算术右移 | suàn shù yòu yí |
| sign bit | 符号位 | fú hào wèi |
| mask | 掩码 | yǎn mǎ |
Shifting bits around
- Moving all the bits left or right is a shift — and it's a fast way to multiply or divide.
- Setting individual bits lets one byte hold up to 8 on/off flags.
- These tricks are everywhere in low-level and embedded code.
Logical shifts 逻辑移位
- A logical shift moves all bits left or right, filling the new positions with 0.
- Left shift by 1 (
LSL #1) — for an unsigned number this is × 2. - Right shift by 1 (
LSR #1) — this is integer ÷ 2. - Shifting by $n$ places multiplies or divides by $2^n$. Example:
00001011(11)LSL #1→00010110(22).

A left shift fills with zeros and doubles the value; a right shift halves it
Shift and mask the bits of a byte
Pick an operator and watch each result bit. A left shift (<<) moves every bit up one place (×2); a right shift (>>) moves them down (÷2); AND with a mask clears the bits you don't want.
The 8-bit value 00001011 (11) is shifted left by 1 (LSL #1). What is the new denary value?
A left shift by 1 multiplies by 2: $11 \times 2 = 22$ (00010110).
Shifting an unsigned number left by 3 places multiplies it by what number?
Shifting by $n$ places multiplies by $2^n$, so by 3 places is $2^3 = 8$.
Arithmetic right shift 算术右移
- A plain logical right shift puts a 0 in the top bit — which would make a negative signed number look positive.
- An arithmetic right shift keeps (copies) the sign bit 符号位, so a negative number stays negative.
- Use it to divide a signed number by powers of 2.
An arithmetic right shift differs from a logical right shift because it:
It preserves the sign bit, so dividing a negative signed number by a power of 2 keeps it negative.
A logical right shift always puts a 0 in the top bit, so it can turn a negative signed number positive.
That is exactly why signed division needs an arithmetic right shift, which copies the sign bit instead.
Bit manipulation with masks 掩码
- Embedded devices often use one bit of a register per signal. Using a mask:
- Set bit $n$:
R = R OR mask(mask has bit $n$ = 1). - Clear bit $n$:
R = R AND mask(mask has bit $n$ = 0, the rest 1). - Toggle bit $n$:
R = R XOR mask(mask has bit $n$ = 1). - Test bit $n$:
R AND mask, then check if the result is non-zero.

A mask with AND, OR or XOR sets, clears, toggles or tests a single bit
- Shifts can be logical, arithmetic or cyclic (rotate); bit masking uses AND/OR/XOR with a mask to set, clear, toggle or test bits; the ACC and IX are the registers involved.
Match each bit operation to the bitwise operator (and mask) that does it.
OR sets, AND clears, XOR toggles, and AND + a non-zero test reads a bit — the four masking moves.
To SET a particular bit to 1, you combine the register with a mask using:
OR with a mask that has that bit = 1 forces the bit to 1 and leaves the others unchanged.
You've got it
- logical shift fills with 0: left = ×2, right = ÷2; by $n$ places = $\times$ or $\div\ 2^n$
- arithmetic right shift keeps the sign bit (for negative signed numbers)
- masks: OR to set, AND to clear, XOR to toggle, AND + test to read a bit