פעולה על סיביות – הבדלי גרסאות

תוכן שנמחק תוכן שנוסף
הגהה
תגיות: עריכה ממכשיר נייד עריכה דרך האתר הנייד
הוספת פרק על הזזה אריתמטית. השאר בהערה (כותרות ותיאורי תמונה מתורגמים)
תגית: גרשיים שגויים
שורה 77:
</div>
כאשר <math>b</math> הוא מספר הביטים ב־ <math>x = \lfloor\log_2 x\rfloor+1</math> עבור כל <math>x \neq 0</math>.
 
== הזזת סיביות ==
 
'''הזזת סיביות''' לעיתים נחשבת כפעולה על סיביות, מכיוון שהיא מטפלת בערך כסדרה של סיביות ולא ככמות מספרית. בפעולה זו הספרות זזות שמאלה או ימינה. ל[[אוגר (מחשבים)|אוגרים]] במעבד המחשב יש גודל קבוע, ולכן סיביות "יזוזו החוצה" מהאוגר בצד אחד, בזמן שסיביות אחרות "יזוזו פנימה" מהצד השני. ההבדל בין סוגים שונים של הזזת סיביות טמון בערכים של הסיביות הנכנסות פנימה.
 
===הזזה אריתמטית===
[[Image:Rotate left logically.svg|thumb|150px|הזזה אריתמטית לשמאל]]
[[Image:Rotate right arithmetically.svg|thumb|150px|הזזה אריתמטית לימין]]
ב''הזזה אריתמטית'', הסיביות שמוזזות החוצה לכל אחד מהצדדים אובדות. בהזזה אריתמטית לשמאל, אפסים זזים פנימה מצד ימין; ובהזזמה אריתמטית לימין, [[סיבית הסימן]] (הסיבית המשמעותית ביותר, MSB) זזה פנימה משמאל, וכך שומרת על הסימן של רצף המספר. המשפט האחרון אינו תקף לטיוטה האחרונה של תקן [[שפת C]]. אם בוצעה הזזה על ערך שלילי, התוצאה היא "תלוית מימוש", מה שאומר שאינה עקבית במעבר בין פלטפורמות.<ref>{{cite book|last1=Garcia|first1=Blandine|title=INTERNATIONAL STANDARD ISO/IEC 9899:201x|date=2011|publisher=ISO/IEC|page=95|edition=201x|url=http://chimera.roma1.infn.it/SP/COMMON/iso-iec-9899-1990.pdf|accessdate=7 September 2015}}</ref>
 
זו דוגמה להסטת מספר בן שמונה סיביות:
 
00010111 (שערכו +23 בבסיס דצימלי) '''הזזה-לשמאל'''
= 00101110 (שערכו +46 בבסיס דצימלי)
 
10010111 (שערכו -105 בבסיס דצימלי) '''הזזה-לימין'''
= 11001011 (שערכו -53 בבסיס דצימלי)
 
במקרה הראשון, הספרה השמאלית ביותר הוזזה מחוץ לאוגר, ו־0 חדש הוזז לתוך המיקום הימני ביותר. במקרה השני, הערך הימני ביותר 1 הוזז החוצה (ייתכן שלתוך דגל הנשא) ו־1 הועתק למיקום השמאלי ביותר על מנת לשמר את הסימן (אבל לא בהכרח, לפי הטיוטה האחרונה של שפת C. ראו הערה למעלה). הזזות מרובות יכולות להתבצע במכה. לדוגמה:
 
00010111 (שערכו +23 בבסיס דצימלי) '''הזזה-לשמאלה-שתי-פעמים'''
= 01011100 (שערכו +92 בבסיס דצימלי)
 
הזזה אריתמטית לשמאל ''n'' פעמים זהה להכפלה ב־2<sup>''n''</sup> (בהנתן שלא הייתה [[גלישה נומרית]]), בזמן שהזזה אריתמטית לימין ''n'' פעמים בשיטת ה[[משלים ל-2|משלים ל־2]] זהה לחלוקה ב־2<sup>''n''</sup> ועיגול לעבר אינסוף שלילי. אם המספר הבינארי מטופל בצורת [[משלים ל-1|משלים ל־1]], אז תוצאת הפעולת של הזזה ימינה היא חלוקה ב־2<sup>''n''</sup> ועיגול כלפי 0.
 
<!--
===הזזה לוגית===
{| style="float:left;" border="0" cellpadding="0" cellspacing="0"
|-
| [[Image:Rotate left logically.svg|thumb|150px|הזזה לוגית לשמאל]]
| [[Image:Rotate right logically.svg|thumb|150px|הזזה לוגית לימין]]
|}
In a ''logical shift'', zeros are shifted in to replace the discarded bits. Therefore the logical and arithmetic left-shifts are exactly the same.
 
However, as the logical right-shift inserts value 0 bits into the most significant bit, instead of copying the sign bit, it is ideal for unsigned binary numbers, while the arithmetic right-shift is ideal for signed [[two's complement]] binary numbers.
{{Clear}}
 
===הזזה בלי סחיבה===
{| style="float:left;" border="0" cellpadding="0" cellspacing="0"
|-
| [[Image:Rotate left.svg|thumb|150px|הזזה לוגית מעגלית שמאלה]]
| [[Image:Rotate right.svg|thumb|150px|הזזה לוגית מעגלית ימינה]]
|}
Another form of shift is the ''circular shift'' or ''bit rotation''. In this operation, the bits are "rotated" as if the left and right ends of the register were joined. The value that is shifted in on the right during a left-shift is whatever value was shifted out on the left, and vice versa. This operation is useful if it is necessary to retain all the existing bits, and is frequently used in digital [[cryptography]].
{{Clear}}
 
===הזזה לוגית עם סחיבה===
{| style="float:left;" border="0" cellpadding="0" cellspacing="0"
|-
|[[Image:Rotate left through carry.svg|thumb|150px|הזזה לוגית עם סחיבה שמאלה]]
|[[Image:Rotate right through carry.svg|thumb|150px|הזזה לוגית עם סחיבה ימינה]]
|}
''Rotate through carry'' is similar to the ''rotate no carry'' operation, but the two ends of the register are separated by the [[carry flag]]. The bit that is shifted in (on either end) is the old value of the carry flag, and the bit that is shifted out (on the other end) becomes the new value of the carry flag.
 
A single ''rotate through carry'' can simulate a logical or arithmetic shift of one position by setting up the carry flag beforehand. For example, if the carry flag contains 0, then <code>x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE</code> is a logical right-shift, and if the carry flag contains a copy of the sign bit, then <code>x RIGHT-ROTATE-THROUGH-CARRY-BY-ONE</code> is an arithmetic right-shift. For this reason, some microcontrollers such as low end [[PIC microcontroller|PIC]]s just have ''rotate'' and ''rotate through carry'', and don't bother with arithmetic or logical shift instructions.
 
Rotate through carry is especially useful when performing shifts on numbers larger than the processor's native [[word size]], because if a large number is stored in two registers, the bit that is shifted off the end of the first register must come in at the other end of the second. With rotate-through-carry, that bit is "saved" in the carry flag during the first shift, ready to shift in during the second shift without any extra preparation.
-->
 
== ראו גם ==