Chapterبەش 3 · Lectureوانە 3.5

Adding and Subtracting Signed Numbersکۆکردنەوە و لێدەرکردنی ژمارە نیشانەدارەکان

How a computer adds and subtracts positive and negative numbers with one ordinary adder: the four cases of 2's-complement addition, why the final carry is discarded, subtraction as adding the 2's complement, and how to recognise overflow.

چۆن و بە یەک ئاسایی کۆ دەکاتەوە و لێک دەردەکات: چوار حاڵەتی کۆکردنەوە بە ، بۆچی دوایین فڕێ دەدرێت، لێدەرکردن وەک زیادکردنی و چۆن دەناسیتەوە.

14 min read15 خولەک خوێندنەوە · 17 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Add two signed 8-bit numbers in 2's complement and read the answer.
  • Explain why the final carry can be discarded.
  • Subtract by adding the 2's complement of the subtrahend.
  • Decide whether an addition has overflowed, with the sign rule and the carry rule.
  • دوو 8 بیتی بە کۆ بکەیتەوە و وەڵامەکە بخوێنیتەوە.
  • ڕوون بکەیتەوە بۆچی دوایین فڕێ دەدرێت.
  • بە زیادکردنی لێدەرکراوەکە لێدەرکردن بکەیت.
  • بە یاسای نیشانە و یاسای بڕیار بدەیت ئایا کۆکردنەوەیەک داوە.

One adder for every signیەک کۆکەرەوە بۆ هەموو نیشانەیەک

In Signed numbers you learned to store negative numbers in 2's complement. Now comes the reward: to add two signed numbers, the computer simply adds the bit patterns in ordinary binary addition — sign bits included — with no special rules for the signs.

لە ژمارە نیشانەدارەکان فێر بوویت بە هەڵبگریت. ئێستا سوودەکەی دەردەکەوێت: بۆ کۆکردنەوەی دوو ، تەنها پێکهاتەی بە کۆکردنەوەی دووانیی ئاسایی کۆ دەکاتەوە — — بەبێ هیچ یاسایەکی تایبەت بۆ نیشانەکان.

Adding signed numbers in 2's complementکۆکردنەوەی ژمارە نیشانەدارەکان بە تەواوکەری دوو
  1. Write both numbers in 2's complement with the same number of bits (8 bits in this lesson). Negative numbers: invert every bit of the positive number, then add 1.

    هەردوو ژمارەکە بە و بە هەمان ژمارەی بنووسە (لەم وانەیەدا 8 ). : هەموو پێچەوانە بکەرەوە، پاشان 1 زیاد بکە.

  2. Add them column by column from the right, like unsigned binary numbers.

    ستوون بە ستوون لە ڕاستەوە کۆیان بکەرەوە، وەک ژمارەی بێنیشانە.

  3. If a 9th bit (a final carry) appears on the left, discard it. The 8 bits that remain are the answer.

    ئەگەر نۆیەم (دوایین ) لە لای چەپ دەرکەوت، فڕێی بدە. ئەو هەشت دەمێننەوە وەڵامەکەن.

  4. Read the answer: sign bit 0 → positive; sign bit 1 → negative (use the −128-128 weight).

    وەڵامەکە بخوێنەرەوە: 0 ← موجەب؛ 1 ← سالب ( بە −128-128 بژمێرە).

Quick checkپشکنینی خێراWhen you add two 8-bit 2's complement numbers and a 9th bit appears, what do you do with it?کاتێک دوو ژمارەی 8 بیتی بە کۆ دەکەیتەوە و نۆیەم دەردەکەوێت، چی لێ دەکەیت؟

The four casesچوار حاڵەتەکە

When two signed numbers are added, there are four cases. The method is the same for all of them; only the result looks different.

کاتێک دوو کۆ دەکرێنەوە، چوار حاڵەت هەیە. ڕێگاکە بۆ هەموویان یەکە؛ تەنها ئەنجامەکە جیاواز دەردەکەوێت.

  1. Both numbers positive.
  2. A positive and a negative number, the positive one larger in magnitude.
  3. A positive and a negative number, the negative one larger in magnitude.
  4. Both numbers negative.
  1. هەردوو ژمارەکە موجەبن.
  2. و ، موجەبەکە گەورەترە.
  3. و ، سالبەکە گەورەترە.
  4. هەردوو ژمارەکە سالبن.

Remember the range: 8 bits hold −128-128 to +127+127. Every number and every answer in this lesson must fit in that range; the overflow screen shows what happens when it does not.

سنوورەکان لەبیر بێت: 8 تەنها −128-128 تا +127+127 هەڵدەگرن. هەموو ژمارە و وەڵامێکی ئەم وانەیە دەبێت لە نێوان ئەم دوو سنوورەدا بێت؛ شاشەی سەرڕێژبوون پیشان دەدات چی ڕوو دەدات کاتێک جێی نابێتەوە.

The four casesچوار حاڵەتەکە

Case 1: both numbers positiveحاڵەتی 1: هەردوو ژمارەکە موجەبن

Two positive numbers are added exactly like unsigned numbers. Example: 7+47 + 4.

دوو ڕێک وەک کۆ دەکرێنەوە. نموونە: 7+47 + 4.

0000 0111(+7)+  0000 0100(+4)0000 1011(+11)\begin{array}{rl}\mathtt{0000\,0111} & (+7) \\ +\;\mathtt{0000\,0100} & (+4) \\ \hline \mathtt{0000\,1011} & (+11)\end{array}

The sign bit of the answer is 0, so it is positive: 8+2+1=118 + 2 + 1 = 11.

وەڵامەکە 0ـە، کەواتە موجەبە: 8+2+1=118 + 2 + 1 = 11.

Quick checkپشکنینی خێراWhat is 00010010 + 00000101 in 8-bit 2's complement?00010010 + 00000101 بە 8 بیتی چەندە؟

The four casesچوار حاڵەتەکە

Case 2: positive number is largerحاڵەتی 2: ژمارە موجەبەکە گەورەترە

Example: 15+(−6)15 + (-6). The positive number (15) has a larger magnitude than the negative one (6), so the answer is positive. Watch the animation: first −6-6 is made, then the two patterns are added.

نموونە: 15+(−6)15 + (-6). ⁦(15)⁩ لە ⁦(6)⁩ گەورەترە، بۆیە وەڵامەکە موجەبە. سەیری جووڵەکە بکە: سەرەتا −6-6 دروست دەکرێت، پاشان دوو پێکهاتەکە کۆ دەکرێنەوە.

+6 = 00000110 is inverted to 11111001 and 1 is added to give −6 = 11111010. Then 00001111 (+15) and 11111010 (−6) are added column by column; the carries ripple to the left, a 9th bit 1 appears and is crossed out, leaving 00001001 = +9.
+6 = 00000110 is inverted to 11111001 and 1 is added to give −6 = 11111010. Then 00001111 (+15) and 11111010 (−6) are added column by column; the carries ripple to the left, a 9th bit 1 appears and is crossed out, leaving 00001001 = +9.
15+(−6)15 + (-6) in 8 bits: make −6-6, add, drop the 9th bit.15+(−6)15 + (-6) بە 8 : −6-6 دروست بکە، کۆی بکەرەوە و نۆیەم فڕێ بدە.CêIT
0000 0110(+6)1111 10011111 1010(−6)\begin{array}{rl} \mathtt{0000\,0110} & (+6) \\ \mathtt{1111\,1001} & \\ \mathtt{1111\,1010} & (-6)\end{array}

Middle row: every bit of +6+6 inverted (the 1's complement). Bottom row: add 1.

ڕیزی ناوەڕاست: هەموو +6+6 پێچەوانە کراونەتەوە (). ڕیزی خوارەوە: 1 زیاد بکە.

0000 1111(+15)+  1111 1010(−6)1 0000 1001(+9)\begin{array}{rl}\mathtt{0000\,1111} & (+15) \\ +\;\mathtt{1111\,1010} & (-6) \\ \hline \cancel{\mathtt{1}}\,\mathtt{0000\,1001} & (+9)\end{array}

A 9th bit appears; discard it. The answer 00001001 = 8+1=+98 + 1 = +9.

نۆیەم دەردەکەوێت؛ فڕێی بدە. وەڵام 00001001 = 8+18 + 1 = +9+9.

The four casesچوار حاڵەتەکە

Case 3: negative number is largerحاڵەتی 3: ژمارە سالبەکە گەورەترە

Example: 16+(−24)16 + (-24). Now the negative number has the larger magnitude, so the answer is negative. First make −24-24:

نموونە: 16+(−24)16 + (-24). ئێستا گەورەترە، بۆیە وەڵامەکە سالبە. سەرەتا −24-24 دروست بکە:

0001 1000(+24)1110 01111110 1000(−24)\begin{array}{rl} \mathtt{0001\,1000} & (+24) \\ \mathtt{1110\,0111} & \\ \mathtt{1110\,1000} & (-24)\end{array}

Middle row: every bit of +24+24 inverted (the 1's complement). Bottom row: add 1.

ڕیزی ناوەڕاست: هەموو +24+24 پێچەوانە کراونەتەوە (). ڕیزی خوارەوە: 1 زیاد بکە.

0001 0000(+16)+  1110 1000(−24)1111 1000(−8)\begin{array}{rl}\mathtt{0001\,0000} & (+16) \\ +\;\mathtt{1110\,1000} & (-24) \\ \hline \mathtt{1111\,1000} & (-8)\end{array}

No 9th bit this time. The sign bit is 1, so the answer is negative: −128+64+32+16+8=−128+120-128 + 64 + 32 + 16 + 8 = -128 + 120 = −8-8.

ئەمجارە نۆیەم نییە. 1ـە، کەواتە وەڵامەکە سالبە: −128+64+32+16+8=−128+120=−8-128 + 64 + 32 + 16 + 8 = -128 + 120 = -8.

Quick checkپشکنینی خێراIn 16+(−24)16 + (-24), why is there no carry to discard?لە 16+(−24)16 + (-24)دا بۆچی هیچ نییە بۆ فڕێدان؟

The four casesچوار حاڵەتەکە

Case 4: both numbers negativeحاڵەتی 4: هەردوو ژمارەکە سالبن

Example: −25+(−19)-25 + (-19). Make both negative numbers first, and check each one with the −128-128 weight.

نموونە: −25+(−19)-25 + (-19). سەرەتا هەردوو دروست بکە و هەریەکەیان بە −128-128ـی بپشکنە.

NumberژمارەPositiveموجەبInvertپێچەوانەAdd 11 زیاد بکەCheckپشکنین
−25-25000110011110011011100111−128+103=−25-128 + 103 = -25
−19-19000100111110110011101101−128+109=−19-128 + 109 = -19
Column addition of 11100111 (−25) and 11101101 (−19) with the carries above. The result has nine bits; the 9th bit, 1, is crossed out, leaving 11010100 = −44 (−128 + 84). The sign-bit column is shaded.کۆکردنەوەی ستوونیی 11100111 (-25) و 11101101 (-19) و گواستراوەکان لە سەرەوە. ئەنجامەکە نۆ بیتە؛ بیتی نۆیەم (1) سڕاوەتەوە و 11010100 = -44 (-128 + 84) دەمێنێتەوە. ستوونی بیتی نیشانە ڕەنگکراوە.
−25+(−19)=−44-25 + (-19) = -44: the final carry is discarded.−25+(−19)=−44-25 + (-19) = -44: دوایین فڕێ دەدرێت.CêIT

11100111 + 11101101 = 1 11010100. Discard the 9th bit: 11010100 = −128+64+16+4=−128+84-128 + 64 + 16 + 4 = -128 + 84 = −44-44. Two negative numbers gave a negative answer, as they should.

11100111 + 11101101 = 1 11010100. نۆیەم فڕێ بدە: 11010100 = −128+64+16+4=−128+84=−44-128 + 64 + 16 + 4 = -128 + 84 = -44. دوو وەڵامێکی سالبیان دا، وەک پێویستە.

Quick checkپشکنینی خێراWhat is −25+(−19)-25 + (-19) written as 8 bits?−25+(−19)-25 + (-19) بە 8 چۆن دەنووسرێت؟

Why may we throw the carry away?بۆچی دەتوانین گواستراوەکە فڕێ بدەین؟

It seems strange to throw a bit away. Here is why it is correct. An 8-bit pattern stores −6-6 as 11111010, which as an unsigned number is 250=256−6250 = 256 - 6. So adding −6-6 really adds 256−6256 - 6:

فڕێدانی سەیر دیارە. ئەمە هۆکاری ڕاستبوونیەتی. پێکهاتەیەکی 8 بیتی −6-6 وەک 11111010 هەڵدەگرێت کە وەک 250=256−6250 = 256 - 6ـە. کەواتە زیادکردنی −6-6 لە ڕاستیدا 256−6256 - 6 زیاد دەکات:

15+250=265=256+915 + 250 = 265 = 256 + 9

The 9th bit has the weight 256. Dropping it removes exactly that 256, and 9 is left. On the 2's complement wheel this is one full turn: you end where you should.

نۆیەم بە ⁦256⁩ دەژمێردرێت. لابردنی ڕێک ئەو ⁦256⁩ـە لادەبات و 9 دەمێنێتەوە. لەسەر بازنەی تەواوکەری دوو ئەمە یەک خولی تەواوە: لە شوێنی دروستدا دەوەستیت.

Walking round a roundaboutسووڕانەوە بە دەوری فولکەیەکدا

Think of the 256 patterns as 256 shops around a big roundabout. Adding a number means walking forward. Walking 250 shops forward lands on the same shop as walking 6 shops back. Going once all the way round (256 shops) changes nothing — that full lap is the carry you drop.

وای دابنێ ⁦256⁩ پێکهاتەکە ⁦256⁩ دوکانن بە دەوری فولکەیەکی گەورەدا. زیادکردنی ژمارەیەک واتە ڕۆیشتن بۆ پێشەوە. ڕۆیشتنی ⁦250⁩ دوکان بۆ پێشەوە دەتگەیەنێتە هەمان ئەو دوکانەی کە بە گەڕانەوەی 6 دوکان بۆ دواوە دەیگەیتێ. یەک جار سووڕانەوەی تەواو (⁦256⁩ دوکان) هیچ ناگۆڕێت — ئەو خولە تەواوە ئەو کە فڕێی دەدەیت.

Where the analogy stops workingسنووری ئەم نموونەیە

The roundabout has no "too far": you can walk round forever. Numbers can: if the true answer is bigger than +127+127 or smaller than −128-128 it lands on the wrong shop. That is overflow (later in this lesson).

فولکەکە «زۆر دوور»ی نییە: دەتوانیت هەتاهەتایە بسووڕێیتەوە. بەڵام ژمارەکان هەیانە: ئەگەر وەڵامی ڕاستەقینە لە +127+127 گەورەتر یان لە −128-128 بچووکتر بێت، دەگاتە دوکانی هەڵە. ئەمە (دواتر لەم وانەیەدا).

Quick checkپشکنینی خێرا−6-6 is stored as 11111010. What is that pattern as an unsigned number?−6-6 وەک 11111010 هەڵدەگیرێت. ئەم پێکهاتەیە وەک چەندە؟

Subtraction: add the 2's complementلێدەرکردن: تەواوکەری دوو زیاد بکە

Subtracting B is the same as adding −B. So a computer never needs a separate subtractor:

لێدەرکردنی B هەمان زیادکردنی −B-Bـە. بۆیە هەرگیز پێویستی بە ئامێرێکی جیای لێدەرکردن نییە:

A−B=A+(−B)A - B = A + (-B)
  1. Take the 2's complement of the number you subtract (the subtrahend): invert every bit and add 1. This changes its sign, whatever the sign was.

    ئەو ژمارەیە وەربگرە کە لێی دەردەکەیت (لێدەرکراو): هەموو پێچەوانە بکەرەوە و 1 زیاد بکە. ئەمە نیشانەکەی دەگۆڕێت، هەر نیشانەیەک بووبێت.

  2. Add it to the first number.

    بیخە سەر ژمارەی یەکەم.

  3. Discard any final carry.

    هەر دوایین هەبوو فڕێی بدە.

If the subtrahend is negative, its 2's complement is positive: subtracting a negative number adds a positive one, e.g. 12−(−9)=12+912 - (-9) = 12 + 9.

ئەگەر لێدەرکراوەکە سالب بێت، موجەبە: لێدەرکردنی وەک زیادکردنی ، بۆ نموونە 12−(−9)=12+912 - (-9) = 12 + 9.

Quick checkپشکنینی خێراTo compute 20−720 - 7 in 8 bits, what does the computer actually add?بۆ هەژمارکردنی 20−720 - 7 بە 8 ، لە ڕاستیدا چی کۆ دەکاتەوە؟

Subtraction: add the 2's complementلێدەرکردن: تەواوکەری دوو زیاد بکە

Four worked subtractionsچوار لێدەرکردنی شیکراوە

  1. 00001000 − 0000001100001000 − 00000011

    +8−(+3)=+8+(−3)+8 - (+3) = +8 + (-3). 2's complement of 00000011: invert → 11111100, add 1 → 11111101 (−3-3). Then 00001000 + 11111101 = 1 00000101 → discard the carry → 00000101 = +5+5.

    +8−(+3)=+8+(−3)+8 - (+3) = +8 + (-3). 00000011: پێچەوانە ← 11111100، 1 زیاد بکە ← 11111101 (−3-3). پاشان 00001000 + 11111101 = 1 00000101 ← فڕێ بدە ← 00000101 = +5+5.

  2. 00001100 − 1111011100001100 − 11110111

    +12−(−9)=+12+9+12 - (-9) = +12 + 9: subtracting a negative number is adding a positive one. 2's complement of 11110111: invert → 00001000, add 1 → 00001001 (+9+9). Then 00001100 + 00001001 = 00010101 = +21+21.

    +12−(−9)=+12+9+12 - (-9) = +12 + 9: لێدەرکردنی وەک زیادکردنی . 11110111: پێچەوانە ← 00001000، 1 زیاد بکە ← 00001001 (+9+9). پاشان 00001100 + 00001001 = 00010101 = +21+21.

  3. 11100111 − 0001001111100111 − 00010011

    −25−(+19)=−25+(−19)-25 - (+19) = -25 + (-19). The 2's complement of 00010011 is 11101101 (−19-19), so this is exactly case 4: the answer is 11010100 = −44-44.

    −25−(+19)=−25+(−19)-25 - (+19) = -25 + (-19). 00010011 دەکاتە 11101101 (−19-19)، کەواتە ئەمە ڕێک حاڵەتی 4ـە: وەڵامەکە 11010100 = −44-44.

  4. 10001000 − 1110001010001000 − 11100010

    −120−(−30)=−120+30-120 - (-30) = -120 + 30: again, subtracting a negative number is adding a positive one. 2's complement of 11100010: invert → 00011101, add 1 → 00011110 (+30+30). Then 10001000 + 00011110 = 10100110 = −128+32+4+2=−128+38-128 + 32 + 4 + 2 = -128 + 38 = −90-90.

    −120−(−30)=−120+30-120 - (-30) = -120 + 30: دیسان لێدەرکردنی وەک زیادکردنی . 11100010: پێچەوانە ← 00011101، 1 زیاد بکە ← 00011110 (+30+30). پاشان 10001000 + 00011110 = 10100110 = −128+32+4+2=−128+38=−90-128 + 32 + 4 + 2 = -128 + 38 = -90.

Quick checkپشکنینی خێراWhat is 00001010 − 00001100 (10−1210 - 12) in 8-bit 2's complement?00001010 − 00001100 (10−1210 - 12) بە 8 بیتی چەندە؟

Overflow: when the answer does not fitسەرڕێژبوون: کاتێک وەڵامەکە جێی نابێتەوە

8 bits hold only −128-128 to +127+127. What is 100+50100 + 50? The true answer, 150, does not fit. The adder still produces 8 bits — and they are wrong:

8 تەنها −128-128 تا +127+127 هەڵدەگرن. 100+50100 + 50 چەندە؟ وەڵامی ڕاستەقینە ⁦(150)⁩ جێی نابێتەوە. هەر 8 دەدات — و هەڵەن:

Definitionپێناسە

An error in which the true result of a calculation does not fit in the available bits. In 2's-complement addition it happens when two numbers with the same sign give a result with the other sign.

هەڵەیەکە کە تێیدا ئەنجامی ڕاستەقینەی هەژمارێک لە جێی نابێتەوە. لە کۆکردنەوەی ڕوو دەدات کاتێک دوو ژمارەی هاونیشانە ئەنجامێک دەدەن کە نیشانەکەی پێچەوانەیە.

01100100 (+100) plus 00110010 (+50) gives 10010110, which reads as −106. The carry into the sign-bit column is 1 but the carry out of it is 0.01100100 (+100) + 00110010 (+50) دەکاتە 10010110 کە وەک -106 دەخوێنرێتەوە. گواستراوە بۆ ناو ستوونی بیتی نیشانە 1ـە، بەڵام گواستراوە بۆ دەرەوەی 0ـە.
100+50100 + 50 in 8 bits: two positive numbers give a "negative" answer.100+50100 + 50 بە 8 : دوو وەڵامێکی «سالب» دەدەن.CêIT

Two positive numbers cannot add up to a negative number, so the answer is clearly wrong. This is overflow. The same happens below −128-128: −100+(−50)-100 + (-50) gives 1 01101010 → 01101010 = +106+106.

دوو ناتوانن کۆیان سالب بێت، بۆیە وەڵامەکە بە ئاشکرا هەڵەیە. ئەمە . هەمان شت لە خوار −128-128ـەوە ڕوو دەدات: −100+(−50)-100 + (-50) دەکاتە 1 01101010 ← 01101010 = +106+106.

Quick checkپشکنینی خێراIn 8-bit 2's complement, the adder gives 10010110 for 100+50100 + 50. What does that pattern mean?لە 8 بیتیدا بۆ 100+50100 + 50 10010110 دەدات. ئەم پێکهاتەیە واتای چییە؟

Overflow: when the answer does not fitسەرڕێژبوون: کاتێک وەڵامەکە جێی نابێتەوە

Two ways to spot overflowدوو ڕێگا بۆ ناسینەوەی سەرڕێژبوون

  • Sign rule: overflow can happen only when both numbers have the same sign. It has happened if the answer has the other sign.
  • Carry rule: look at the sign-bit column. Overflow has happened if the carry into it differs from the carry out of it.
  • یاسای نیشانە: تەنها کاتێک ڕوو دەدات کە هەردوو ژمارەکە هەمان نیشانەیان هەبێت. ئەگەر وەڵامەکە نیشانەی پێچەوانەی هەبێت، ڕووی داوە.
  • یاسای : سەیری ستوونی بکە. ئەگەر بۆ ناوی جیاواز بێت لە بۆ دەرەوەی، ڕووی داوە.

Why can two numbers with different signs never overflow? Their sum always lies between them, and both of them already fit.

بۆچی دوو ژمارە بە نیشانەی جیاواز هەرگیز نادەن؟ کۆکەیان هەمیشە لە نێوانیاندایە و هەردووکیان پێشتر جێیان بووەتەوە.

The stadium scoreboardتابلۆی یاریگا

An old stadium scoreboard has two digit panels for each team. After 99 goals, one more goal shows 00. The board is not broken; the true score simply needs a third digit that the board does not have.

تابلۆیەکی کۆنی یاریگا بۆ هەر تیمێک دوو پانێڵی هەیە. دوای ⁦99⁩ گۆڵ، گۆڵێکی تر ⁦00⁩ پیشان دەدات. تابلۆکە تێک نەچووە؛ ئەنجامی ڕاستەقینە تەنها پێویستی بە سێیەمە کە تابلۆکە نییەتی.

Where the analogy stops workingسنووری ئەم نموونەیە

In 2's complement the wrap does not go to 0 but across the line between +127+127 and −128-128, so the wrong answer even has the wrong sign. That is what makes the sign rule work.

لە سووڕانەوەکە ناچێتە سەر 0، بەڵکو بە سەر هێڵی نێوان +127+127 و −128-128دا دەپەڕێتەوە، بۆیە وەڵامە هەڵەکە تەنانەت نیشانەکەشی هەڵەیە. ئەمەیە کە یاسای نیشانە کارپێدەکات.

Quick checkپشکنینی خێراWhich 8-bit addition overflows?کام کۆکردنەوەی 8 بیتی دەدات؟

Overflow: when the answer does not fitسەرڕێژبوون: کاتێک وەڵامەکە جێی نابێتەوە

A carry out is not overflowگواستراوەی دەرەوە سەرڕێژبوون نییە

Do not confuse the discarded carry with overflow. In the examples of this lesson a 9th bit appeared several times and every answer was right. The table compares the carry into the sign column with the carry out of it. For unsigned numbers the rule is different: there the carry out of the leftmost bit is the overflow (When the answer does not fit).

فڕێدراو و تێکەڵ مەکە. لە نموونەکانی ئەم وانەیەدا چەند جار نۆیەم دەرکەوت و هەموو وەڵامەکان ڕاست بوون. خشتەکە بۆ ناو ستوونی نیشانە لەگەڵ بۆ دەرەوەی بەراورد دەکات. بۆ یاساکە جیاوازە: لەوێدا دەرەوەی لای چەپترین خۆی (کاتێک وەڵامەکە جێی نابێتەوە).

Carries in the sign column لە ستوونی نیشانەدا
SumکۆکردنەوەCarry into sign bit بۆ ناو Carry out بۆ دەرەوە8-bit answerوەڵامی 8 بیتیCorrect?ڕاستە؟
15+(−6)15 + (-6)11+9+9yesبەڵێ
16+(−24)16 + (-24)00−8-8yesبەڵێ
−25+(−19)-25 + (-19)11−44-44yesبەڵێ
−64+(−64)-64 + (-64)11−128-128yesبەڵێ
100+50100 + 5010−106-106no: overflowنەخێر:
−100+(−50)-100 + (-50)01+106+106no: overflowنەخێر:
Quick checkپشکنینی خێرا−25+(−19)-25 + (-19) produced a 9th bit. Was there overflow?−25+(−19)-25 + (-19) نۆیەمی دا. ئایا ڕووی دا؟

An exam question: −27+(−37)-27 + (-37)پرسیارێکی تاقیکردنەوە: −27+(−37)-27 + (-37)

A midterm asked: express each number as an 8-bit signed number and add: −27+(−37)-27 + (-37). Here is the full working.

پرسیارێکی تاقیکردنەوەی نیوەی وەرز: هەر ژمارەیەک وەک 8 بیتی بنووسە و کۆیان بکەرەوە: −27+(−37)-27 + (-37). ئەمە هەموو هەژمارەکەیە.

  1. Sign-magnitude formsشێوەکانی نیشانە و نرخی ڕووت

    +27+27 = 00011011 → sign-magnitude 10011011. +37+37 = 00100101 → sign-magnitude 10100101.

    +27+27 = 00011011 ← 10011011. +37+37 = 00100101 ← 10100101.

  2. 2's complement formsشێوەکانی تەواوکەری دوو

    −27-27: invert 00011011 → 11100100, add 1 → 11100101. −37-37: invert 00100101 → 11011010, add 1 → 11011011.

    −27-27: 00011011 پێچەوانە بکەرەوە ← 11100100، 1 زیاد بکە ← 11100101. −37-37: 00100101 پێچەوانە بکەرەوە ← 11011010، 1 زیاد بکە ← 11011011.

  3. Addکۆ بکەرەوە

    11100101 + 11011011 = 1 11000000.

    11100101 + 11011011 = 1 11000000.

    1110 0101(−27)+  1101 1011(−37)1 1100 0000(−64)\begin{array}{rl}\mathtt{1110\,0101} & (-27) \\ +\;\mathtt{1101\,1011} & (-37) \\ \hline \cancel{\mathtt{1}}\,\mathtt{1100\,0000} & (-64)\end{array}
  4. Read the answerوەڵامەکە بخوێنەرەوە

    Discard the 9th bit: 11000000 = −128+64-128 + 64 = −64-64. Both inputs negative, answer negative, carries into and out of the sign bit both 1: no overflow.

    نۆیەم فڕێ بدە: 11000000 = −128+64=−64-128 + 64 = -64. هەردوو ژمارەکە سالبن، وەڵامەکەش سالبە و ناو و دەرەوەی هەردووکیان 1ن: نییە.

Exercisesڕاهێنانەکان

ExerciseڕاهێنانAdd the signed numbersژمارە نیشانەدارەکان کۆ بکەرەوە

Add in 8-bit 2's complement: (a) 7+47 + 4 (b) 15+(−6)15 + (-6) (c) 16+(−24)16 + (-24) (d) −5+(−9)-5 + (-9).

بە 8 بیتی کۆیان بکەرەوە: (أ) 7+47 + 4 (ب) 15+(−6)15 + (-6) (ج) 16+(−24)16 + (-24) (د) −5+(−9)-5 + (-9).

Show answerوەڵامەکە پیشان بدە
  • (a) 00000111 + 00000100 = 00001011 = +11+11
  • (b) 00001111 + 11111010 = 1 00001001 → 00001001 = +9+9
  • (c) 00010000 + 11101000 = 11111000 = −8-8
  • (d) −5-5 = 11111011 (from 00000101 → 11111010 → 11111011), −9-9 = 11110111 (from 00001001 → 11110110 → 11110111); 11111011 + 11110111 = 1 11110010 → 11110010 = −128+114-128 + 114 = −14-14
  • (أ) 00000111 + 00000100 = 00001011، واتە +11+11
  • (ب) 00001111 + 11111010 = 1 00001001؛ فڕێ بدە: 00001001، واتە +9+9
  • (ج) 00010000 + 11101000 = 11111000، واتە −8-8
  • (د) −5-5 = 11111011 (لە 00000101 ← 11111010 ← 11111011)، −9-9 = 11110111 (لە 00001001 ← 11110110 ← 11110111)؛ 11111011 + 11110111 = 1 11110010 ← 11110010 = −128+114-128 + 114 = −14-14
ExerciseڕاهێنانSubtract the signed numbersژمارە نیشانەدارەکان لێک دەربکە

Subtract (8-bit 2's complement): (a) 00001000 − 00000011 (b) 00001100 − 11110111 (c) 11010111 − 00100011 (d) 10001000 − 11100010.

لێک دەریان بکە (بە 8 بیتی): (أ) 00001000 − 00000011 (ب) 00001100 − 11110111 (ج) 11010111 − 00100011 (د) 10001000 − 11100010.

Show answerوەڵامەکە پیشان بدە
  • (a) 8−38 - 3: 00001000 + 11111101 = 1 00000101 → +5+5
  • (b) 12−(−9)12 - (-9): 00001100 + 00001001 = 00010101 → +21+21
  • (c) −41−35-41 - 35: 2's complement of 00100011 is 11011101 (−35-35); 11010111 + 11011101 = 1 10110100 → 10110100 = −128+52-128 + 52 = −76-76
  • (d) −120−(−30)-120 - (-30): 10001000 + 00011110 = 10100110 → −90-90
  • (أ) 8−38 - 3: 00001000 + 11111101 = 1 00000101؛ فڕێ بدە: +5+5
  • (ب) 12−(−9)12 - (-9): 00001100 + 00001001 = 00010101، واتە +21+21
  • (ج) −41−35-41 - 35: 00100011 دەکاتە 11011101 (−35-35)؛ 11010111 + 11011101 = 1 10110100 ← 10110100 = −128+52-128 + 52 = −76-76
  • (د) −120−(−30)-120 - (-30): 10001000 + 00011110 = 10100110، واتە −90-90
ExerciseڕاهێنانOverflow or not?سەرڕێژبوون یان نا؟

Add in 8-bit 2's complement and say whether the answer overflows: (a) 120+10120 + 10 (b) 90+(−40)90 + (-40) (c) −128+(−1)-128 + (-1). (d) In 4 bits: 5+45 + 4.

بە 8 بیتی کۆیان بکەرەوە و بڵێ ئایا وەڵامەکە دەدات: (أ) 120+10120 + 10 (ب) 90+(−40)90 + (-40) (ج) −128+(−1)-128 + (-1). (د) بە 4 : 5+45 + 4.

Hintئاماژە

Same signs in, other sign out → overflow.

نیشانەی وەک یەک بۆ ناوەوە و نیشانەی پێچەوانە بۆ دەرەوە ← .

Show answerوەڵامەکە پیشان بدە
  • (a) 01111000 + 00001010 = 10000010 = −126-126. Two positives gave a negative: overflow (130>127130 > 127).
  • (b) 01011010 + 11011000 = 1 00110010 → 00110010 = +50+50. Different signs: no overflow.
  • (c) 10000000 + 11111111 = 1 01111111 → 01111111 = +127+127. Two negatives gave a positive: overflow (−129<−128-129 < -128).
  • (d) 0101 + 0100 = 1001 = −7-7 in 4 bits. Overflow: 4 bits hold only −8-8 … +7+7.
  • (أ) 01111000 + 00001010 = 10000010 = −126-126. دوو موجەب سالبیان دا: (130>127130 > 127).
  • (ب) 01011010 + 11011000 = 1 00110010 ← 00110010 = +50+50. نیشانەی جیاواز: نییە.
  • (ج) 10000000 + 11111111 = 1 01111111 ← 01111111 = +127+127. دوو سالب موجەبیان دا: (−129<−128-129 < -128).
  • (د) 0101 + 0100 = 1001 = −7-7 بە 4 . : 4 تەنها −8-8 … +7+7 هەڵدەگرن.
Quick checkپشکنینی خێراWhat is 01000000 + 01000000 (64+6464 + 64) in 8-bit 2's complement?01000000 + 01000000 (64+6464 + 64) بە 8 بیتی چییە؟

Common mistakesهەڵە باوەکان

MistakeهەڵەFixچارەسەر
Adding numbers of different widths, e.g. 1011 + 00000110.کۆکردنەوەی ژمارە بە پانیی جیاواز، بۆ نموونە 1011 + 00000110.Write both numbers with the same number of bits before you add.پێش کۆکردنەوە هەردوو ژمارەکە بە هەمان ژمارەی بنووسە.
Keeping the 9th bit and reading a 9-bit answer.هێشتنەوەی نۆیەم و خوێندنەوەی وەڵامێکی 9 بیتی.Discard the final carry; the answer has 8 bits.دوایین فڕێ بدە؛ وەڵامەکە 8 .
Thinking a carry out means the answer is wrong.وا بزانیت دەرەوە واتە وەڵامەکە هەڵەیە.Check overflow with the sign rule or the carry rule, not with the carry out alone. بە یاسای نیشانە یان یاسای بپشکنە، نەک تەنها بە دەرەوە.
Subtracting by taking the 2's complement of the first number.لێدەرکردن بە وەرگرتنی ژمارەی یەکەم.Complement the number you take away (the subtrahend), then add. ئەو ژمارەیە وەربگرە کە لێی دەردەکەیت (لێدەرکراو)، پاشان کۆی بکەرەوە.
Adding sign-magnitude patterns bit by bit.کۆکردنەوەی پێکهاتەکانی بە .Turn negative numbers into 2's complement first.سەرەتا بگۆڕە بۆ .
Reading a negative answer like 11010100 as 212 or −84-84.خوێندنەوەی وەڵامێکی سالبی وەک 11010100 بە ⁦212⁩ یان −84-84.Use the −128-128 weight: −128+84=−44-128 + 84 = -44. بە −128-128 بژمێرە: −128+84=−44-128 + 84 = -44.

Summaryپوختە

  • In 2's complement, signed numbers are added like unsigned numbers, sign bits included.
  • Discard any carry out of the leftmost bit: it is one full turn of 256 (for 8 bits).
  • The four cases (both positive, positive larger, negative larger, both negative) all use the same method.
  • A−B=A+(−B)A - B = A + (-B): take the 2's complement of the subtrahend and add.
  • Overflow: the true answer is outside −128-128 … +127+127. It happens only with same-sign inputs; the answer then has the wrong sign, and the carries into and out of the sign bit differ.
  • لە وەک کۆ دەکرێنەوە، .
  • هەر لە لای چەپترین دەرچوو فڕێی بدە: یەک خولی تەواوی ⁦256⁩ـە (بۆ 8 ).
  • چوار حاڵەتەکە (هەردووکیان موجەب، موجەب گەورەتر، سالب گەورەتر، هەردووکیان سالب) هەموویان هەمان ڕێگا بەکار دەهێنن.
  • A−B=A+(−B)A - B = A + (-B): لێدەرکراوەکە وەربگرە و کۆی بکەرەوە.
  • : وەڵامی ڕاستەقینە لە دەرەوەی −128-128 … +127+127ـە. تەنها لەگەڵ ژمارەی هاونیشانە ڕوو دەدات؛ ئەو کاتە وەڵامەکە نیشانەی هەڵەی هەیە و ناو و دەرەوەی جیاوازن.
Review questionsپرسیارەکانی پێداچوونەوە
Why does a computer not need a subtractor circuit?بۆچی کۆمپیوتەر پێویستی بە سووڕێکی لۆجیکیی تایبەت بە لێدەرکردن نییە؟

Because A−B=A+(−B)A - B = A + (-B), and −B-B is just the 2's complement of BB, made by inverting the bits and adding 1. The same adder does the rest.

چونکە A−B=A+(−B)A - B = A + (-B) و −B-B تەنها Bـیە کە بە پێچەوانەکردنەوەی و زیادکردنی 1 دروست دەبێت. هەمان ئەوەی تر دەکات.

Can 50+(−100)50 + (-100) overflow in 8 bits?ئایا 50+(−100)50 + (-100) بە 8 بیت سەرڕێژبوون دەدات؟

No. The signs differ, so the sum (−50-50) lies between the two numbers and always fits.

نەخێر. نیشانەکان جیاوازن، بۆیە کۆکە (−50-50) لە نێوان دوو ژمارەکەدایە و هەمیشە جێی دەبێتەوە.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

Binary: Plusses & Minuses (Why We Use Two's Complement) - ComputerphileComputerphile · Englishئینگلیزی · 435K viewsبینین · 12K likesلایک · Partبەشی 2:50–16:15 (ofلە 16:15)

Why computers use 2's complement: sign-and-magnitude spoils ordinary addition, while 2's complement lets one adder add signed numbers and flag overflow.

هۆی بەکارهێنانی : کۆکردنەوەی ئاسایی تێک دەدات، بەڵام بە یەک کۆ دەکات و دەدۆزێتەوە.

Starts at 2:50 (binary additions with signs); 5:31 two's complement; 10:21 a worked example.

لە ⁦2:50⁩ـەوە دەست پێ دەکات (کۆکردنەوەی بە )؛ ⁦5:31⁩ ؛ ⁦10:21⁩ نموونەیەکی شیکراوە.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Binary Coded Decimal (BCD)کۆدی دەیی دووانی

Related lecturesوانە پەیوەندیدارەکان

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.