הזזות בינאריות ועיבוד ביטים
הוצג
21:25
ארכיטקטורת פון נוימן
ברוב ההיסטוריה, מכונה הייתה מבצעת משימה אחת. ארגז אריג רק בד. מחשבון חיבר. כדי להניע אותה לבצע דבר חדש, בנית אותה מחדש. אז הגיעה… פשוטה במראה אך מטעה
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| 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 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
הזזה ימינית אריתמטית שונה מהזזה ימינית לוגית כי היא:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
הוא שומר על ביט הסימן, ולכן חלוקה של מספר חתום שלילי בחזקה של 2 משאירה אותו שלילי.
הזזה ימינית לוגית תמיד מניחה 0 בביט העליון, ולכן היא יכולה להפוך מספר חתום שלילי למספר חיובי.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
זו בדיוק הסיבה שחלוקה חתומה זקוקה להזזה ימינית אריתמטית, שמעתיקה את ביט הסימן במקום זאת.
הזזה ימינית אריתמטית של 10011110 ב-3 מקומות נותנת את הדפוס ה-8-ביטי ____.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
שלושת המקומות הפנויים בצד שמאל ממולאים בעות של ביט הסימן, 1, ושלושת הביטים הימניים ביותר 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.
ביצוע סזירה שמאלית מחזורית של 1 מקומות על 10000110. מה התוצאה?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
ה-1 המוביל עוזב בצד שמאל ונכנס מחדש בצד ימין, כך שאף ביט לא אבוד. 00001100 היה ההזזה הלוגית.
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 מטבעות + בונוס קטן בהשלמה הראשונה היום
שתי התוצאות נבדלות רק בביט שנכנס מצד שמאל: 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 + בדיקה של ערך לא-אפסי קוראים ביט — ארבע פעולות המסכה.
כדי לקבוע ביט ספציפי ל-1, משלבים את הרגיסטר עם מסכה באמצעות:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
OR עם מסכה שבה הביט = 1 מחייב את הביט להיות 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 מטבעות + בונוס קטן בהשלמה הראשונה היום
OR עם מסכה שיש בה 1 רק במקום האחרון. העשרוני #1, הבין-ראשי B00000001 וההקסידאמי &1 הם אותה מסכה.
סדרו את השלבים לבדיקה האם ביט 3 ב-ACC קבוע.
רמת קושי 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 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
מה השתבש? רק צוות האתר רואה זאת.