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

Signed Numbers: Sign-Magnitude, 1's and 2's Complementژمارە نیشانەدارەکان: نیشانە و نرخی ڕووت، تەواوکەری یەک و تەواوکەری دوو

How a computer stores negative integers with only 0s and 1s: unsigned numbers, the sign bit, and the three signed forms — sign-magnitude, 1's complement and 2's complement — with their ranges, their zeros, and why 2's complement is used everywhere today.

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

18 min read19 خولەک خوێندنەوە · 20 stepsهەنگاو

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

  • Give the range of unsigned numbers with n bits.
  • Write a negative number in sign-magnitude, 1's complement and 2's complement.
  • Read the decimal value of a signed number in each form (the −128-128 weight trick).
  • State the correct range of each form and explain why 2's complement has one zero and one extra negative value.
  • بچووکترین و گەورەترین بە n بنووسیت.
  • بە ، و بنووسیت.
  • بە هەر شێوەیەک بێت بخوێنیتەوە و بەهاکەی بە بنووسیت (ڕێگای −128-128 بۆ ).
  • سنووری دروستی بەهاکانی هەر شێوەیەک بڵێیت و ڕوون بکەیتەوە بۆچی یەک سفر و یەک بەهای سالبی زیاتری هەیە.

Numbers with a signژمارە بە نیشانەوە

Everything in a computer's is bits. Some bits are instructions (orders for the ) and the rest are data. Data comes in a few formats:

هەموو شتێک لە . هەندێک لە (فەرمان بۆ ) و ئەوانی تر . چەند شێوەیەکی هەیە:

  • Numbers: whole numbers () such as 25 or −25-25, and real numbers with a fraction part such as 3.75.
  • Character codes: letters, digits and symbols, for example ASCII (a later lesson).
  • ژمارە: وەک ⁦25⁩ یان −25-25 و کە بەشی کەرتیان هەیە وەک ⁦3.75⁩.
  • کۆدی نووسە: پیت، و هێما، بۆ نموونە (لە وانەیەکی دواتردا).

This lesson is about integers that can be negative. Bits are only 0 and 1. There is no minus sign inside a computer. So how can a computer store −25-25? Engineers found three answers. You will learn all three, and why one of them won.

ئەم وانەیە دەربارەی کە دەتوانن سالب بن. تەنها 0 و 1ن و نیشانەی کەم ⁦(−)⁩ لە ناو نییە. کەواتە چۆن −25-25 هەڵدەگرێت؟ ئەندازیاران سێ وەڵامیان دۆزییەوە. لێرەدا فێری هەر سێکیان دەبیت و دەزانیت بۆچی یەکێکیان سەرکەوت.

Quick checkپشکنینی خێراWhich of these is data, not an instruction?کامیان نەک ؟

Unsigned numbers: only zero and upژمارە بێنیشانەکان: تەنها سفر و سەرەوە

The usual way to store a whole number is plain binary. With n bits there are 2n2^n different patterns of 0s and 1s. If every pattern means a number from 0 upwards, we call these numbers unsigned.

ڕێگای ئاسایی بۆ هەڵگرتنی سادەیە. بە n 2n2^n پێکهاتەی جیاوازی 0 و 1 هەیە. ئەگەر هەر پێکهاتەیەک ژمارەیەک بێت لە 0 بەرەو سەرەوە، پێیان دەڵێین .

Definitionپێناسە

A binary number with no sign bit: all its bits show the size, so it is never negative. With n bits it can hold 0 to 2n−12^n - 1.

ژمارەیەکی بەبێ : هەموو قەبارەکەی پیشان دەدەن، بۆیە هەرگیز سالب نییە. بە n بیت دەتوانێت 0 تا 2n−12^n - 1 هەڵبگرێت.

0  ≤  x  ≤  2n−10 \;\le\; x \;\le\; 2^n - 1

An unsigned number xx with n bits lies between 0 and 2n−12^n - 1. With 8 bits: 28=2562^8 = 256 patterns, from 00000000 = 0 to 11111111 = 255.

وەک xx بە n لە نێوان 0 و 2n−12^n - 1ـدایە. بە 8 : 28=2562^8 = 256 پێکهاتە، لە 00000000 = 0 تا 11111111 = ⁦255⁩.

1111 1111+  0000 00011 0000 0000\begin{array}{r}\mathtt{1111\,1111} \\ +\;\mathtt{0000\,0001} \\ \hline \mathtt{1\,0000\,0000}\end{array}

Why is 255 the largest? Add 1 to 11111111: every column becomes 1+1=1021 + 1 = 10_2, so it writes 0 and carries 1. The last carry needs a ninth bit, and 1 00000000 is 256=28256 = 2^8. Eight bits cannot hold it, so they stop at 28−12^8 - 1. A register that loses this ninth bit has an unsigned overflow (When the answer does not fit).

بۆچی ⁦255⁩ گەورەترینە؟ 1 بخە سەر 11111111: هەموو ستوونێک دەبێتە 1+1=1021 + 1 = 10_2، بۆیە 0 دەنووسێت و 1 دەگوازێتەوە. دوایین پێویستی بە نۆیەم هەیە و 1 00000000 دەکاتە 256=28256 = 2^8. هەشت ناتوانن هەڵیبگرن، بۆیە لە 28−12^8 - 1دا دەوەستن. ئەو کە ئەم نۆیەمە لەدەست دەدات تووشی دەبێت (کاتێک وەڵامەکە جێی نابێتەوە).

This works only for positive numbers and zero. To store negative numbers we must give up something: one bit must tell the sign.

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

Quick checkپشکنینی خێراHow many different values can 8 bits hold, and what is the largest unsigned value?8 چەند بەهای جیاواز هەڵدەگرن و گەورەترین چەندە؟

The sign bitبیتی نیشانە

All three ways of storing signed numbers use the same idea: the leftmost bit (the ) is the sign bit.

هەر سێ ڕێگاکەی هەڵگرتنی هەمان بیرۆکە بەکار دەهێنن: لای چەپترین () .

  • Sign bit 0 → the number is positive (or zero).
  • Sign bit 1 → the number is negative.
  • 0 ← ژمارەکە موجەبە (یان سفرە).
  • 1 ← ژمارەکە سالبە.
Definitionپێناسە

The leftmost bit of a signed binary number: 0 means positive and 1 means negative.

لای چەپترین : 0 واتە موجەب و 1 واتە سالب.

The shopkeeper's debt bookدەفتەری قەرزی دوکاندار

A shopkeeper in the bazaar keeps a debt book. Each line has a small first column with a mark — owes or paid in advance — and then the amount. The amount is always written the same way; only the mark changes. The sign bit is that first column.

دوکاندارێک لە بازاڕ دەفتەرێکی قەرزی هەیە. هەر دێڕێک ستوونێکی بچووکی یەکەمی هەیە بە نیشانەیەک — قەرزدارە یان پێشەکی داوە — و پاشان بڕی پارەکە. بڕەکە هەمیشە بە هەمان شێوە دەنووسرێت؛ تەنها نیشانەکە دەگۆڕێت. ئەو ستوونە یەکەمەیە.

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

The book can use as many digits as it needs, but a computer has a fixed number of bits. Because one bit is used for the sign, fewer bits are left for the size, so the largest number gets smaller.

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

Quick checkپشکنینی خێراIn a signed 8-bit number, which bit is the sign bit?لە 8 بیتیدا کام ؟

The sign bitبیتی نیشانە

Three ways, one differenceسێ ڕێگا، یەک جیاوازی

The three ways are sign-magnitude, 1's complement and 2's complement. They agree on positive numbers: a positive number is always the sign bit 0 followed by its normal binary value. For example, +10+10 in 5 bits is 01010 in all three. They differ only in how they write negative numbers.

سێ ڕێگاکە ئەمانەن: ، و . لەسەر یەک دەگرنەوە: هەمیشە 0ـە و لە دوایدا بەها ئاساییەکەی دێت. بۆ نموونە +10+10 بە 5 لە هەر سێکیاندا 01010ـە. جیاوازییان تەنها لە نووسینی ژمارە سالبەکاندایە.

Quick checkپشکنینی خێراWhere do sign-magnitude, 1's complement and 2's complement differ?، و لە چیدا جیاوازن؟

Way 1: sign-magnitudeڕێگای 1: نیشانە و نرخی ڕووت

Sign-magnitude is the way we write numbers on paper: a sign, then the size. In an n-bit number the sign bit is on the left and the magnitude (the size without the sign) fills the other n−1n-1 bits. A negative number has the same magnitude bits as its positive partner; only the sign bit changes from 0 to 1.

هەمان ئەو ڕێگایەیە کە لەسەر کاغەز ژمارەی پێ دەنووسین: نیشانەیەک و پاشان قەبارەکە. لە ژمارەیەکی n بیتیدا لای چەپە و (قەبارەکە بەبێ نیشانە) n−1n-1 تر پڕ دەکاتەوە. هەمان هاوتا موجەبەکەی هەیە؛ تەنها لە 0ـەوە دەبێتە 1.

Definitionپێناسە

A way to write signed binary numbers: the leftmost bit is the sign and the other bits are the magnitude (the size without the sign). It has two zeros, +0+0 and −0-0.

ڕێگەیەکە بۆ نووسینی : لای چەپترین بیت و بیتەکانی تر نرخی ڕووتی ژمارەکەن (قەبارەکەی بەبێ نیشانە). دوو سفری هەیە: +0+0 و −0-0.

+18 is 0 0010010 and −18 is 1 0010010 in 8-bit sign-magnitude. The first box is the sign bit; the other seven boxes, 0010010 = 18, are the same in both rows.+18 دەبێتە 0 0010010 و -18 دەبێتە 1 0010010 بە نیشانە و نرخی ڕووتی 8 بیتی. خانەی یەکەم بیتی نیشانەیە؛ حەوت خانەکەی تر (0010010 = 18) لە هەردوو ڕیزەکەدا وەک یەکن.
±18 in 8-bit sign-magnitude: only the sign bit differs.±18\pm 18 بە 8 بیتی: تەنها جیاوازە.CêIT
Sign-magnitude examplesنموونەکانی
NumberژمارەBitsSign bitMagnitude
+18+18 (8 bits)+18+18 (⁦8⁩ )0001001000010010 = 18
−18-18 (8 bits)−18-18 (⁦8⁩ )1001001010010010 = 18
+7+7 (6 bits)+7+7 (⁦6⁩ )000111000111 = 7
−7-7 (6 bits)−7-7 (⁦6⁩ )100111100111 = 7
+127+127 (8 bits)+127+127 (⁦8⁩ )0111111101111111 = 127
−127-127 (8 bits)−127-127 (⁦8⁩ )1111111111111111 = 127
Quick checkپشکنینی خێراWhat is −18-18 in 8-bit sign-magnitude?−18-18 بە 8 بیتی چییە؟

Way 1: sign-magnitudeڕێگای 1: نیشانە و نرخی ڕووت

Reading a sign-magnitude numberخوێندنەوەی ژمارەیەک بە نیشانە و نرخی ڕووت

To find the decimal value, split the bits into two parts: the sign bit, and the magnitude bits. Change the magnitude to decimal, then put the sign in front.

بۆ دۆزینەوەی بەها ، بکە بە دوو بەش: و . بگۆڕە بۆ و پاشان نیشانەکە بخە پێشی.

  1. 1 00101011 0010101

    Sign bit 1 → negative. Magnitude 0010101 = 16+4+1=2116 + 4 + 1 = 21. Value: −21-21.

    1ـە ← سالب. 0010101 = 16+4+1=2116 + 4 + 1 = 21. بەها: −21-21.

  2. 0 11101110 1110111

    Sign bit 0 → positive. Magnitude 1110111 = 64+32+16+4+2+1=11964 + 32 + 16 + 4 + 2 + 1 = 119. Value: +119+119.

    0ـە ← موجەب. 1110111 = 64+32+16+4+2+1=11964 + 32 + 16 + 4 + 2 + 1 = 119. بەها: +119+119.

Quick checkپشکنینی خێراWhat is the value of the sign-magnitude number 00010111?بەهای ژمارەی 00010111 بە چەندە؟

Way 1: sign-magnitudeڕێگای 1: نیشانە و نرخی ڕووت

Range and the two zerosسنووری بەهاکان و دوو سفرەکە

With n bits, the magnitude has n−1n-1 bits, so its largest value is 2n−1−12^{n-1}-1. Each magnitude can be positive or negative:

بە n ، n−1n-1 هەیە، بۆیە گەورەترین بەهای 2n−1−12^{n-1}-1ـە. هەر دەتوانێت موجەب یان سالب بێت:

−(2n−1−1)  ≤  x  ≤  +(2n−1−1)-(2^{n-1}-1) \;\le\; x \;\le\; +(2^{n-1}-1)

For 8 bits: −(128−1)=−127-(128-1) = -127 (11111111) up to +127+127 (01111111).

بۆ 8 : −(128−1)=−127-(128-1) = -127 (11111111) تا +127+127 (01111111).

Count the values: 127 negative + 127 positive + zero = 255. But 8 bits have 256 patterns. Where is the missing one? Zero is written twice: 00000000 (+0+0) and 10000000 (−0-0). Two zeros waste a pattern, and a circuit must check both when it asks "is this zero?". Adding is also harder: the circuit must look at the signs first, then decide to add or subtract the magnitudes.

بەهاکان بژمێرە: ⁦127⁩ سالب + ⁦127⁩ موجەب + سفر = ⁦255⁩. بەڵام 8 ⁦256⁩ پێکهاتەیان هەیە. ئەوەی ون بووە لە کوێیە؟ سفر دوو جار نووسراوە: 00000000 (+0+0) و 10000000 (−0-0). دوو سفر پێکهاتەیەک بە فیڕۆ دەدەن و کاتێک دەپرسێت «ئایا ئەمە سفرە؟» دەبێت هەردووکیان بپشکنێت. کۆکردنەوەش قورسترە: سەرەتا دەبێت سەیری نیشانەکان بکات و پاشان بڕیار بدات کۆ بکاتەوە یان لێک دەربکات.

Quick checkپشکنینی خێراWhich two 8-bit patterns both mean zero in sign-magnitude?کام دوو پێکهاتەی 8 بیتی لە هەردووکیان واتای سفر دەدەن؟

Way 2: 1's complementڕێگای 2: تەواوکەری یەک

In 1's complement, positive numbers are the same as before. To make a negative number, take the positive number with all its bits (sign bit included) and invert every bit: each 0 becomes 1 and each 1 becomes 0. You met this operation in 1's and 2's complements.

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

Negative numbers in 5-bit 1's complement بە 5 بیتی
Number (5 bits)ژمارە (⁦5⁩ )Positive formشێوەی موجەبInvert every bitهەموو پێچەوانە بکەرەوە1's complement
−3-300011→11100
−10-1001010→10101

The handout reaches the same answer from the sign-magnitude form: −3-3 = 1 0011; keep the sign bit 1 and invert only the four magnitude bits → 1 1100. Both routes give the same bits.

پەڕەی دەستەکە لە شێوەی دەگاتە هەمان وەڵام: −3-3 = 1 0011؛ بە 1 بهێڵەوە و تەنها چوار پێچەوانە بکەرەوە ← 1 1100. هەردوو ڕێگاکە هەمان دەدەن.

To read a 1's complement number that starts with 1, invert it back: 10101 → 01010 = 10, so 10101 is −10-10.

بۆ خوێندنەوەی ژمارەیەکی کە بە 1 دەست پێدەکات، دیسان پێچەوانەی بکەرەوە: 10101 ← 01010 = ⁦10⁩، کەواتە 10101 بەهاکەی −10-10ـە.

Quick checkپشکنینی خێراWhat is −6-6 in 4-bit 1's complement?−6-6 بە 4 بیتی چییە؟

Way 2: 1's complementڕێگای 2: تەواوکەری یەک

Range of 1's complementسنووری بەهاکانی تەواوکەری یەک

1's complement has exactly the same range as sign-magnitude, and it also has two zeros: 0…0 (+0+0) and 1…1 (−0-0, the inverse of +0+0).

ڕێک هەمان سنووری بەهاکانی هەیە و ئەویش دوو سفری هەیە: 0…0 (+0+0) و 1…1 (−0-0، پێچەوانەی +0+0).

−(2n−1−1)  ≤  x  ≤  +(2n−1−1)-(2^{n-1}-1) \;\le\; x \;\le\; +(2^{n-1}-1)

For 4 bits: largest +(23−1)=+7+(2^{3}-1) = +7 = 0111; smallest −(23−1)=−7-(2^{3}-1) = -7 = 1000 (invert 0111).

بۆ 4 : گەورەترین +(23−1)=+7+(2^{3}-1) = +7 = 0111؛ بچووکترین −(23−1)=−7-(2^{3}-1) = -7 = 1000 (پێچەوانەی 0111).

Quick checkپشکنینی خێراWhat is the smallest (most negative) value of 4-bit 1's complement, and its bits?بچووکترین بەهای 4 بیتی و چین؟

Way 3: 2's complementڕێگای 3: تەواوکەری دوو

In 2's complement, positive numbers are again the same. To make a negative number: write the positive number, invert every bit (1's complement), then add 1.

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

−10-10 in 5-bit 2's complement−10-10 بە تەواوکەری دووی 5 بیتی
  1. Write the positive numberژمارە موجەبەکە بنووسە

    +10+10 in 5 bits is 01010.

    +10+10 بە 5 دەکاتە 01010.

  2. Invertپێچەوانە بکەرەوە

    Invert every bit: 10101 (this is −10-10 in 1's complement).

    هەموو پێچەوانە بکەرەوە: 10101 (ئەمە −10-10ـە بە ).

  3. Add 11 زیاد بکە

    Add 1: 10101 + 1 = 10110. So −10-10 is 10110 in 5-bit 2's complement.

    1 زیاد بکە: 10101 + 1 = 10110. کەواتە −10-10 بە 5 بیتی 10110ـە.

The handout starts from the sign-magnitude form 1 1010 and inverts only the magnitude bits → 1 0101, then adds 1 → 1 0110. The result is the same.

پەڕەی دەستەکە لە شێوەی 1 1010ـەوە دەست پێدەکات و تەنها پێچەوانە دەکاتەوە ← 1 0101، پاشان 1 زیاد دەکات ← 1 0110. ئەنجامەکە هەمانە.

Quick checkپشکنینی خێراWhat is −5-5 in 4-bit 2's complement?−5-5 بە 4 بیتی چییە؟

Way 3: 2's complementڕێگای 3: تەواوکەری دوو

Why does “invert and add 1” work?بۆچی «پێچەوانەکردنەوە و زیادکردنی 1» کار دەکات؟

In 2's complement, −1-1 is all ones (11111 in 5 bits), because −1+1-1 + 1 must give 0. Now look at −A=(−1−A)+1-A = (-1 - A) + 1. Taking AA away from all ones never needs a borrow: each 1−0=11 - 0 = 1 and each 1−1=01 - 1 = 0. So −1−A-1 - A is just AA with every bit inverted. Add 1 and you have −A-A.

لە −1-1 هەمووی 1ـە (11111 بە 5 )، چونکە −1+1-1 + 1 دەبێت سفر بدات. ئێستا سەیری −A=(−1−A)+1-A = (-1 - A) + 1 بکە. لێدەرکردنی AA لە ژمارەیەک کە هەمووی 1ـە هەرگیز ناوێت: هەر 1−0=11-0=1 و هەر 1−1=01-1=0. کەواتە −1−A-1-A هەمان AAـیە بە هەموو پێچەوانەکراوە. 1ـی بۆ زیاد بکە و −A-Aـت دەست دەکەوێت.

A taxi's distance meter rolling backعەدادی تەکسی کە بۆ دواوە دەگەڕێتەوە

Imagine a taxi's distance meter showing 00000 and the car is pushed back 1 km. The digits roll back to 99999. So "one less than zero" looks like all nines. In binary the same thing happens with all ones: one less than 0000 is 1111, which 2's complement reads as −1-1.

وای دابنێ عەدادی تەکسییەک ⁦00000⁩ پیشان دەدات و ئۆتۆمبێلەکە 1 کیلۆمەتر بۆ دواوە دەبرێت. ژمارەکان دەگەڕێنەوە بۆ ⁦99999⁩. کەواتە «یەک کەمتر لە سفر» وەک هەمووی نۆ دەردەکەوێت. لە هەمان شت بە هەمووی 1 ڕوو دەدات: یەک کەمتر لە 0000 دەکاتە 1111 کە بە −1-1 دەیخوێنێتەوە.

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

A real meter has ten digits per place and is not meant to go backwards. The idea that carries over is only this: when you count down past zero, you land on the largest pattern.

عەدادی ڕاستەقینە بۆ هەر خانەیەک دە هەیە و بۆ ئەوە دروست نەکراوە بەرەو دواوە بڕوات. ئەوەی لێرەدا دەمێنێتەوە تەنها ئەمەیە: کاتێک بە دابەزین لە سفر تێدەپەڕیت، دەگەیتە گەورەترین پێکهاتە.

Quick checkپشکنینی خێراWhat is −1-1 in 4-bit 2's complement?−1-1 بە 4 بیتی چییە؟

Way 3: 2's complementڕێگای 3: تەواوکەری دوو

The 2's complement wheelبازنەی تەواوکەری دوو

Put all 16 patterns of 4 bits on a wheel, in counting order. Adding 1 moves one step clockwise. Watch what happens after +7+7.

هەموو ⁦16⁩ پێکهاتەی 4 بە ڕیزی ژماردن لەسەر بازنەیەک دابنێ. زیادکردنی 1 یەک هەنگاو بە ئاراستەی میلی کاتژمێر دەتبات. سەیر بکە دوای +7+7 چی ڕوو دەدات.

A wheel with the 16 four-bit patterns. A marker starts at 0000 = 0, moves through 0101 = +5, 0110 = +6 and 0111 = +7, then one more step to 1000, where the value jumps to −8. It goes on to 1001 = −7 and 1111 = −1, and one more step brings it back to 0000 = 0.
A wheel with the 16 four-bit patterns. A marker starts at 0000 = 0, moves through 0101 = +5, 0110 = +6 and 0111 = +7, then one more step to 1000, where the value jumps to −8. It goes on to 1001 = −7 and 1111 = −1, and one more step brings it back to 0000 = 0.
Counting in 4-bit 2's complement: +7+1+7 + 1 wraps round to −8-8, and −1+1-1 + 1 comes back to 0.ژماردن بە 4 بیتی: دوای +7+7 دەگاتە −8-8 و دوای −1-1 دەگەڕێتەوە سەر 0.CêIT

The right half of the wheel (sign bit 0) holds 0 to +7+7. The left half (sign bit 1) holds −8-8 to −1-1. There is only one zero, so all 16 patterns are used, and the negative side gets one extra value:

نیوەی ڕاستی بازنەکە ( ⁦0) 0⁩ تا +7+7 هەڵدەگرێت. نیوەی چەپ ( ⁦1⁩) −8-8 تا −1-1. تەنها یەک سفر هەیە، بۆیە هەموو ⁦16⁩ پێکهاتەکە بەکار دێن و لای سالب یەک بەهای زیاتری بەر دەکەوێت:

−2n−1  ≤  x  ≤  +(2n−1−1)-2^{n-1} \;\le\; x \;\le\; +(2^{n-1}-1)

4 bits: −8-8 to +7+7. 8 bits: −128-128 to +127+127. 16 bits: −32768-32768 to +32767+32767.

4 : −8-8 تا +7+7. 8 : −128-128 تا +127+127. ⁦16⁩ : −32768-32768 تا +32767+32767.

Quick checkپشکنینی خێراWhat is the range of 6-bit 2's complement?سنووری بەهاکانی 6 بیتی چییە؟

Way 3: 2's complementڕێگای 3: تەواوکەری دوو

Reading a 2's complement number quicklyخوێندنەوەی خێرای ژمارەیەکی تەواوکەری دوو

There is a quick way to read any 2's complement number: give the sign bit a negative weight, −2n−1-2^{n-1}, and add the normal weights of the other 1 bits. In 8 bits the sign bit weighs −128-128.

ڕێگایەکی خێرا هەیە بۆ خوێندنەوەی هەر ژمارەیەکی : سالب بدە بە ، واتە −2n−1-2^{n-1}. پاشان ئاسایی تر کە 1ن بخە سەری. لە 8 −128-128 دەژمێردرێت.

The bits 11011001 under the weights −128, 64, 32, 16, 8, 4, 2, 1. The 1 bits give −128, 64, 16, 8 and 1, which add up to −128 + 89 = −39.بیتەکانی 11011001 لە ژێر بەها خانەییەکان -128، 64، 32، 16، 8، 4، 2، 1. بیتەکان کە 1ن -128، 64، 16، 8 و 1 دەدەن کە کۆیان -128 + 89 = -39ـە.
11011001 = −128+89=−39-128 + 89 = -39: the sign bit has the weight −128-128.11011001 = −128+89=−39-128 + 89 = -39: بە −128-128 دەژمێردرێت.CêIT

This also explains the wheel: 1000 in 4 bits is just the sign bit, −8-8; 1111 is −8+4+2+1=−1-8 + 4 + 2 + 1 = -1.

ئەمە بازنەکەش ڕوون دەکاتەوە: 1000 بە 4 تەنها ، واتە −8-8؛ 1111 دەکاتە −8+4+2+1=−1-8 + 4 + 2 + 1 = -1.

Quick checkپشکنینی خێراWhat is the value of the 8-bit 2's complement number 10110110?بەهای ژمارەی 10110110 بە 8 بیتی چەندە؟

Binary 2 - Two's Complement Representation of Negative NumbersComputer Science Lessons · Englishئینگلیزی · 133K viewsبینین · 2.3K likesلایک · Partبەشی 3:07–8:59 (ofلە 12:56)

Two's complement with a −128 sign weight, and the quick ways to make a negative number: invert and add 1.

کە تێیدا بەهای ⁦−128⁩ـی هەیە، لەگەڵ ڕێگا خێراکانی دروستکردنی : پێچەوانەکردنەوە و زیادکردنی 1.

Chapters 3:07-8:59: the sign bit weighs −128; checked a frame (−128 + 64 + 32 + 16 + 8).

بەشەکانی ⁦3:07⁩ تا ⁦8:59⁩: بەهای ⁦−128⁩ـی هەیە؛ لە ڤیدیۆکەدا: −128+64+32+16+8-128 + 64 + 32 + 16 + 8.

One number, three ways: −39-39یەک ژمارە بە سێ ڕێگا: −39-39

The lecture asks for −39-39 as an 8-bit number in all three forms. Here are all three, including the sign-magnitude form that the slide skips.

وانەکە داوای −39-39 دەکات وەک ژمارەیەکی 8 بیتی بە هەر سێ شێوەکە. ئەوە هەر سێکیانە، لەگەڵ شێوەی کە سلایدەکە بازی بەسەردا دەدات.

  1. Write +39+39 in 8 bits+39+39 بە 8 بیت بنووسە

    39=32+4+2+139 = 32 + 4 + 2 + 1, so +39+39 = 00100111.

    39=32+4+2+139 = 32 + 4 + 2 + 1، کەواتە +39+39 = 00100111.

  2. Sign-magnitudeنیشانە و نرخی ڕووت

    Change only the sign bit: 10100111.

    تەنها بگۆڕە: 10100111.

  3. 1's complementتەواوکەری یەک

    Invert every bit of 00100111: 11011000.

    هەموو 00100111 پێچەوانە بکەرەوە: 11011000.

  4. 2's complementتەواوکەری دوو

    Add 1 to the 1's complement: 11011000 + 1 = 11011001.

    1 بخە سەر : 11011000 + 1 = 11011001.

  5. Check with the −128-128 weightبە −128-128ـی بیتی نیشانە بیپشکنە

    −128+64+16+8+1=−128+89=−39-128 + 64 + 16 + 8 + 1 = -128 + 89 = -39. ✓

    −128+64+16+8+1=−128+89=−39-128 + 64 + 16 + 8 + 1 = -128 + 89 = -39. ✓

Quick checkپشکنینی خێراWhich form of −39-39 is 11011000?11011000 کام شێوەی −39-39ـە؟

All the ways side by sideهەموو ڕێگاکان لە تەنیشت یەکەوە

Here is every 4-bit pattern read in five ways. In the first four columns the top half (sign bit 0) is the same everywhere, and the bottom half shows the differences. The last column, excess-8, is one more signed code; the next screen explains it.

ئەمە هەموو پێکهاتەیەکی 4 بیتییە کە بە پێنج ڕێگا خوێندراوەتەوە. لە چوار ستوونی یەکەمدا نیوەی سەرەوە ( ⁦0⁩) لە هەموو شوێنێک وەک یەکە و نیوەی خوارەوە جیاوازییەکان پیشان دەدات. ستوونی کۆتایی، ⁦excess-8⁩، کۆدێکی نیشانەداری ترە کە شاشەی داهاتوو ڕوونی دەکاتەوە.

All 16 patterns of 4 bits, read five waysهەموو ⁦16⁩ پێکهاتەی 4 بە پێنج ڕێگا خوێندراونەتەوە
bitsUnsignedSign-magnitude1's complement2's complementExcess-8کۆدی ⁦excess-8⁩
00000000−8-8
00011+1+1+1+1+1+1−7-7
00102+2+2+2+2+2+2−6-6
00113+3+3+3+3+3+3−5-5
01004+4+4+4+4+4+4−4-4
01015+5+5+5+5+5+5−3-3
01106+6+6+6+6+6+6−2-2
01117+7+7+7+7+7+7−1-1
10008−0-0−7-7−8-80
10019−1-1−6-6−7-7+1+1
101010−2-2−5-5−6-6+2+2
101111−3-3−4-4−5-5+3+3
110012−4-4−3-3−4-4+4+4
110113−5-5−2-2−3-3+5+5
111014−6-6−1-1−2-2+6+6
111115−7-7−0-0−1-1+7+7
Rangesسنووری بەهاکان
FormشێوەRange with n bitsسنوورەکان بە n 8 bitsبە 8 Zerosسفرەکان
Unsigned00 … 2n−12^n-10 … 2551
Sign-magnitude−(2n−1−1)-(2^{n-1}-1) … +(2n−1−1)+(2^{n-1}-1)−127-127 … +127+1272
1's complement−(2n−1−1)-(2^{n-1}-1) … +(2n−1−1)+(2^{n-1}-1)−127-127 … +127+1272
2's complement−2n−1-2^{n-1} … +(2n−1−1)+(2^{n-1}-1)−128-128 … +127+1271
Excess-K (K=2n−1K = 2^{n-1})کۆدی ⁦excess-K⁩ (K=2n−1K = 2^{n-1})−2n−1-2^{n-1} … +(2n−1−1)+(2^{n-1}-1)−128-128 … +127+1271
Quick checkپشکنینی خێراWhy do modern computers use 2's complement?بۆچی ئەمڕۆ بەکار دەهێنن؟

All the ways side by sideهەموو ڕێگاکان لە تەنیشت یەکەوە

A fourth code: excess-Kکۆدێکی چوارەم: ⁦excess-K⁩

Excess-K (also called a biased or offset code) has no sign bit at all. To store a number, add a fixed number K, the bias, and write the result as an ordinary unsigned number. With 4 bits and K=8K = 8 (excess-8): −8+8=0-8 + 8 = 0 → 0000, 0+8=80 + 8 = 8 → 1000 and +7+8=15+7 + 8 = 15 → 1111. To read a pattern, take its unsigned value and subtract K: 0101 is 5−8=−35 - 8 = -3.

⁦Excess-K⁩ (کۆدی biased یان offsetیش پێی دەڵێن) هیچ نییە. بۆ هەڵگرتنی ژمارەیەک، ژمارەیەکی جێگیر ⁦(K)⁩ دەخەینە سەری و ئەنجامەکە وەک ئاسایی دەنووسین. بە 4 و K=8K = 8 ⁦(excess-8)⁩: −8+8=0-8 + 8 = 0 ← 0000، 0+8=80 + 8 = 8 ← 1000 و +7+8=15+7 + 8 = 15 ← 1111. بۆ خوێندنەوەی پێکهاتەیەک، بەها بێنیشانەکەی وەربگرە و K لێ دەربکە: 0101 دەکاتە 5−8=−35 - 8 = -3.

  • Numeric order: 0000 is the smallest number and 1111 the largest, so a circuit can compare two numbers exactly like unsigned numbers.
  • One zero (1000), like 2's complement. In fact excess-8 is 2's complement with the leftmost bit flipped: compare the last two columns.
  • Where it is used: the exponent of floating-point numbers. The IEEE 754 single-precision format stores the exponent with K=127K = 127, so an exponent of 0 is 01111111.
  • ڕیزی ژمارەکان: 0000 بچووکترین ژمارەیە و 1111 گەورەترین، بۆیە دەتوانێت دوو ژمارە ڕێک وەک بەراورد بکات.
  • یەک سفر (1000)، وەک . لە ڕاستیدا ⁦excess-8⁩ هەمان بەڵام لای چەپترین پێچەوانە کراوەتەوە: دوو ستوونی کۆتایی بەراورد بکە.
  • لە کوێ بەکار دێت: ژمارە ⁦floating-point⁩ـەکان. شێوازی ⁦single precision⁩ـی ⁦IEEE 754⁩ بە K=127K = 127 هەڵدەگرێت، بۆیە 0 دەبێتە 01111111.
Quick checkپشکنینی خێراIn 4-bit excess-8, which number is 0011?لە ⁦excess-8⁩ـی 4 بیتیدا 0011 کام ژمارەیە؟

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

ExerciseڕاهێنانDecimal value of sign-magnitude numbersژمارە بە نیشانە و نرخی ڕووت: بەهاکەیان بە دەیی

Find the decimal value of these 8-bit sign-magnitude numbers: (a) 10010101 (b) 01110111 (c) 00010111.

ئەم ژمارە 8 بیتییانە بە نووسراون. بەهاکەیان بە بدۆزەرەوە: (أ) 10010101 (ب) 01110111 (ج) 00010111.

Hintئاماژە

Split off the sign bit first.

سەرەتا جیا بکەرەوە.

Show answerوەڵامەکە پیشان بدە
  • (a) sign 1, magnitude 0010101 = 21 → −21-21
  • (b) sign 0, magnitude 1110111 = 119 → +119+119
  • (c) sign 0, magnitude 0010111 = 23 → +23+23
  • (أ) نیشانە 1، 0010101 = ⁦21⁩ ← −21-21
  • (ب) نیشانە 0، 1110111 = ⁦119⁩ ← +119+119
  • (ج) نیشانە 0، 0010111 = ⁦23⁩ ← +23+23
ExerciseڕاهێنانThree forms of −39-39, −19-19 and −25-25سێ شێوەکەی −39-39، −19-19 و −25-25

Write each number as an 8-bit number in sign-magnitude, 1's complement and 2's complement: −39-39, −19-19, −25-25.

هەر ژمارەیەک وەک ژمارەیەکی 8 بیتی بنووسە بە ، و : −39-39، −19-19، −25-25.

Show answerوەڵامەکە پیشان بدە
NumberژمارەPositiveموجەبSign-magnitude1's complement2's complement
−39-3900100111101001111101100011011001
−19-1900010011100100111110110011101101
−25-2500011001100110011110011011100111

Check −25-25: −128+64+32+4+2+1=−128+103=−25-128 + 64 + 32 + 4 + 2 + 1 = -128 + 103 = -25. Check −19-19: −128+64+32+8+4+1=−128+109=−19-128 + 64 + 32 + 8 + 4 + 1 = -128 + 109 = -19.

پشکنینی −25-25: −128+64+32+4+2+1=−128+103=−25-128 + 64 + 32 + 4 + 2 + 1 = -128 + 103 = -25. پشکنینی −19-19: −128+64+32+8+4+1=−128+109=−19-128 + 64 + 32 + 8 + 4 + 1 = -128 + 109 = -19.

ExerciseڕاهێنانOne pattern, four readingsیەک پێکهاتە، چوار خوێندنەوە

Read the 8-bit pattern 10110110 as an unsigned number, in sign-magnitude, in 1's complement and in 2's complement.

پێکهاتەی 8 بیتیی 10110110 بخوێنەرەوە وەک ، بە ، بە و بە .

Show answerوەڵامەکە پیشان بدە
  • Unsigned: 128+32+16+4+2128 + 32 + 16 + 4 + 2 = 182
  • Sign-magnitude: sign 1, magnitude 0110110 = 54 → −54-54
  • 1's complement: invert → 01001001 = 73 → −73-73
  • 2's complement: −128+54-128 + 54 = −74-74
  • : 128+32+16+4+2128 + 32 + 16 + 4 + 2 = ⁦182⁩
  • : نیشانە 1، 0110110 = ⁦54⁩ ← −54-54
  • : پێچەوانە ← 01001001 = ⁦73⁩ ← −73-73
  • : −128+54-128 + 54 = −74-74
ExerciseڕاهێنانSix bitsشەش بیت

(a) Write −13-13 in 6-bit sign-magnitude, 1's complement and 2's complement. (b) Give the range of each form for 6 bits.

(أ) −13-13 بە ، و 6 بیتی بنووسە. (ب) سنووری بەهاکانی هەر شێوەیەک بۆ 6 بنووسە.

Show answerوەڵامەکە پیشان بدە
  • (a) +13+13 = 001101; sign-magnitude 101101; 1's complement 110010; 2's complement 110011 (−32+16+2+1=−13-32 + 16 + 2 + 1 = -13).
  • (b) Sign-magnitude and 1's complement: −31-31 to +31+31. 2's complement: −32-32 to +31+31. (Unsigned: 0 to 63.)
  • (أ) +13+13 = 001101؛ 101101؛ 110010؛ 110011 (−32+16+2+1=−13-32 + 16 + 2 + 1 = -13).
  • (ب) و : −31-31 تا +31+31. : −32-32 تا +31+31. (: 0 تا ⁦63.⁩)
ExerciseڕاهێنانChoosing a codeهەڵبژاردنی کۆد

A lab experiment gives whole numbers from −5-5 to +6+6. (a) Are 4 bits enough? (b) Which 4-bit codes of this lesson can hold every result? (c) Which one would you choose, and why? (d) Write −5-5 and +6+6 in your code.

ئەزموونێکی تاقیگە لە −5-5 تا +6+6 دەدات. (أ) ئایا 4 بەسە؟ (ب) کام کۆدی 4 بیتیی ئەم وانەیە دەتوانێت هەموو ئەنجامەکان هەڵبگرێت؟ (ج) کامیان هەڵدەبژێریت و بۆچی؟ (د) −5-5 و +6+6 بە کۆدەکەی خۆت بنووسە.

Hintئاماژە

Count the values first, then compare with each code's range.

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

Show answerوەڵامەکە پیشان بدە
  • (a) Yes: −5-5 … +6+6 is 12 values, and 4 bits give 16 patterns.
  • (b) Sign-magnitude and 1's complement (−7-7 … +7+7), 2's complement and excess-8 (−8-8 … +7+7). Unsigned numbers cannot hold the negative results.
  • (c) 2's complement: one zero, and the same adder works for every sum. Excess-8 is a good second choice when the results must be compared or sorted, because its patterns are in numeric order.
  • (d) 2's complement: −5-5 = 1011, +6+6 = 0110. Excess-8: −5+8=3-5 + 8 = 3 → 0011, +6+8=14+6 + 8 = 14 → 1110. (A custom excess-5 code would even map −5-5 … +6+6 to 0000 … 1011.)
  • (أ) بەڵێ: −5-5 … +6+6 دەکاتە ⁦12⁩ بەها و 4 ⁦16⁩ پێکهاتە دەدەن.
  • (ب) و (−7-7 … +7+7)، و ⁦excess-8⁩ (−8-8 … +7+7). ناتوانن ئەنجامە سالبەکان هەڵبگرن.
  • (ج) : یەک سفری هەیە و هەمان بۆ هەموو کۆکردنەوەیەک کار دەکات. ئەگەر ئەنجامەکان دەبێت بەراورد یان ڕیز بکرێن، ⁦excess-8⁩ هەڵبژاردنێکی باشی دووەمە، چونکە پێکهاتەکانی بە ڕیزی ژمارەکانن.
  • (د) : −5-5 = 1011، +6+6 = 0110. ⁦Excess-8⁩: −5+8=3-5 + 8 = 3 ← 0011، +6+8=14+6 + 8 = 14 ← 1110. (کۆدێکی ⁦excess-5⁩ـی تایبەت تەنانەت −5-5 … +6+6 دەگۆڕێت بۆ 0000 … 1011.)
Quick checkپشکنینی خێرا+10+10 in 5 bits is 01010. What is it in 1's complement?+10+10 بە 5 01010ـە. بە چییە؟

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

MistakeهەڵەFixچارەسەر
Inverting a number that is not yet n bits long: +5+5 as 101 → 010.پێچەوانەکردنەوەی ژمارەیەک پێش ئەوەی n بێت: +5+5 وەک 101 ← 010.Pad to the full width first: 00000101 → 11111010 → 11111011.سەرەتا تا پانیی تەواو سفر زیاد بکە: 00000101 ← 11111010 ← 11111011.
Forgetting the +1+1, so the answer is the 1's complement.لەبیرکردنی +1+1، بۆیە وەڵامەکە .2's complement = invert and add 1. Check with the −128-128 weight. = پێچەوانەکردنەوە و زیادکردنی 1. بە −128-128ـی بیپشکنە.
Reading 11011001 as −89-89 (as if it were sign-magnitude).خوێندنەوەی 11011001 وەک −89-89 (وەک ئەوەی بێت).In 2's complement the sign bit weighs −128-128: −128+89=−39-128 + 89 = -39.لە −128-128 دەژمێردرێت: −128+89=−39-128 + 89 = -39.
Using the sign-magnitude range for 2's complement (−127-127 … +127+127).بەکارهێنانی سنووری بۆ (−127-127 … +127+127).2's complement has one more negative value: −128-128 … +127+127. یەک بەهای سالبی زیاترە: −128-128 … +127+127.
Thinking 1000 is −0-0 in 2's complement.وا بزانیت 1000 لە −0-0ـە.2's complement has only one zero; 1000 is −8-8 in 4 bits. تەنها یەک سفری هەیە؛ 1000 بە 4 −8-8ـە.
Changing a positive number when converting it.دەستکاریکردنی کاتێک بۆ شێوەیەکی نیشانەدار دەیگۆڕیت.Positive numbers are the same in all three forms; only negatives change. لە هەر سێ شێوەکەدا وەک یەکن؛ تەنها سالبەکان دەگۆڕێن.

Summaryپوختە

  • n bits give 2n2^n patterns; unsigned numbers use them for 0 … 2n−12^n-1.
  • Signed numbers use the leftmost bit as the sign bit: 0 = positive, 1 = negative.
  • Positive numbers are the same in all three forms.
  • Sign-magnitude: change only the sign bit. Range ±(2n−1−1)\pm(2^{n-1}-1), two zeros.
  • 1's complement: invert every bit. Same range, two zeros.
  • 2's complement: invert every bit, then add 1. Range −2n−1-2^{n-1} … 2n−1−12^{n-1}-1, one zero. The sign bit weighs −2n−1-2^{n-1}.
  • Excess-K: add a bias K and store the result unsigned. One zero, patterns in numeric order; used for floating-point exponents.
  • Computers use 2's complement because one adder then handles every case.
  • n 2n2^n پێکهاتە دەدەن؛ بۆ 0 … 2n−12^n-1 بەکاریان دەهێنن.
  • لای چەپترین وەک بەکار دەهێنن: 0 = موجەب، 1 = سالب.
  • لە هەر سێ شێوەکەدا وەک یەکن.
  • : تەنها بگۆڕە. سنوورەکان ±(2n−1−1)\pm(2^{n-1}-1)، دوو سفر.
  • : هەموو پێچەوانە بکەرەوە. هەمان سنوور، دوو سفر.
  • : هەموو پێچەوانە بکەرەوە، پاشان 1 زیاد بکە. سنوورەکان −2n−1-2^{n-1} … 2n−1−12^{n-1}-1، یەک سفر. بە −2n−1-2^{n-1} دەژمێردرێت.
  • ⁦Excess-K⁩: ژمارەیەکی جێگیر ⁦(K)⁩ زیاد بکە و ئەنجامەکە وەک هەڵبگرە. یەک سفری هەیە و پێکهاتەکانی بە ڕیزی ژمارەکانن؛ بۆ ژمارە ⁦floating-point⁩ـەکان بەکار دێت.
  • بەکار دەهێنن چونکە ئەو کاتە یەک هەموو حاڵەتەکان دەکات.
Review questionsپرسیارەکانی پێداچوونەوە
What is −1-1 in 8-bit 2's complement?−1-1 بە تەواوکەری دووی 8 بیتی چییە؟

11111111 (all ones): −128+127=−1-128 + 127 = -1.

11111111 (هەمووی ⁦1⁩): −128+127=−1-128 + 127 = -1.

Why does sign-magnitude have two zeros?بۆچی نیشانە و نرخی ڕووت دوو سفری هەیە؟

Because the magnitude 0 can carry either sign: 0000… is +0+0 and 1000… is −0-0.

چونکە 0 دەتوانێت هەر دوو نیشانەکە هەڵبگرێت: 0000… +0+0ـە و 1000… −0-0ـە.

How many values can 8-bit 2's complement hold?تەواوکەری دووی 8 بیتی چەند بەها هەڵدەگرێت؟

256: from −128-128 to +127+127, including one zero.

⁦256⁩: لە −128-128 تا +127+127، لەگەڵ یەک سفر.

Videoڤیدیۆ

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

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

Twos complement: Negative numbers in binaryBen Eater · Englishئینگلیزی · 1.4M viewsبینین · 29K likesلایک

Three ways to store negative numbers — sign bit, 1's complement, 2's complement — and why 2's complement wins.

سێ ڕێگای هەڵگرتنی (، و ) و بۆچی لە هەمووان باشترە.

Chapters: 0:26 sign bit, 2:58 problems with the sign bit, 4:20 one's complement, 9:39 two's complement.

بەشەکان: ⁦0:26⁩ ، ⁦2:58⁩ گرفتەکانی ، ⁦4:20⁩ ، ⁦9:39⁩ .

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

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

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

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

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

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

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