Dịch chuyển nhị phân và thao tác bit
Đã giới thiệu
21:25
Kiến trúc Von Neumann
Trong phần lớn lịch sử, một thiết bị chỉ làm một việc. Một chiếc dệt vải dệt cloth. Một chiếc máy tính bỏ túi cộng. Để nó làm điều gì đó mới, bạn phải xây lại nó. Sau đó xuất hiện một cái gì đó tưởng chừng đơn giản nhưng đầy bí ẩn…
Thuyết minh tiếng Anh · Có thể chuyển đổi phụ đề tiếng Anh và tiếng Trung
Trong bài học này
| English | Ý nghĩa |
|---|---|
| bit/bɪt/ | |
| mask/mæsk/ | mặt nạ |
| logical shift/ˈlɒdʒɪkl ʃɪft/ | dịch chuyển logic |
| sign bit/saɪn bɪt/ | Bit dấu |
| arithmetic right shift/ˌærɪθˈmetɪk raɪt ʃɪft/ | dịch chuyển phải theo phép tính |
| cyclic shift/ˈsaɪklɪk ʃɪft/ | dịch chuyển tuần hoàn |
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)
Dịch chuyển và che bitmask các bit của một byte
Chọn một toán tử và xem xét từng bit kết quả. Dịch chuyển trái (<<) moves every bit up one place (×2); a right shift (>>) di chuyển chúng xuống (÷2); AND với mask sẽ xóa các bit bạn không muốn.
Giá trị 8-bit 00001011 (11) được dịch sang trái bởi 1 (LSL #1). Giá trị thập phân mới là gì?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Phép dịch trái 1 tương đương với phép nhân cho 2: $11 \times 2 = 22$ (00010110).
Dịch chuyển số không dấu sang trái bởi 3 sẽ nhân nó với số nào?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Di chuyển $n$ vị trí nhân với $2^n$, vì vậy di chuyển 3 vị trí là $2^3 = 8$.
Worked example: when × 4 stops being true
- Bits shifted off the end are lost, so the multiplication is correct only if the result fits in the available bits.
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
Dịch chuyển phải số học khác với dịch chuyển phải logic ở chỗ nó:
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Nó bảo tồn bit dấu, vì vậy chia một số có dấu âm cho lũy thừa của 2 giữ cho nó âm.
Dịch chuyển phải logic luôn đặt một 0 vào bit cao nhất, do đó nó có thể biến một số có dấu âm thành số dương.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Đó chính là lý do tại sao phép chia có dấu cần một phép dịch phải toán học, vốn sao chép bit dấu thay vì làm như vậy.
Phép dịch phải số học của 10011110 đi 3 vị trí tạo ra mẫu bit 8 ____.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Ba vị trí trống bên trái được lấp đầy bằng các bản sao của bit dấu, 1, và ba bit bên phải cùng 110 rơi ra.
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.
Một phép dịch trái vòng 1 vị trí được áp dụng cho 10000110. Kết quả là gì?
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Bit dẫn đầu 1 để lại bên trái và đi vào bên phải, nên không có bit nào bị mất. Phép dịch 00001100 sẽ là phép dịch logic.
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.
Câu nào sau đây về byte 11110000 là đúng? Chọn tất cả những câu áp dụng.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Hai kết quả chỉ khác nhau ở bit đi vào bên trái: 0 cho phép dịch logic, bit dấu cho phép dịch toán học.
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
Khớp mỗi thao tác bit với toán tử bitwise (và mask) thực hiện việc đó.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
OR SET, AND CLEAR, XOR TOGGLE, và AND + test khác 0 đọc bit — bốn thao tác masking.
Để SET một bit cụ thể lên 1, bạn kết hợp thanh ghi với một mask bằng cách sử dụng:
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
OR với một mask có bit đó = 1 ép bit đó lên 1 và giữ nguyên các bit khác.
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.
Lệnh thiết lập bit ít quan trọng nhất của ACC lên 1 và giữ nguyên các bit khác là OR ____.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
OR với một mask có 1 chỉ ở vị trí cuối cùng. Thập phân #1, nhị phân B00000001 và thập lục phân &1 là cùng một mask.
Sắp xếp các bước để kiểm tra xem bit 3 của ACC có được SET không.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Mask, so sánh, nhảy. AND để lại 00001000 hoặc 00000000, và so sánh với 0 cho thấy sự khác biệt.
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 if the result fits in the available bits. Check for overflow.
- 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
Đã xảy ra vấn đề gì? Chỉ đội ngũ trang web nhìn thấy thông tin này.