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 کۆپی بکە» بەکاربهێنە. ببینە بۆچی تەواوکەرەکان ڕێگە بە دەدەن بنووسێت و بە کۆکردنەوە لێدەربکات.
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 and and check answers with them.
- Explain why the 2's complement represents and turns subtraction into addition.
- ژمارەی بە کۆکردنەوەی تەواوکەری نۆ لێ دەربکەیت و ببینیت کە و هەمان بیرۆکەن لە 2دا.
- ژمارەیەکی بە هەر درێژییەک بدۆزیتەوە.
- بە پێچەوانەکردنەوە و زیادکردنی 1 و بە ڕێگا کورتەکە بدۆزیتەوە.
- تەواوکەرەکان وەک و ڕوون بکەیتەوە و وەڵامەکانی پێ بپشکنیت.
- ڕوونی بکەیتەوە بۆچی جێگەی دەگرێتەوە و لێدەرکردن دەکاتە کۆکردنەوە.
Why complements?بۆچی تەواوکەرەکان؟
The binary system has base . Every base has two complements, the 's complement and the 's complement, so binary has two: the 1's complement and the 2's complement.
سیستەمی ـە. هەر دوو تەواوکەری هەیە: تەواوکەری و تەواوکەری . بۆیە دوو تەواوکەری هەیە: و .
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.
گرنگن چونکە ڕێگە بە دەدەن پیشان بدات تەنها بە ، هەروەها لێدەرکردن دەکەنە کۆکردنەوە، بۆیە یەک دەتوانێت هەردوو کارەکە بکات.
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 : 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 .
وای دابنێ کیلۆمەترژمێری 6 ژمارەیی تەکسییەک بتوانێت بەرەو دواوە بڕوات. یەک کیلۆمەتر بەرەو دواوە لە 000000ـەوە 999999 پیشان دەدات. ئەو 999999ـە وەک ڕەفتار دەکات: 1ـی بخەرە سەر و دەگەڕێیتەوە بۆ 000000. بە 4 هەمان کار دەکات: یەک هەنگاو بەرەو دواوە لە 0000ـەوە 1111 پیشان دەدات و 1111 ڕێک 4 0001ـە. بەم شێوەیە دەنووسێت.
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.
کیلۆمەترژمێری ڕاستەقینە هەرگیز بەرەو دواوە ناڕوات و دووریی سالب هەڵناگرێت. بە ئەنقەست ئەم سووڕانەوەیە بەکاردەهێنێت و تەنها لە ناو ژمارەیەکی جێگیری .
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 . For 287 that gives 712, because . The 10's complement is one more: , because .
تەواوکەرەکان داهێنانی نین: هەموو تەواوکەری هەیە و لە دەتوانیت بە قەڵەم و کاغەز تاقییان بکەیتەوە. بۆ تەواوکەری نۆ، هەر d لە ژمارەکەدا بکە بە . بۆ 287 ئەمە 712 دەدات، چونکە . تەواوکەری دە یەکێک زیاترە: ، چونکە .
Now the trick: subtract by adding. To work out , add the 9's complement of 287 and one more, then drop the leading 1:
ئێستا فێڵەکە: بە کۆکردنەوە لێی دەربکە. بۆ دۆزینەوەی ، تەواوکەری نۆی 287 و یەکێکی تر بخەرە سەر 652، پاشان 1ـی پێشەوە لاببە:
Why it works: adding is the same as adding . So the sum is 1000 too big, and dropping the leading 1 takes that 1000 away again.
بۆچی کار دەکات: زیادکردنی وەک زیادکردنی وایە. کەواتە کۆکە 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؟).
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 : 68 becomes 0068, its 9's complement is 9931, and . Drop the leading 1: 4956 ✓.
ئەگەر ئەو ژمارەیەی لێی دەردەکەیت کورتتر بوو، سەرەتا بە سفر درێژی بکەرەوە تا هەمان درێژی هەبێت. ئەو سفرانە لە تەواوکەرەکەدا دەبنە 9. بۆ : 68 دەبێتە 0068، تەواوکەری نۆی 9931ـە و . 1ـی پێشەوە لاببە: 4956 ✓.
The general rule for an n-digit number x in base r:
یاسا گشتییەکە بۆ ژمارەیەکی x کە n هەیە لە r:
Left: the 's complement (each digit becomes ). Right: the 's complement, one more. With these are the 9's and 10's complements; with they are the 1's and 2's complements: each bit becomes , which simply flips it, and the 2's complement is that plus 1.
چەپ: تەواوکەری (هەر دەبێتە ). ڕاست: تەواوکەری ، کە یەکێک زیاترە. بە ئەمانە تەواوکەری نۆ و تەواوکەری دەن؛ بە دەبنە و : هەر دەبێتە ، واتە تەنها پێچەوانە دەبێتەوە و ئەوە + 1ـە.
The 1's complement: flip every bitتەواوکەری یەک: هەموو بیتێک پێچەوانە بکەرەوە
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.
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: (). 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 . In hardware it is one NOT gate per bit.
بۆچی پێی دەوترێت تەواوکەر؟ ژمارەکە لە ڕیزێکی 1 بە هەمان درێژی لێ دەربکە: (). لێدەرکردن لە 1ـەکان هەرگیز پێویست نییە، بۆیە هەموو تەنها پێچەوانە دەبێتەوە. بە گشتی ژمارەیەکی n x یەکسانە بە . لە بۆ هەر یەک گەیتی NOTـە.
The 2's complement: flip, then add 1تەواوکەری دوو: پێچەوانە بکەرەوە، پاشان 1 زیاد بکە
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، کە دەخرێتە سەر (سەری ڕاست).
The values tell you what the 2's complement really is: and , and . The 2's complement of an n-bit number x is , the amount that is still missing to reach .
بەهاکان پیشانی دەدەن لە ڕاستیدا چییە: و و . ژمارەیەکی n x یەکسانە بە ، ئەو بڕەی هێشتا ماوە تا بگاتە .
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: ← پێچەوانەکردنەوە ← زیادکردنی 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) بکە:


- 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 ; keep 5 bits: 00000. The 2's complement of 0 is 0.
- ژمارەی بهێڵەوە. ژمارەیەکی 6 تەواوکەرێکی 6 هەیە.
- کە لە چەپترین دەردەچێت فڕێ بدە. بۆ 00000: 11111ـە و ؛ 5 بهێڵەوە: 00000. 0 خۆی 0ـە.
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ـی خۆی و ئەویش لەگەڵیدا، بێ گۆڕان دەرچوو. ئەمە ڕێگایەکی خێراتر دەدات بێ هیچ کۆکردنەوەیەک:
Start at the right end. Copy every bit up to and including the first 1.
لە سەری ڕاستەوە دەست پێبکە. هەموو کۆپی بکە تا یەکەم 1 و ئەویش لەگەڵیدا.
Flip every bit to the left of that 1.
هەموو لە لای چەپی ئەو 1ـە پێچەوانە بکەرەوە.
The 2's complement of 10110100 (180) is 01001100 (76), and . The underlined part 100 is copied; the overlined part is flipped.
10110100 (180) یەکسانە بە 01001100 (76) و . بەشی 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. هەموو ئەوانەی لە لای چەپترن پێچەوانە دەمێننەوە.
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 بە «تا یەکەم 1 کۆپی بکە، ئەوانی تر پێچەوانە بکەرەوە» (لە وێنەکانی ڤیدیۆکەوە پشکنراوە).
The 2's complement as a negative numberتەواوکەری دوو وەک ژمارەی سالب
In a fixed number of bits, the 2's complement of x behaves exactly like . With 8 bits, 00000101 is and its 2's complement 11111011 stands for . Add them and the 8 bits give 0, because the carry out of the eighth bit is dropped:
لە ژمارەیەکی جێگیری ، x ڕێک وەک ڕەفتار دەکات. بە 8 ، 00000101 یەکسانە بە و ئەو ژمارەیە، 11111011، جێگەی دەگرێتەوە. کۆیان بکەرەوە و 8 0 دەدەن، چونکە ئەو لە هەشتەم دەردەچێت فڕێ دەدرێت:
: the crossed-out 1 is the ninth bit, which does not fit in 8 bits.
: 1ـە خەتلێدراوەکە نۆیەمە، کە لە 8 جێی نابێتەوە.
Taking the 2's complement twice gives the number back (), just as . And subtraction becomes addition: = + (the 2's complement of ). Here is from the previous lesson, done with an adder only:
دوو جار وەرگرتنی ژمارەکە دەگەڕێنێتەوە ()، ڕێک وەک . هەروەها لێدەرکردن دەبێتە کۆکردنەوە: یەکسانە بە + . ئەمە ـە لە وانەی پێشووـەوە، تەنها بە کۆکردنەوە:
The 2's complement of 0110 is 1010. ; drop the fifth bit: , the same answer as with borrows.
0110 یەکسانە بە 1010. ؛ پێنجەم فڕێ بدە: ، هەمان وەڵامی بە .
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.
چۆنیەتیی هەڵگرتنی لە بەم بیرۆکانە و چۆنیەتیی کۆکردنەوە و لێدەرکردنی تەواویان بابەتی ژمارە نیشانەدارەکان و ژمێرکاریی ژمارە نیشانەدارەکانـن.
1's or 2's complement?تەواوکەری یەک یان تەواوکەری دوو؟
| Propertyتایبەتمەندی | 1's complement | 2'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 | ||
| Example 10101 (21)نموونە 10101 (21) | ||
| Used todayئەمڕۆ لە کوێ بەکاردێت | The Internet checksum of IP, TCP and UDP; some older computersپشکنینی کۆی (checksum) IP، TCP و UDP؛ هەندێک کۆن | Signed integers in practically all modern computersژمارە نیشانەدارە تەواوەکان لە نزیکەی هەموو نوێیەکاندا |
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | Exampleنموونە | Correctدروست |
|---|---|---|
| Dropping leading zeros before complementingلابردنی سفرەکانی پێشەوە پێش وەرگرتنی تەواوکەر | ✗ | ✓ |
| Adding 1 at the left endزیادکردنی 1 لە سەری چەپ | ✗ | ✓ |
| Forgetting the carries of +1لەبیرکردنی +1 | ✗ | ✓ |
| Shortcut: flipping the first 1 tooڕێگای کورت: پێچەوانەکردنەوەی یەکەم 1ـیش | ✗ | ✓ |
| Stopping at the 1's complementوەستان لە | ✗ | ✓ |
| Keeping a carry out of the leftmost bitهێشتنەوەی کە لە چەپترین دەردەچێت | ✗ | ✓ |
Self-check: a number plus its 2's complement must give , that is 1 followed by n zeros; a number plus its 1's complement gives n ones.
پشکنینی خۆت: ژمارەیەک + خۆی دەبێت بدات، واتە 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.
پێنج ڕاهێنانەکەی وانەکە. بدۆزەرەوە، پاشان ؛ پاشان بە ڕێگا کورتەکە بیپشکنە.
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 complement | 2's complement | Checkپشکنین |
|---|---|---|---|
With the shortcut, (c) 10111000: copy 1000, flip , giving 01001000. In (e) 11111100 the 2's complement is 00000100 ().
بە ڕێگا کورتەکە، (ج) 10111000: 1000 کۆپی بکە و 1011 پێچەوانە بکەرەوە ()، کە 01001000 دەدات. لە (هـ)دا 11111100 یەکسانە بە 00000100 ().
Find the 8-bit 2's complement of (a) 00000000 and (b) 10000000. What is special about them?
8 (أ) 00000000 و (ب) 10000000 بدۆزەرەوە. چییان تایبەتە؟
Show answerوەڵامەکە پیشان بدە
(a) ; keep 8 bits: 00000000. The complement of 0 is 0 itself.
(أ) ؛ 8 بهێڵەوە: 00000000. تەواوکەری 0 خودی 0ـە.
(b) : the number is its own 2's complement (). In signed 8-bit numbers this pattern is , which has no positive partner; the signed numbers lesson explains why.
(ب) : ژمارەکە خۆیەتی (). لە 8 ئەم شێوەیە ـە کە هیچ هاوتایەکی موجەبی نییە؛ وانەی ژمارە نیشانەدارەکان هۆکارەکەی ڕوون دەکاتەوە.
Base 6 uses the digits 0 to 5, so its 's complement is the 5's complement. (a) Find the 5's complement of . (b) Check it by adding. (c) What is the 6's complement of ?
6 0 تا 5 بەکاردەهێنێت، بۆیە لێرەدا تەواوکەری تەواوکەری پێنج پێی دەوترێت. (أ) تەواوکەری پێنجی بدۆزەرەوە. (ب) بە کۆکردنەوە بیپشکنە. (ج) تەواوکەری شەشی چییە؟
Hintئاماژە
Each digit d becomes , just as each decimal digit becomes .
هەر d دەبێتە ، ڕێک وەک چۆن هەر دەبێتە .
Show answerوەڵامەکە پیشان بدە
In (b) no column carries: every column gives 5, the largest base-6 digit, just as 9s appear in decimal. For (c), .
لە (ب)دا هیچ ستوونێک نییە: هەموو ستوونێک 5 دەدات، گەورەترین 6، ڕێک وەک چۆن لە 9 دەردەکەوێت. بۆ (ج): .
Summaryپوختە
- 1's complement: flip every bit; it equals .
- 2's complement: 1's complement + 1; it equals .
- 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 , so = + (the 2's complement of ).
- : هەموو پێچەوانە بکەرەوە؛ یەکسانە بە .
- : + 1؛ یەکسانە بە .
- ڕێگای کورت: تا یەکەم 1 لە لای ڕاستەوە کۆپی بکە (ئەویش لەگەڵیدا) و ئەوی تر پێچەوانە بکەرەوە.
- ژمارەی بهێڵەوە و ئەو لە چەپترین دەردەچێت فڕێ بدە.
- ئەو ڕێگایەیە کە ـی پێ دەنووسن، بۆیە یەکسانە بە + .
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زاراوە سەرەکییەکان
Signed Numbers: Sign-Magnitude, 1's and 2's Complementژمارە نیشانەدارەکان: نیشانە و نرخی ڕووت، تەواوکەری یەک و تەواوکەری دوو
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Wikipedia: Two's complement (definition 2^N − x, invert and add one, the shortcut, history)
- Wikipedia: Ones' complement (two zeros, end-around carry, machines that used it)
- MIT 6.004 Computation Structures notes: Basics of information (two's complement encoding)
- RFC 1071: Computing the Internet checksum (one's complement sum)
- Wikipedia: Method of complements (nines' and ten's complement, padding, mechanical calculators)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.