Chapterبەش 4 · Lectureوانە 4.2
The Gray Codeکۆدی گرەی
The Gray (reflected binary) code, in which only one bit changes from one number to the next: why rotary encoders need it, the 0–15 table, building it by reflection, XOR in one minute, and converting between binary and Gray code in both directions, with the exam question 122 → one byte.
(کۆدی ڕەنگدراوە) کە تێیدا لە ژمارەیەکەوە بۆ ژمارەی دواتر تەنها یەک دەگۆڕێت: بۆچی پێویستیان پێیەتی، خشتەی 0–15، دروستکردنی بە ڕەنگدانەوە، XOR لە یەک خولەکدا و گۆڕین لە نێوان و بە هەردوو ئاراستەدا، لەگەڵ پرسیاری تاقیکردنەوەی 122 ← یەک .
What you will learnچی فێر دەبیت
- Explain why the Gray code is used in position encoders.
- Build the Gray code by reflection and read the 0–15 table.
- Convert binary to Gray code and Gray code to binary with XOR.
- ڕوون بکەیتەوە بۆچی لە ئامێرەکانی پێوانی شوێندا بەکار دێت.
- بە ڕەنگدانەوە دروست بکەیت و خشتەی 0–15 بخوێنیتەوە.
- بە XOR بگۆڕیت بۆ و بۆ .
A code where only one bit changesکۆدێک کە تەنها یەک بیت دەگۆڕێت
When you count in binary, several bits can change at once: from 7 (0111) to 8 (1000) all four bits flip. The Gray code is a different order of the same bit patterns, chosen so that only one bit changes from each number to the next.
کاتێک بە دەژمێریت، دەشێت چەند پێکەوە بگۆڕێن: لە 7 (0111) بۆ 8 (1000) هەر چوار دەگۆڕێن. ڕیزبەندییەکی ترە بۆ هەمان پێکهاتەکانی ، بە جۆرێک هەڵبژێردراوە کە لە هەر ژمارەیەکەوە بۆ ژمارەی دواتر تەنها یەک بگۆڕێت.
A binary code in which only one bit changes from one number to the next. Position encoders use it because changing several bits at once can give wrong readings.
کۆدێکی کە تەنها یەک بیت لە ژمارەیەکەوە بۆ ژمارەی دواتر دەگۆڕێت. ئامێرەکانی پێوانی شوێن بەکاری دەهێنن، چونکە گۆڕانی چەند بیتێک لە یەک کاتدا دەکرێت خوێندنەوەی هەڵە بدات.
- It is unweighted: its bit positions have no values like 8, 4, 2, 1, so you cannot add up weights to read it.
- It is not an arithmetic code: you do not add or subtract Gray numbers directly. You convert them to binary first.
- بێکێشە: شوێنی هیچ وەک 8، 4، 2، 1یان نییە، بۆیە ناتوانیت بە کۆکردنەوەی بیخوێنیتەوە.
- کۆدێکی ژمێرکاری نییە: ژمارەکانی ڕاستەوخۆ کۆ ناکەیتەوە و لێک دەرناکەیت. سەرەتا دەیانگۆڕیت بۆ .
Its other name is the reflected binary code. Frank Gray, an engineer at Bell Labs, gave it that name in a patent filed in 1947 (granted in 1953), and the code was later named after him. A French telegraph engineer had already used such a code in 1878.
ناوێکی تری کۆدی ڕەنگدراوەیە (reflected binary code). فرانک گرەی، ئەندازیارێک لە Bell Labs، ئەم ناوەی لە داهێنانێکی تۆمارکراودا دانا کە لە 1947 پێشکەش کرا (لە 1953 پەسەند کرا) و دواتر کۆدەکە بە ناوی ئەوەوە ناونرا. ئەندازیارێکی فەرەنسیی تەلەگراف پێشتر لە 1878 کۆدێکی وای بەکار هێنابوو.
Why one bit at a time mattersبۆچی گۆڕانی یەک بیت لە یەک کاتدا گرنگە
Imagine a machine that reads a number from sensors — for example the position of a turning shaft. In real hardware the bits never change at exactly the same instant. If three bits must change together, for a tiny moment some have changed and some have not, and the machine can read a number that is neither the old one nor the new one.
ئامێرێک لە بەرچاو بگرە کە ژمارەیەک لە دەخوێنێتەوە — بۆ نموونە شوێنی تەوەرەیەکی خولاوە. لە ڕاستەقینەدا هەرگیز ڕێک لە یەک ساتدا ناگۆڕێن. ئەگەر سێ دەبێت پێکەوە بگۆڕێن، بۆ ساتێکی زۆر کورت هەندێکیان گۆڕاون و هەندێکیان نا و ئامێرەکە دەشێت ژمارەیەک بخوێنێتەوە کە نە کۆنەکەیە نە نوێیەکە.


Why one bit at a time mattersبۆچی گۆڕانی یەک بیت لە یەک کاتدا گرنگە
Readings that are neither old nor newخوێندنەوەیەک کە نە کۆنە نە نوێ
Look at 3 → 4. Binary goes from 011 to 100: all three bits change. Depending on which bit is fastest, the machine might see 111 (7), 001 (1), 000 (0) or any other mix for a moment. With the Gray code, 3 → 4 is 010 → 110: one bit changes, so every reading is either 3 or 4. The more bits change at once, the bigger the possible error — that is why position encoders use the Gray code.
سەیری 3 ← 4 بکە. لە 011ـەوە دەچێتە 100: هەر سێ دەگۆڕێن. بەپێی ئەوەی کام خێراترە، ئامێرەکە بۆ ساتێک دەشێت 111 (7)، 001 (1)، 000 (0) یان هەر تێکەڵەیەکی تر ببینێت. بە ، 3 ← 4 دەبێتە 010 ← 110: یەک دەگۆڕێت، بۆیە هەموو خوێندنەوەیەک یان 3ـە یان 4. تا زیاتر پێکەوە بگۆڕێن، هەڵەی ئەگەری گەورەترە — بۆیە ئامێرەکانی پێوانی شوێن بەکار دەهێنن.
Why one bit at a time mattersبۆچی گۆڕانی یەک بیت لە یەک کاتدا گرنگە
An everyday pictureوێنەیەکی ڕۆژانە
A teacher signals a number to the class with three light switches. If she must flip three switches to go from one number to the next, students who look while her hand is moving see a wrong pattern. If every step flips only one switch, anyone looking at any moment sees either the old number or the new one.
مامۆستایەک بە سێ کلیلی گڵۆپ ژمارەیەک بۆ پۆلەکە نیشان دەدات. ئەگەر بۆ چوون لە ژمارەیەکەوە بۆ دواتر دەبێت سێ کلیل بگۆڕێت، ئەو قوتابیانەی لە کاتی جووڵەی دەستیدا سەیر دەکەن پێکهاتەیەکی هەڵە دەبینن. ئەگەر هەر هەنگاوێک تەنها یەک کلیل بگۆڕێت، هەر کەسێک لە هەر ساتێکدا سەیر بکات یان ژمارە کۆنەکە دەبینێت یان نوێیەکە.
Where the analogy stops workingسنووری ئەم نموونەیە
A teacher is slow; electronic sensors differ by microseconds. The idea is the same, but the Gray code only protects reading a position — it does not make arithmetic easier.
مامۆستا خاوە؛ بە چەند میکرۆچرکەیەک جیاوازن. بیرۆکەکە هەمانە، بەڵام تەنها خوێندنەوەی شوێن دەپارێزێت — ژمێرکاری ئاسانتر ناکات.
Why one bit at a time mattersبۆچی گۆڕانی یەک بیت لە یەک کاتدا گرنگە
Inside a rotary encoderناو کۆدکەرێکی سووڕانەوە
A rotary encoder tells a machine the angle of a turning shaft — a volume knob, a robot joint, a steering wheel. A disc on the shaft has rings of dark and light sectors, and a row of sensors reads one bit from each ring. The picture compares a 3-bit binary disc with a 3-bit Gray-code disc, with the sensors exactly on the line between positions 3 and 4.
بە ئامێرێک دەڵێت تەوەرەیەکی خولاوە لە چ گۆشەیەکدایە — دوگمەی دەنگ، جومگەی ، سووکانی ئۆتۆمبێل. قرسێک لەسەر تەوەرەکە ئەڵقەی بەشی تاریک و ڕووناکی هەیە و ڕیزێک لە هەر ئەڵقەیەک یەک دەخوێنێتەوە. وێنەکە قرسێکی 3 بیتی و قرسێکی 3 بیتی بەراورد دەکات و ڕێک لەسەر هێڵی نێوان شوێنی 3 و 4ن.
A that turns the angle of a turning shaft or knob into a code. A disc with rings of dark and light sectors turns in front of a row of sensors, one sensor per ring, and each sensor reads one . Most of them use the , so only one bit changes between neighbouring positions.
کە گۆشەی تەوەرەیەکی خولاوە یان دوگمەیەکی خولاوە دەکات بە کۆدێکی . قرسێک کە ئەڵقەی بەشی تاریک و ڕووناکی هەیە لەبەردەم ڕیزێک دەخولێتەوە (بۆ هەر ئەڵقەیەک یەک ) و هەر یەک دەخوێنێتەوە. زۆربەیان بەکار دەهێنن، بۆیە لە نێوان دوو شوێنی دراوسێدا تەنها یەک دەگۆڕێت.
The 4-bit Gray codeکۆدی گرەیی 4 بیتی
Here are the numbers 0 to 15 in binary and in Gray code. Bold bits are the ones that changed from the row above.
ئەمە ژمارەکانی 0 تا 15ـە بە و بە . ئەوانەن کە لە ڕیزی سەرەوە گۆڕاون.
| Decimal | Binary | Bits changedچەند گۆڕا | Gray code | Bits changedچەند گۆڕا |
|---|---|---|---|---|
| 0 | 0000 | – | 0000 | – |
| 1 | 0001 | 1 | 0001 | 1 |
| 2 | 0010 | 2 | 0011 | 1 |
| 3 | 0011 | 1 | 0010 | 1 |
| 4 | 0100 | 3 | 0110 | 1 |
| 5 | 0101 | 1 | 0111 | 1 |
| 6 | 0110 | 2 | 0101 | 1 |
| 7 | 0111 | 1 | 0100 | 1 |
| 8 | 1000 | 4 | 1100 | 1 |
| 9 | 1001 | 1 | 1101 | 1 |
| 10 | 1010 | 2 | 1111 | 1 |
| 11 | 1011 | 1 | 1110 | 1 |
| 12 | 1100 | 3 | 1010 | 1 |
| 13 | 1101 | 1 | 1011 | 1 |
| 14 | 1110 | 2 | 1001 | 1 |
| 15 | 1111 | 1 | 1000 | 1 |
The Gray code is also cyclic: from 15 (1000) back to 0 (0000) only one bit changes. Binary changes all four bits there (1111 → 0000).
بازنەییشە: لە 15 (1000)ـەوە بۆ 0 (0000) تەنها یەک دەگۆڕێت. لەوێدا هەر چوار دەگۆڕێت (1111 ← 0000).
How the Gray code table is built and its properties: unweighted, reflected, one bit changing at a time, compared with binary, cyclic.
خشتەی چۆن دروست دەکرێت و تایبەتمەندییەکانی: بێکێشە، ڕەنگدراوەیە، هەر جارە تەنها یەک دەگۆڕێت و بازنەییە، لەگەڵ بەراورد بە .
2:17-10:00: structure of the code, unweighted, self-reflecting, unit distance, comparison with binary, cyclic.
لە 2:17 تا 10:00: پێکهاتەی کۆدەکە، بێکێشی، ڕەنگدانەوە، گۆڕانی تەنها یەک لە نێوان دوو ژمارەی دراوسێدا، بەراورد بە و بازنەیی.
The 4-bit Gray codeکۆدی گرەیی 4 بیتی
Why "reflected"?بۆچی «ڕەنگدراوە»؟
You can build the Gray code without any table. Start with the 1-bit list 0, 1. To get the next size, write the list again as it is with a 0 in front, then write it mirrored (upside down) underneath with a 1 in front. Each new list keeps the one-bit-change rule, also across the mirror line.
دەتوانیت بەبێ هیچ خشتەیەک دروست بکەیت. لە لیستی 1 بیتیی 0، 1 دەست پێ بکە. بۆ قەبارەی دواتر، لیستەکە وەک خۆی بنووسەرەوە و 0 بخە پێشی، پاشان لە ژێریدا وەک ئاوێنە (سەرەوژێر) بینووسە و 1 بخە پێشی. هەر لیستێکی نوێ یاسای گۆڕانی یەک دەپارێزێت، بە سەر هێڵی ئاوێنەکەشدا.
XOR in one minuteXOR لە یەک خولەکدا
Conversions between binary and Gray code use one operation, XOR (exclusive OR), written . For two bits, XOR gives 1 when the bits are different and 0 when they are the same. You can also think of it as adding two bits and ignoring the carry. (In hardware this is done by the XOR gate.)
گۆڕینی نێوان و یەک کار بەکار دەهێنێت: XOR (exclusive OR) کە بە دەنووسرێت. بۆ دوو ، XOR 1 دەدات کاتێک جیاواز بن و 0 کاتێک وەک یەک بن. دەتوانیت وەک کۆکردنەوەی دوو بیری لێ بکەیتەوە بەبێ گوێدانە . (لە گەیتی XOR ئەم کارە دەکات.)
| A | B | |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Binary to Gray codeلە دووانییەوە بۆ کۆدی گرەی
The leftmost Gray bit is the same as the leftmost binary bit (the MSB).
لای چەپترین هەمان لای چەپترین ().
Moving right, XOR each pair of neighbouring binary bits. Each result is the next Gray bit.
بەرەو ڕاست، هەر دوو دراوسێی XOR بکە. هەر ئەنجامێک دواتری .
For 5 bits: , , , .
بۆ 5 : ، ، ، .
Check: 1 stays 1; ; ; ; → 10011.
پشکنین: 1 وەک خۆی 1ـە؛ ؛ ؛ ؛ ← 10011.
Gray code to binaryلە کۆدی گرەیەوە بۆ دووانی
The leftmost binary bit is the same as the leftmost Gray bit.
لای چەپترین هەمان لای چەپترین .
Moving right, XOR the binary bit you just made with the next Gray bit. The result is the next binary bit.
بەرەو ڕاست، ئەو ئێستا دروستت کرد لەگەڵ دواتری XOR بکە. ئەنجامەکە دواتری .
For 5 bits: , , , .
بۆ 5 : ، ، ، .
Check: 1; ; ; ; → 11101. It is the number we started from on the previous screen.
پشکنین: 1؛ ؛ ؛ ؛ ← 11101. ئەمە ئەو ژمارەیەیە کە لە شاشەی پێشوودا لێیەوە دەستمان پێکرد.
Exercisesڕاهێنانەکان
Convert to Gray code: (a) 101101 (b) 11000110 (c) 10110.
بیانگۆڕە بۆ : (أ) 101101 (ب) 11000110 (ج) 10110.
Show answerوەڵامەکە پیشان بدە
- (a) →
111011 - (b) →
10100101 - (c) →
11101
- (أ) ، کەواتە
111011 - (ب) ، کەواتە
10100101 - (ج) ، کەواتە
11101
Convert to binary: (a) 11011 (b) 100111 (c) 10101111.
بیانگۆڕە بۆ : (أ) 11011 (ب) 100111 (ج) 10101111.
Hintئاماژە
XOR with the binary bit you just made, not with the previous Gray bit.
لەگەڵ ئەو ئێستا دروستت کرد XOR بکە، نەک لەگەڵ پێشووی .
Show answerوەڵامەکە پیشان بدە
- (a) →
10010 - (b) →
111010 - (c) →
11001010
- (أ) ، کەواتە
10010 - (ب) ، کەواتە
111010 - (ج) ، کەواتە
11001010
Use 1 byte to convert to Gray code.
بە یەک بگۆڕە بۆ .
Show answerوەڵامەکە پیشان بدە
- Decimal to binaryدەیی بۆ دووانی
Divide by 2 again and again and write down each remainder. Read the remainders from the bottom up:
1111010.چەند جار دابەشی 2 بکە و هەر پاشماوەیەک بنووسە. پاشماوەکان لە خوارەوە بۆ سەرەوە بخوێنەرەوە:
1111010.Divisionدابەشکردن Quotientئەنجام Remainderپاشماوە 61 0 30 1 15 0 7 1 3 1 1 1 0 1 - Fill one byteیەک بایت پڕ بکەرەوە
One byte is 8 bits, so add a 0 on the left:
01111010.یەک 8 ، بۆیە 0ـێک بخە لای چەپ:
01111010. - Binary to Grayدووانی بۆ کۆدی گرەی
→
01000111.←
01000111.
(a) Write the number 9 in 4-bit Gray code. (b) Which decimal number is the Gray code 1101?
(أ) ژمارەی 9 بە 4 بیتی بنووسە. (ب) 1101 کام ژمارەی ؟
Show answerوەڵامەکە پیشان بدە
- (a) 9 =
1001→ →1101. - (b)
1101→ →1001= 9. The two conversions undo each other.
- (أ) 9 =
1001؛ پاشان ، کەواتە1101. - (ب)
1101→ →1001= 9. هەردوو گۆڕینەکە یەکتر هەڵدەوەشێننەوە.
Where the Gray code is usedکۆدی گرەی لە کوێ بەکار دێت
- Rotary and linear encoders: volume and temperature knobs, robot arms, machine tools, wind-direction sensors.
- Karnaugh maps: the rows and columns of these maps are labelled in Gray-code order, so that neighbouring cells differ in one variable. You will use them to simplify logic circuits in Karnaugh maps.
- Digital communication: neighbouring signal levels get codes that differ in one bit, so a small reading error damages only one bit.
- Counters read by another clock signal: when one part of a chip reads a counter that another part is changing, a Gray-code counter guarantees the reading is the old or the new value.
- و پێوەرە هێڵییەکان: دوگمەی دەنگ و پلەی گەرمی، دەستی ، ئامێرەکانی کارگە، ئاراستەی با.
- : ڕیز و ستوونەکانی ئەم نەخشانە بە ڕیزبەندیی ناونراون، بۆ ئەوەی خانە دراوسێکان تەنها لە یەک گۆڕاودا جیاواز بن. لە وانەی نەخشەکانی کارنۆ بۆ سادەکردنەوەی بەکاریان دەهێنیت.
- گەیاندنی : ئاستە دراوسێکانی کۆدێک وەردەگرن کە تەنها لە یەک جیاوازە، بۆیە هەڵەیەکی بچووکی خوێندنەوە تەنها یەک تێک دەدات.
- کە بە تر دەخوێنرێنەوە: کاتێک بەشێکی دەخوێنێتەوە کە بەشێکی تر دەیگۆڕێت، بە دڵنیا دەکاتەوە کە خوێندنەوەکە یان بەها کۆنەکەیە یان نوێیەکە.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | Fixچارەسەر |
|---|---|
| Using ordinary addition instead of XOR ().بەکارهێنانی کۆکردنەوەی ئاسایی لە جیاتی XOR (). | XOR has no carry: .XOR هیچ نییە: . |
| Gray → binary: XOR-ing neighbouring Gray bits. ← : XORـکردنی . | XOR each new binary bit with the next Gray bit.هەر نوێی لەگەڵ دواتری XOR بکە. |
Dropping leading zeros when a width is asked for (1000111 for one byte).فڕێدانی سفری سەرەتا کاتێک پانییەک داوا کراوە (1000111 بۆ یەک ). | Keep all n bits: 01000111.هەموو n بهێڵەوە: 01000111. |
| Reading a Gray code with weights 8, 4, 2, 1.خوێندنەوەی بە 8، 4، 2، 1. | Gray is unweighted: convert to binary first. بێکێشە: سەرەتا بیگۆڕە بۆ . |
| Starting the XOR chain from the right.دەستپێکردنی زنجیرەی XOR لە ڕاستەوە. | Start at the leftmost bit (MSB), which is copied.لە لای چەپترین () دەست پێ بکە کە وەک خۆی دەنووسرێتەوە. |
Summaryپوختە
- In the Gray code only one bit changes between neighbouring numbers, also from the last back to the first.
- It is unweighted and not used for arithmetic; its job is safe reading of positions (rotary encoders).
- It is built by reflection: copy with 0 in front, mirror with 1 in front.
- Binary → Gray: copy the MSB, then XOR neighbouring binary bits.
- Gray → binary: copy the MSB, then XOR each new binary bit with the next Gray bit.
- لە لە نێوان ژمارە دراوسێکاندا تەنها یەک دەگۆڕێت، لە دواییەوە بۆ یەکەمیش.
- بێکێشە و بۆ ژمێرکاری بەکار نایەت؛ کارەکەی خوێندنەوەی سەلامەتی شوێنە ().
- بە ڕەنگدانەوە دروست دەبێت: لەبەرگرتنەوە بە 0 لە پێشەوە و ئاوێنە بە 1 لە پێشەوە.
- ← : وەک خۆی بنووسە، پاشان XOR بکە.
- ← : وەک خۆی بنووسە، پاشان هەر نوێی لەگەڵ دواتری XOR بکە.
Is the Gray code a weighted code?ئایا کۆدی گرەی کۆدێکی کێشدارە؟
No. Its positions have no weights; to know the value, convert it to binary.
نەخێر. شوێنەکانی کێشیان نییە؛ بۆ زانینی بەهاکەی بیگۆڕە بۆ .
What is 5 in 3-bit Gray code?5 بە کۆدی گرەیی 3 بیتی چییە؟
111 (binary 101 → ).
111 ( 101 ← ).
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
Converting binary to Gray code and Gray code back to binary step by step, with practice examples and why Gray code is useful.
گۆڕینی بۆ و گەڕاندنەوەی بۆ هەنگاو بە هەنگاو، لەگەڵ نموونەی ڕاهێنان و بۆچی بەسوودە.
1:05 binary to Gray; 2:32 practice; 4:26 working backwards; 7:06 Gray to binary; 9:16 advantages.
1:05 بۆ ؛ 2:32 ڕاهێنان؛ 4:26 کارکردن بە پێچەوانەوە؛ 7:06 بۆ ؛ 9:16 سوودەکانی.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
ASCII and Character CodesASCII و کۆدەکانی نووسە
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 ن.