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

1's and 2's Complementsتەواوکەری یەک و تەواوکەری دوو

Flip every bit for the 1's complement, add 1 for the 2's complement, and use the copy-up-to-the-first-1 shortcut. See why complements let a computer write negative numbers and subtract by adding.

بۆ هەموو پێچەوانە بکەرەوە، بۆ 1 زیاد بکە و ڕێگا کورتەکەی «تا یەکەم 1 کۆپی بکە» بەکاربهێنە. ببینە بۆچی تەواوکەرەکان ڕێگە بە دەدەن بنووسێت و بە کۆکردنەوە لێدەربکات.

11 min read12 خولەک خوێندنەوە · 12 stepsهەنگاو

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

  • Subtract decimal numbers by adding the 9's complement, and see that the 1's and 2's complements are the same idea in base 2.
  • Find the 1's complement of a binary number of any length.
  • Find the 2's complement by flipping and adding 1, and with the shortcut.
  • Explain the complements as (2n−1)−x(2^n - 1) - x and 2n−x2^n - x and check answers with them.
  • Explain why the 2's complement represents −x-x and turns subtraction into addition.
  • ژمارەی بە کۆکردنەوەی تەواوکەری نۆ لێ دەربکەیت و ببینیت کە و هەمان بیرۆکەن لە 2دا.
  • ژمارەیەکی بە هەر درێژییەک بدۆزیتەوە.
  • بە پێچەوانەکردنەوە و زیادکردنی 1 و بە ڕێگا کورتەکە بدۆزیتەوە.
  • تەواوکەرەکان وەک (2n−1)−x(2^n - 1) - x و 2n−x2^n - x ڕوون بکەیتەوە و وەڵامەکانی پێ بپشکنیت.
  • ڕوونی بکەیتەوە بۆچی جێگەی −x-x دەگرێتەوە و لێدەرکردن دەکاتە کۆکردنەوە.

Why complements?بۆچی تەواوکەرەکان؟

The binary system has base r=2r = 2. Every base rr has two complements, the (r−1)(r - 1)'s complement and the rr's complement, so binary has two: the 1's complement and the 2's complement.

سیستەمی r=2r = 2ـە. هەر rr دوو تەواوکەری هەیە: تەواوکەری (r−1)(r - 1) و تەواوکەری rr. بۆیە دوو تەواوکەری هەیە: و .

They matter because they let a computer represent negative numbers with nothing but bits, and because they turn subtraction into addition, so one adder circuit can do both jobs.

گرنگن چونکە ڕێگە بە دەدەن پیشان بدات تەنها بە ، هەروەها لێدەرکردن دەکەنە کۆکردنەوە، بۆیە یەک دەتوانێت هەردوو کارەکە بکات.

A taxi's kilometre counter running backwardsکیلۆمەترژمێری تەکسی کە بەرەو دواوە دەڕوات

Imagine the 6-digit kilometre counter of a taxi could run backwards. One kilometre back from 000000 it would show 999999. That 999999 behaves like −1-1: add 1 and you are back at 000000. A 4-bit binary counter does the same: one step back from 0000 shows 1111, and 1111 is exactly the 4-bit 2's complement of 0001. That is how a computer writes −1-1.

وای دابنێ کیلۆمەترژمێری 6 ژمارەیی تەکسییەک بتوانێت بەرەو دواوە بڕوات. یەک کیلۆمەتر بەرەو دواوە لە ⁦000000⁩ـەوە ⁦999999⁩ پیشان دەدات. ئەو ⁦999999⁩ـە وەک −1-1 ڕەفتار دەکات: 1ـی بخەرە سەر و دەگەڕێیتەوە بۆ ⁦000000⁩. بە 4 هەمان کار دەکات: یەک هەنگاو بەرەو دواوە لە ⁦0000⁩ـەوە ⁦1111⁩ پیشان دەدات و ⁦1111⁩ ڕێک 4 ⁦0001⁩ـە. بەم شێوەیە −1-1 دەنووسێت.

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

A real counter never runs backwards and does not store negative distances. A computer uses this wrap-around on purpose, and only inside a fixed number of bits.

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

Quick checkپشکنینی خێراWhy are the 1's and 2's complements important?بۆچی و گرنگن؟

The same trick in decimal: 9's and 10's complementsهەمان فێڵ لە دەییدا: تەواوکەری نۆ و تەواوکەری دە

Complements are not a computer invention: every base has them, and in decimal you can try them with pencil and paper. The 9's complement of a decimal number replaces every digit d with 9−d9 - d. For 287 that gives 712, because 287+712=999287 + 712 = 999. The 10's complement is one more: 712+1=713712 + 1 = 713, because 287+713=1000287 + 713 = 1000.

تەواوکەرەکان داهێنانی نین: هەموو تەواوکەری هەیە و لە دەتوانیت بە قەڵەم و کاغەز تاقییان بکەیتەوە. بۆ تەواوکەری نۆ، هەر d لە ژمارەکەدا بکە بە 9−d9 - d. بۆ ⁦287⁩ ئەمە ⁦712⁩ دەدات، چونکە 287+712=999287 + 712 = 999. تەواوکەری دە یەکێک زیاترە: 712+1=713712 + 1 = 713، چونکە 287+713=1000287 + 713 = 1000.

Now the trick: subtract by adding. To work out 652−287652 - 287, add the 9's complement of 287 and one more, then drop the leading 1:

ئێستا فێڵەکە: بە کۆکردنەوە لێی دەربکە. بۆ دۆزینەوەی 652−287652 - 287، تەواوکەری نۆی ⁦287⁩ و یەکێکی تر بخەرە سەر ⁦652⁩، پاشان 1ـی پێشەوە لاببە:

652+712+1=13651365−1000=365652−287=365  ✓\begin{aligned} 652 + 712 + 1 &= 1365 \\ 1365 - 1000 &= 365 \\ 652 - 287 &= 365 \;\checkmark \end{aligned}

Why it works: adding 712+1=713712 + 1 = 713 is the same as adding 1000−2871000 - 287. So the sum is 1000 too big, and dropping the leading 1 takes that 1000 away again.

بۆچی کار دەکات: زیادکردنی 712+1=713712 + 1 = 713 وەک زیادکردنی 1000−2871000 - 287 وایە. کەواتە کۆکە ⁦1000⁩ زیاترە و لابردنی 1ـی پێشەوە ئەو ⁦1000⁩ـە لادەباتەوە.

Some decimal codes make the 9's complement easy for a machine: in Excess-3 you only invert the bits (Why Excess-3?).

هەندێک کۆدی تەواوکەری نۆ بۆ ئامێر ئاسان دەکەن: لە ⁦Excess-3⁩دا تەنها پێچەوانە دەکەیتەوە (بۆچی ⁦Excess-3⁩؟).

Quick checkپشکنینی خێراWhat is the 9's complement of 4073?تەواوکەری نۆی ⁦4073⁩ چییە؟

Shorter numbers, and back to binaryژمارەی کورتتر و گەڕانەوە بۆ دووانی

If the number you subtract is shorter, first pad it with zeros to the same length. The zeros become 9s in the complement. For 5024−685024 - 68: 68 becomes 0068, its 9's complement is 9931, and 5024+9931+1=149565024 + 9931 + 1 = 14956. Drop the leading 1: 4956 ✓.

ئەگەر ئەو ژمارەیەی لێی دەردەکەیت کورتتر بوو، سەرەتا بە سفر درێژی بکەرەوە تا هەمان درێژی هەبێت. ئەو سفرانە لە تەواوکەرەکەدا دەبنە 9. بۆ 5024−685024 - 68: ⁦68⁩ دەبێتە ⁦0068⁩، تەواوکەری نۆی ⁦9931⁩ـە و 5024+9931+1=149565024 + 9931 + 1 = 14956. 1ـی پێشەوە لاببە: ⁦4956⁩ ✓.

The general rule for an n-digit number x in base r:

یاسا گشتییەکە بۆ ژمارەیەکی x کە n هەیە لە r:

(rn−1)−xrn−x(r^n - 1) - x \qquad\qquad r^n - x

Left: the (r−1)(r - 1)'s complement (each digit becomes (r−1)−d(r - 1) - d). Right: the rr's complement, one more. With r=10r = 10 these are the 9's and 10's complements; with r=2r = 2 they are the 1's and 2's complements: each bit becomes 1−d1 - d, which simply flips it, and the 2's complement is that plus 1.

چەپ: تەواوکەری (r−1)(r - 1) (هەر دەبێتە (r−1)−d(r - 1) - d). ڕاست: تەواوکەری rr، کە یەکێک زیاترە. بە r=10r = 10 ئەمانە تەواوکەری نۆ و تەواوکەری دەن؛ بە r=2r = 2 دەبنە و : هەر دەبێتە 1−d1 - d، واتە تەنها پێچەوانە دەبێتەوە و ئەوە + 1ـە.

Quick checkپشکنینی خێراWhich addition gives 803−45803 - 45 with the 9's complement?کام کۆکردنەوە 803−45803 - 45 بە تەواوکەری نۆ دەدات؟

The 1's complement: flip every bitتەواوکەری یەک: هەموو بیتێک پێچەوانە بکەرەوە

Definitionپێناسە

The binary number you get by changing every 0 to 1 and every 1 to 0. In 1's-complement form, a negative number is the 1's complement of the positive one.

ئەو ژمارەیەیە کە دەستت دەکەوێت کاتێک لە ژمارەیەکی هەموو 0ـەکان دەکەیتە 1 و هەموو 1ـەکان دەکەیتە 0. لە شێوازی تەواوکەری یەکدا ژمارەیەکی سالب تەواوکەری یەکی ژمارە موجەبەکەیە.

To find the 1's complement, change every 1 to 0 and every 0 to 1. This is called taking the complement, or simply the 1's complement. Keep the same number of bits: leading zeros become 1s too.

بۆ دۆزینەوەی ، هەموو 1ـێک بکە بە 0 و هەموو 0ـێک بکە بە 1. ئەمە پێی دەوترێت وەرگرتنی تەواوکەر، یان تەنها . هەمان ژمارەی بهێڵەوە: سفرەکانی پێشەوەش دەبنە 1.

10101↓↓↓↓↓0101010101  →  01010\begin{array}{ccccc} 1 & 0 & 1 & 0 & 1 \\ \downarrow & \downarrow & \downarrow & \downarrow & \downarrow \\ 0 & 1 & 0 & 1 & 0 \end{array}\qquad 10101 \;\to\; 01010

The lecture's example: every bit is flipped.

نموونەکەی وانەکە: هەموو پێچەوانە کراوەتەوە.

Why is it called a complement? Subtract the number from a row of 1s of the same length: 11111−10101=0101011111 - 10101 = 01010 (31−21=1031 - 21 = 10). Subtracting from 1s never needs a borrow, so every bit simply flips. In general the 1's complement of an n-bit number x is (2n−1)−x(2^n - 1) - x. In hardware it is one NOT gate per bit.

بۆچی پێی دەوترێت تەواوکەر؟ ژمارەکە لە ڕیزێکی 1 بە هەمان درێژی لێ دەربکە: 11111−10101=0101011111 - 10101 = 01010 (31−21=1031 - 21 = 10). لێدەرکردن لە 1ـەکان هەرگیز پێویست نییە، بۆیە هەموو تەنها پێچەوانە دەبێتەوە. بە گشتی ژمارەیەکی n x یەکسانە بە (2n−1)−x(2^n - 1) - x. لە بۆ هەر یەک گەیتی NOTـە.

Quick checkپشکنینی خێراWhat is the 1's complement of 0011000110? 0011000110 چییە؟

The 2's complement: flip, then add 1تەواوکەری دوو: پێچەوانە بکەرەوە، پاشان 1 زیاد بکە

Definitionپێناسە

The 1's complement of a binary number plus 1. Computers store negative integers in 2's complement because the same adder then also does subtraction.

بریتییە لە ژمارەیەکی زیاد 1. بە تەواوکەری دوو هەڵدەگرن، چونکە بەم شێوەیە هەمان لێدەرکردنیش دەکات.

The 2's complement of a binary number is its 1's complement plus 1, added at the least significant bit (the right end).

ژمارەیەکی یەکسانە بە هەمان ژمارە + 1، کە دەخرێتە سەر (سەری ڕاست).

Bit boxes. The given number 10101 (21). Arrows flip every bit to give the 1's complement 01010 (10, which is 31 − 21). Then 1 is added at the right end, giving the 2's complement 01011 (11, which is 32 − 21).خانەی بیتەکان. ژمارەی دراو 10101 (21). تیرەکان هەموو بیتێک پێچەوانە دەکەنەوە و تەواوکەری یەک 01010 دەدەن (10، واتە 31 - 21). پاشان 1 دەخرێتە سەر لای ڕاست و تەواوکەری دوو 01011 دەدات (11، واتە 32 - 21).
The lecture's example 10101: flip every bit, then add 1.نموونەکەی وانەکە ⁦10101⁩: هەموو پێچەوانە بکەرەوە، پاشان 1 زیاد بکە.CêIT

The values tell you what the 2's complement really is: 10101=2110101 = 21 and 01011=1101011 = 11, and 21+11=32=2521 + 11 = 32 = 2^5. The 2's complement of an n-bit number x is 2n−x2^n - x, the amount that is still missing to reach 2n2^n.

بەهاکان پیشانی دەدەن لە ڕاستیدا چییە: 10101=2110101 = 21 و 01011=1101011 = 11 و 21+11=32=2521 + 11 = 32 = 2^5. ژمارەیەکی n x یەکسانە بە 2n−x2^n - x، ئەو بڕەی هێشتا ماوە تا بگاتە 2n2^n.

Quick checkپشکنینی خێراWhat is the 2's complement of 10011001? 10011001 چییە؟

Binary 2 - Two's Complement Representation of Negative NumbersComputer Science Lessons · Englishئینگلیزی · 133K viewsبینین · 2.3K likesلایک · Partبەشی 8:59–11:41 (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.

Chapter 8:59 "Alternative conversion methods": +6 → invert → add 1 (checked a frame).

بەشی ⁦8:59⁩، Alternative conversion methods: +6+6 ← پێچەوانەکردنەوە ← زیادکردنی 1 (لە ڤیدیۆکەدا پشکنراوە).

When the +1 ripplesکاتێک ⁦+1⁩ دەڕوات

Adding 1 is a normal binary addition, so it can make carries. If the 1's complement ends in 1s, the +1 turns them into 0s and carries on until it meets the first 0. Watch the 2's complement of 101100 (44):

زیادکردنی 1 کۆکردنەوەیەکی ئاسایی ، بۆیە دەتوانێت دروست بکات. ئەگەر بە 1 کۆتایی بێت، ⁦+1⁩ ئەو 1ـانە دەکاتە 0 و بەردەوام دەبێت تا دەگاتە یەکەم 0. سەیری ⁦101100 (44)⁩ بکە:

Animation. The number 101100 (44) is shown in boxes. Its bits are flipped two at a time, giving the 1's complement 010011 (19). Then 1 is added at the right end: 1 + 1 = 10, write 0 and carry 1; again 1 + 1 = 10, write 0 and carry 1; then 0 + 1 = 1 and the carry stops. The other bits come down unchanged. The 2's complement is 010100 (20 = 64 − 44). The last frame marks the right part 100, which is the same in the number and in its 2's complement.
Animation. The number 101100 (44) is shown in boxes. Its bits are flipped two at a time, giving the 1's complement 010011 (19). Then 1 is added at the right end: 1 + 1 = 10, write 0 and carry 1; again 1 + 1 = 10, write 0 and carry 1; then 0 + 1 = 1 and the carry stops. The other bits come down unchanged. The 2's complement is 010100 (20 = 64 − 44). The last frame marks the right part 100, which is the same in the number and in its 2's complement.
Flip every bit, then add 1: the carry runs through the 1s at the right end and stops at the first 0.هەموو پێچەوانە بکەرەوە، پاشان 1 زیاد بکە: بە ناو 1ـەکانی سەری ڕاستدا دەڕوات و لە یەکەم 0دا دەوەستێت.CêIT
  • Keep the number of bits. A 6-bit number has a 6-bit complement.
  • Drop a carry out of the leftmost bit. For 00000: 1's complement 11111, and 11111+1=10000011111 + 1 = 100000; keep 5 bits: 00000. The 2's complement of 0 is 0.
  • ژمارەی بهێڵەوە. ژمارەیەکی 6 تەواوکەرێکی 6 هەیە.
  • کە لە چەپترین دەردەچێت فڕێ بدە. بۆ ⁦00000⁩: ⁦11111⁩ـە و 11111+1=10000011111 + 1 = 100000؛ 5 بهێڵەوە: ⁦00000⁩. 0 خۆی 0ـە.
Quick checkپشکنینی خێراWhat is the 2's complement of 1010010100? 1010010100 چییە؟

A shortcut: copy up to the first 1ڕێگایەکی کورت: تا یەکەم 1 کۆپی بکە

The GIF shows a pattern: the right part of the number, up to and including its first 1, came out unchanged. That gives a faster method with no addition at all:

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

  1. Start at the right end. Copy every bit up to and including the first 1.

    لە سەری ڕاستەوە دەست پێبکە. هەموو کۆپی بکە تا یەکەم 1 و ئەویش لەگەڵیدا.

  2. Flip every bit to the left of that 1.

    هەموو لە لای چەپی ئەو 1ـە پێچەوانە بکەرەوە.

10110‾ 100‾  →  01001‾ 100‾=01001100\overline{10110}\,\underline{100} \;\to\; \overline{01001}\,\underline{100} = 01001100

The 2's complement of 10110100 (180) is 01001100 (76), and 180+76=256=28180 + 76 = 256 = 2^8. The underlined part 100 is copied; the overlined part is flipped.

⁦10110100 (180)⁩ یەکسانە بە ⁦01001100 (76)⁩ و 180+76=256=28180 + 76 = 256 = 2^8. بەشی ⁦100⁩ کە هێڵی لە ژێرە کۆپی دەکرێت؛ ئەو بەشەی هێڵی لە سەرە پێچەوانە دەکرێتەوە.

Why does it work? The zeros at the right end of the number become 1s in the 1's complement, and the +1 turns them all back into 0s with a carry. The carry stops at the first 0 of the 1's complement, which was the first 1 of the number, and makes it 1 again. Everything further left stays flipped.

بۆچی کار دەکات؟ سفرەکانی سەری ڕاستی ژمارەکە لە دەبنە 1 و ⁦+1⁩ هەموویان بە دەکاتەوە 0. لە یەکەم 0ـی دەوەستێت (کە یەکەم 1ـی ژمارەکە بوو) و دەیکاتەوە 1. هەموو ئەوانەی لە لای چەپترن پێچەوانە دەمێننەوە.

Quick checkپشکنینی خێراUse the shortcut: what is the 2's complement of 0110100001101000?ڕێگا کورتەکە بەکاربهێنە: 0110100001101000 چییە؟

How to: Twos ComplementEric Valero · Englishئینگلیزی · 304K viewsبینین · 5.2K likesلایک

The fast way to a 2's complement: keep the bits up to the first 1 from the right, then flip the rest.

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

Pen and paper: 20 = 00010100 → −20 by "copy to the first 1, flip the rest" (checked frames).

بە قەڵەم و کاغەز: 20=0001010020 = 00010100 دەکاتە ⁦−20⁩ بە «تا یەکەم 1 کۆپی بکە، ئەوانی تر پێچەوانە بکەرەوە» (لە وێنەکانی ڤیدیۆکەوە پشکنراوە).

The 2's complement as a negative numberتەواوکەری دوو وەک ژمارەی سالب

In a fixed number of bits, the 2's complement of x behaves exactly like −x-x. With 8 bits, 00000101 is +5+5 and its 2's complement 11111011 stands for −5-5. Add them and the 8 bits give 0, because the carry out of the eighth bit is dropped:

لە ژمارەیەکی جێگیری ، x ڕێک وەک −x-x ڕەفتار دەکات. بە 8 ، ⁦00000101⁩ یەکسانە بە +5+5 و ئەو ژمارەیە، ⁦11111011⁩، جێگەی −5-5 دەگرێتەوە. کۆیان بکەرەوە و 8 0 دەدەن، چونکە ئەو لە هەشتەم دەردەچێت فڕێ دەدرێت:

1111111100000101+11111011100000000\begin{array}{cccccccccc} & {\scriptstyle 1} & {\scriptstyle 1} & {\scriptstyle 1} & {\scriptstyle 1} & {\scriptstyle 1} & {\scriptstyle 1} & {\scriptstyle 1} & {\scriptstyle 1} & \\ & & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 \\ + & & 1 & 1 & 1 & 1 & 1 & 0 & 1 & 1 \\ \hline & \cancel{1} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{array}

5+(−5)5 + (-5): the crossed-out 1 is the ninth bit, which does not fit in 8 bits.

5+(−5)5 + (-5): 1ـە خەتلێدراوەکە نۆیەمە، کە لە 8 جێی نابێتەوە.

Taking the 2's complement twice gives the number back (10101→01011→1010110101 \to 01011 \to 10101), just as −(−x)=x-(-x) = x. And subtraction becomes addition: A−BA - B = AA + (the 2's complement of BB). Here is 1101−01101101 - 0110 from the previous lesson, done with an adder only:

دوو جار وەرگرتنی ژمارەکە دەگەڕێنێتەوە (10101→01011→1010110101 \to 01011 \to 10101)، ڕێک وەک −(−x)=x-(-x) = x. هەروەها لێدەرکردن دەبێتە کۆکردنەوە: A−BA - B یەکسانە بە AA + BB. ئەمە 1101−01101101 - 0110ـە لە وانەی پێشووـەوە، تەنها بە کۆکردنەوە:

11101+101010111\begin{array}{cccccc} & {\scriptstyle 1} & & & & \\ & & 1 & 1 & 0 & 1 \\ + & & 1 & 0 & 1 & 0 \\ \hline & \cancel{1} & 0 & 1 & 1 & 1 \end{array}

The 2's complement of 0110 is 1010. 1101+1010=101111101 + 1010 = 10111; drop the fifth bit: 0111=70111 = 7, the same answer as 13−613 - 6 with borrows.

⁦0110⁩ یەکسانە بە ⁦1010⁩. 1101+1010=101111101 + 1010 = 10111؛ پێنجەم فڕێ بدە: 0111=70111 = 7، هەمان وەڵامی 13−613 - 6 بە .

How computers store signed numbers with these ideas, and how signed addition and subtraction work in full, are the topics of Signed numbers and Signed arithmetic.

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

Quick checkپشکنینی خێراIn 8 bits, 00000101 is +5+5. Which pattern stands for −5-5?بە 8 ، ⁦00000101⁩ یەکسانە بە +5+5. کام شێوە جێگەی −5-5 دەگرێتەوە؟

1's or 2's complement?تەواوکەری یەک یان تەواوکەری دوو؟

Propertyتایبەتمەندی1's complement2's complement
How to form itچۆن دروست دەکرێتFlip every bitهەموو پێچەوانە بکەرەوەFlip every bit, then add 1 (or: copy up to the first 1, flip the rest)هەموو پێچەوانە بکەرەوە، پاشان 1 زیاد بکە (یان: تا یەکەم 1 کۆپی بکە و ئەوی تر پێچەوانە بکەرەوە)
Value for an n-bit xبەها بۆ xـی n (2n−1)−x(2^n - 1) - x2n−x2^n - x
Example 10101 (21)نموونە ⁦10101 (21)⁩01010=1001010 = 1001011=1101011 = 11
Used todayئەمڕۆ لە کوێ بەکاردێتThe Internet checksum of IP, TCP and UDP; some older computersپشکنینی کۆی (checksum) IP، TCP و UDP؛ هەندێک کۆنSigned integers in practically all modern computersژمارە نیشانەدارە تەواوەکان لە نزیکەی هەموو نوێیەکاندا
Quick checkپشکنینی خێراWhich complement do modern computers use for signed integers? نوێیەکان بۆ ژمارە نیشانەدارە تەواوەکان کام تەواوکەر بەکاردەهێنن؟

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

MistakeهەڵەExampleنموونەCorrectدروست
Dropping leading zeros before complementingلابردنی سفرەکانی پێشەوە پێش وەرگرتنی تەواوکەر00000101→101→01100000101 \to 101 \to 011 ✗00000101→1111101100000101 \to 11111011 ✓
Adding 1 at the left endزیادکردنی 1 لە سەری چەپ01010+10000=1101001010 + 10000 = 11010 ✗01010+00001=0101101010 + 00001 = 01011 ✓
Forgetting the carries of +1لەبیرکردنی ⁦+1⁩01011+1=0101201011 + 1 = 01012 ✗01011+1=0110001011 + 1 = 01100 ✓
Shortcut: flipping the first 1 tooڕێگای کورت: پێچەوانەکردنەوەی یەکەم 1ـیش10110100→0100100010110100 \to 01001000 ✗10110100→0100110010110100 \to 01001100 ✓
Stopping at the 1's complementوەستان لە 10101→0101010101 \to 01010 ✗10101→0101110101 \to 01011 ✓
Keeping a carry out of the leftmost bitهێشتنەوەی کە لە چەپترین دەردەچێت00000→10000000000 \to 100000 ✗00000→0000000000 \to 00000 ✓

Self-check: a number plus its 2's complement must give 2n2^n, that is 1 followed by n zeros; a number plus its 1's complement gives n ones.

پشکنینی خۆت: ژمارەیەک + خۆی دەبێت 2n2^n بدات، واتە 1 و دوای ئەو n سفر؛ ژمارەیەک + خۆی n دانە 1 دەدات.

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

The lecture's five exercises. Find the 1's complement, then the 2's complement; then check with the shortcut.

پێنج ڕاهێنانەکەی وانەکە. بدۆزەرەوە، پاشان ؛ پاشان بە ڕێگا کورتەکە بیپشکنە.

Exerciseڕاهێنان1. 2's complements of 8-bit numbersڕاهێنانی 1: تەواوکەری دووی ژمارەی 8 بیتی

Find the 2's complement of: (a) 10110010 (b) 11001110 (c) 10111000 (d) 00010110 (e) 11111100

بدۆزەرەوە بۆ: (أ) ⁦10110010⁩ (ب) ⁦11001110⁩ (ج) ⁦10111000⁩ (د) ⁦00010110⁩ (هـ) ⁦11111100⁩

Hintئاماژە

Flip every bit, add 1 at the right end, and keep 8 bits.

هەموو پێچەوانە بکەرەوە، 1 بخەرە سەر سەری ڕاست و 8 بهێڵەوە.

Show answerوەڵامەکە پیشان بدە
Numberژمارە1's complement2's complementCheckپشکنین
101100101011001001001101010011010100111001001110178+78=256178 + 78 = 256
110011101100111000110001001100010011001000110010206+50=256206 + 50 = 256
101110001011100001000111010001110100100001001000184+72=256184 + 72 = 256
00010110000101101110100111101001111010101110101022+234=25622 + 234 = 256
111111001111110000000011000000110000010000000100252+4=256252 + 4 = 256

With the shortcut, (c) 10111000: copy 1000, flip 1011→01001011 \to 0100, giving 01001000. In (e) 11111100 the 2's complement is 00000100 (252+4=256252 + 4 = 256).

بە ڕێگا کورتەکە، (ج) ⁦10111000: 1000⁩ کۆپی بکە و ⁦1011⁩ پێچەوانە بکەرەوە (1011→01001011 \to 0100)، کە ⁦01001000⁩ دەدات. لە (هـ)دا ⁦11111100⁩ یەکسانە بە ⁦00000100⁩ (252+4=256252 + 4 = 256).

Exerciseڕاهێنان2. The edge casesڕاهێنانی 2: حاڵەتە تایبەتەکان

Find the 8-bit 2's complement of (a) 00000000 and (b) 10000000. What is special about them?

8 (أ) ⁦00000000⁩ و (ب) ⁦10000000⁩ بدۆزەرەوە. چییان تایبەتە؟

Show answerوەڵامەکە پیشان بدە

(a) 11111111+1=1 0000000011111111 + 1 = 1\,00000000; keep 8 bits: 00000000. The complement of 0 is 0 itself.

(أ) 11111111+1=1 0000000011111111 + 1 = 1\,00000000؛ 8 بهێڵەوە: ⁦00000000⁩. تەواوکەری 0 خودی 0ـە.

(b) 01111111+1=1000000001111111 + 1 = 10000000: the number is its own 2's complement (256−128=128256 - 128 = 128). In signed 8-bit numbers this pattern is −128-128, which has no positive partner; the signed numbers lesson explains why.

(ب) 01111111+1=1000000001111111 + 1 = 10000000: ژمارەکە خۆیەتی (256−128=128256 - 128 = 128). لە 8 ئەم شێوەیە −128-128ـە کە هیچ هاوتایەکی موجەبی نییە؛ وانەی ژمارە نیشانەدارەکان هۆکارەکەی ڕوون دەکاتەوە.

Exerciseڕاهێنان3. Complements in base 6ڕاهێنانی 3: تەواوکەر لە بنچینەی 6دا

Base 6 uses the digits 0 to 5, so its (r−1)(r - 1)'s complement is the 5's complement. (a) Find the 5's complement of (2104)6(2104)_6. (b) Check it by adding. (c) What is the 6's complement of (2104)6(2104)_6?

6 0 تا 5 بەکاردەهێنێت، بۆیە لێرەدا تەواوکەری (r−1)(r - 1) تەواوکەری پێنج پێی دەوترێت. (أ) تەواوکەری پێنجی (2104)6(2104)_6 بدۆزەرەوە. (ب) بە کۆکردنەوە بیپشکنە. (ج) تەواوکەری شەشی (2104)6(2104)_6 چییە؟

Hintئاماژە

Each digit d becomes 5−d5 - d, just as each decimal digit becomes 9−d9 - d.

هەر d دەبێتە 5−d5 - d، ڕێک وەک چۆن هەر دەبێتە 9−d9 - d.

Show answerوەڵامەکە پیشان بدە
(a)  5−2,  5−1,  5−0,  5−4  ⇒  (3451)6(b)  (2104)6+(3451)6=(5555)6(c)  (3451)6+1=(3452)6\begin{aligned} (a)\;& 5-2,\; 5-1,\; 5-0,\; 5-4 \;\Rightarrow\; (3451)_6 \\ (b)\;& (2104)_6 + (3451)_6 = (5555)_6 \\ (c)\;& (3451)_6 + 1 = (3452)_6 \end{aligned}

In (b) no column carries: every column gives 5, the largest base-6 digit, just as 9s appear in decimal. For (c), (2104)6+(3452)6=(10000)6=64(2104)_6 + (3452)_6 = (10000)_6 = 6^4.

لە (ب)دا هیچ ستوونێک نییە: هەموو ستوونێک 5 دەدات، گەورەترین 6، ڕێک وەک چۆن لە 9 دەردەکەوێت. بۆ (ج): (2104)6+(3452)6=(10000)6=64(2104)_6 + (3452)_6 = (10000)_6 = 6^4.

Summaryپوختە

  • 1's complement: flip every bit; it equals (2n−1)−x(2^n - 1) - x.
  • 2's complement: 1's complement + 1; it equals 2n−x2^n - x.
  • Shortcut: copy up to and including the first 1 from the right, flip the rest.
  • Keep the number of bits and drop a carry out of the leftmost bit.
  • The 2's complement is how computers write −x-x, so A−BA - B = AA + (the 2's complement of BB).
  • : هەموو پێچەوانە بکەرەوە؛ یەکسانە بە (2n−1)−x(2^n - 1) - x.
  • : + 1؛ یەکسانە بە 2n−x2^n - x.
  • ڕێگای کورت: تا یەکەم 1 لە لای ڕاستەوە کۆپی بکە (ئەویش لەگەڵیدا) و ئەوی تر پێچەوانە بکەرەوە.
  • ژمارەی بهێڵەوە و ئەو لە چەپترین دەردەچێت فڕێ بدە.
  • ئەو ڕێگایەیە کە −x-xـی پێ دەنووسن، بۆیە A−BA - B یەکسانە بە AA + BB.
Review questionsپرسیارەکانی پێداچوونەوە
What is the 1's and the 2's complement of 10101?تەواوکەری یەک و تەواوکەری دووی ⁦10101⁩ چییە؟

01010 and 01011.

⁦01010⁩ و ⁦01011⁩.

Why does the shortcut leave the right part unchanged?بۆچی ڕێگا کورتەکە بەشی ڕاست وەک خۆی دەهێڵێتەوە؟

Because the +1 turns the flipped trailing zeros back into 0s and restores the first 1; only the bits to its left stay flipped.

چونکە ⁦+1⁩ سفرە پێچەوانەکراوەکانی کۆتایی دەکاتەوە 0 و یەکەم 1 دەگەڕێنێتەوە؛ تەنها لای چەپی پێچەوانە دەمێننەوە.

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

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

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

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

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

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

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