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⁩ ← یەک .

13 min read14 خولەک خوێندنەوە · 15 stepsهەنگاو

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) هەر چوار دەگۆڕێن. ڕیزبەندییەکی ترە بۆ هەمان پێکهاتەکانی ، بە جۆرێک هەڵبژێردراوە کە لە هەر ژمارەیەکەوە بۆ ژمارەی دواتر تەنها یەک بگۆڕێت.

Definitionپێناسە

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⁩ کۆدێکی وای بەکار هێنابوو.

Quick checkپشکنینی خێراWhat is special about the Gray code? چ تایبەتمەندییەکی هەیە؟

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.

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

Two rows of three bit boxes, binary and Gray code, count from 0 to 7 and back to 0. Changed bits turn orange. Binary changes 1, 2, 1, 3, 1, 2, 1 and 3 bits on the way; the Gray row changes exactly 1 bit every time, also from 7 back to 0.
Two rows of three bit boxes, binary and Gray code, count from 0 to 7 and back to 0. Changed bits turn orange. Binary changes 1, 2, 1, 3, 1, 2, 1 and 3 bits on the way; the Gray row changes exactly 1 bit every time, also from 7 back to 0.
Counting 0 to 7: binary sometimes changes 3 bits at once; Gray code always changes one.ژماردنی 0 تا 7: هەندێک جار 3 پێکەوە دەگۆڕێت؛ هەمیشە یەک.CêIT
Quick checkپشکنینی خێراA binary sensor goes from 7 (0111) to 8 (1000). How many bits change, and what about Gray code? لە 7 (0111)ـەوە دەچێتە 8 (1000). چەند دەگۆڕێن و چۆنە؟

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. تا زیاتر پێکەوە بگۆڕێن، هەڵەی ئەگەری گەورەترە — بۆیە ئامێرەکانی پێوانی شوێن بەکار دەهێنن.

Quick checkپشکنینی خێراA 3-bit Gray-code encoder sits on the border between 3 (010) and 4 (110). What can the sensors read for a moment? بە 3 بیتی لەسەر سنووری نێوان 3 (010) و 4 (110)ـە. بۆ ساتێک چی دەخوێننەوە؟

Why one bit at a time mattersبۆچی گۆڕانی یەک بیت لە یەک کاتدا گرنگە

An everyday pictureوێنەیەکی ڕۆژانە

Light switches in a classroomکلیلی گڵۆپەکانی پۆل

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.

مامۆستا خاوە؛ بە چەند میکرۆچرکەیەک جیاوازن. بیرۆکەکە هەمانە، بەڵام تەنها خوێندنەوەی شوێن دەپارێزێت — ژمێرکاری ئاسانتر ناکات.

Quick checkپشکنینی خێراWhy does the teacher change only one switch per step?بۆچی مامۆستاکە لە هەر هەنگاوێکدا تەنها یەک کلیل دەگۆڕێت؟

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ن.

Definitionپێناسە

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.

کە گۆشەی تەوەرەیەکی خولاوە یان دوگمەیەکی خولاوە دەکات بە کۆدێکی . قرسێک کە ئەڵقەی بەشی تاریک و ڕووناکی هەیە لەبەردەم ڕیزێک دەخولێتەوە (بۆ هەر ئەڵقەیەک یەک ) و هەر یەک دەخوێنێتەوە. زۆربەیان بەکار دەهێنن، بۆیە لە نێوان دوو شوێنی دراوسێدا تەنها یەک دەگۆڕێت.

Two discs with three rings and eight numbered sectors, dark for 1 and light for 0. A dashed sensor line with three sensor dots sits on the border between sectors 3 and 4. On the binary disc all three rings change colour across that border (011 to 100); on the Gray-code disc only the inner ring changes (010 to 110).دوو قرس بە سێ ئەڵقە و هەشت بەشی ژمارەدار، تاریک بۆ 1 و ڕووناک بۆ 0. هێڵێکی پچڕپچڕی هەستەوەر بە سێ خاڵەوە لەسەر سنووری نێوان بەشی 3 و 4ـە. لە قرسی دووانیدا هەر سێ ئەڵقەکە لەو سنوورەدا ڕەنگیان دەگۆڕێت (011 بۆ 100)؛ لە قرسی کۆدی گرەیدا تەنها ئەڵقەی ناوەوە دەگۆڕێت (010 بۆ 110).
Binary disc and Gray-code disc at the border between positions 3 and 4.قرسی و قرسی لە سنووری نێوان شوێنی 3 و 4دا.CêIT
Quick checkپشکنینی خێراOn the binary disc, the sensors sit on the 3 | 4 border. Which reading is NOT possible for a moment?لەسەر قرسی ، لەسەر سنووری 3 | 4ن. کام خوێندنەوە بۆ ساتێک مەحاڵە؟

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⁩ـە بە و بە . ئەوانەن کە لە ڕیزی سەرەوە گۆڕاون.

Binary and Gray code, 0–15 و ، ⁦0–15⁩
DecimalBinaryBits changedچەند گۆڕاGray codeBits changedچەند گۆڕا
00000–0000–
10001100011
20010200111
30011100101
40100301101
50101101111
60110201011
70111101001
81000411001
91001111011
101010211111
111011111101
121100310101
131101110111
141110210011
151111110001

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).

Quick checkپشکنینی خێراWhat is 10 in 4-bit Gray code?⁦10⁩ بە 4 بیتی چییە؟

Gray Code: Basics, Properties, Importance, and ApplicationsEngineering Funda · Englishئینگلیزی · 69K viewsبینین · 1.1K likesلایک · Partبەشی 2:17–10:00 (ofلە 12:16)

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 بخە پێشی. هەر لیستێکی نوێ یاسای گۆڕانی یەک دەپارێزێت، بە سەر هێڵی ئاوێنەکەشدا.

1 bit: 0, 1. 2 bits: 00, 01, then a mirror line, then 11, 10. 3 bits: 000, 001, 011, 010, mirror line, 110, 111, 101, 100. The new leftmost bit is 0 above the mirror and 1 below it.1 بیت: 0، 1. 2 بیت: 00، 01، پاشان هێڵی ئاوێنە، پاشان 11، 10. 3 بیت: 000، 001، 011، 010، هێڵی ئاوێنە، 110، 111، 101، 100. بیتی نوێی لای چەپ لە سەرووی ئاوێنەکە 0ـە و لە خوارییەوە 1.
Building the Gray code by reflection: 1 bit → 2 bits → 3 bits.دروستکردنی بە ڕەنگدانەوە: 1 ← 2 ← 3 .CêIT
Quick checkپشکنینی خێراReflect the 3-bit list to make 4 bits. What is the 9th code (decimal 8)?لیستی 3 بیتی ڕەنگ بدەرەوە بۆ دروستکردنی 4 . کۆدی نۆیەم (⁦8⁩ـی ) چییە؟

XOR in one minuteXOR لە یەک خولەکدا

Conversions between binary and Gray code use one operation, XOR (exclusive OR), written ⊕\oplus. 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⁩) کە بە ⊕\oplus دەنووسرێت. بۆ دوو ، XOR 1 دەدات کاتێک جیاواز بن و 0 کاتێک وەک یەک بن. دەتوانیت وەک کۆکردنەوەی دوو بیری لێ بکەیتەوە بەبێ گوێدانە . (لە گەیتی XOR ئەم کارە دەکات.)

XOR of two bitsXORـی دوو
ABA⊕BA \oplus B
000
011
101
110
Quick checkپشکنینی خێراWhat is 1⊕11 \oplus 1?1⊕11 \oplus 1 چەندە؟

Binary to Gray codeلە دووانییەوە بۆ کۆدی گرەی

  1. The leftmost Gray bit is the same as the leftmost binary bit (the MSB).

    لای چەپترین هەمان لای چەپترین ().

  2. Moving right, XOR each pair of neighbouring binary bits. Each result is the next Gray bit.

    بەرەو ڕاست، هەر دوو دراوسێی XOR بکە. هەر ئەنجامێک دواتری .

g1=b1,gi=bi−1⊕bi    (i=2,3,… )g_1 = b_1,\quad g_i = b_{i-1} \oplus b_i \;\;(i = 2, 3, \dots)

For 5 bits: g2=b1⊕b2g_2 = b_1 \oplus b_2, g3=b2⊕b3g_3 = b_2 \oplus b_3, g4=b3⊕b4g_4 = b_3 \oplus b_4, g5=b4⊕b5g_5 = b_4 \oplus b_5.

بۆ 5 : g2=b1⊕b2g_2 = b_1 \oplus b_2، g3=b2⊕b3g_3 = b_2 \oplus b_3، g4=b3⊕b4g_4 = b_3 \oplus b_4، g5=b4⊕b5g_5 = b_4 \oplus b_5.

Binary 1 1 1 0 1 on top, Gray 1 0 0 1 1 below. b1 is copied straight down to g1; every other Gray bit comes from an XOR circle fed by two neighbouring binary bits.دووانی 1 1 1 0 1 لە سەرەوە و کۆدی گرەی 1 0 0 1 1 لە خوارەوە. b1 ڕاستەوخۆ دەچێتە خوارەوە بۆ g1؛ هەر بیتێکی تری کۆدی گرەی لە بازنەیەکی XORـەوە دێت کە دوو بیتی دراوسێی دووانی دەیخۆنە ناوی.
Binary 11101 → Gray 10011. ⁦11101⁩ ← ⁦10011⁩.CêIT

Check: 1 stays 1; 1⊕1=01 \oplus 1 = 0; 1⊕1=01 \oplus 1 = 0; 1⊕0=11 \oplus 0 = 1; 0⊕1=10 \oplus 1 = 1 → 10011.

پشکنین: 1 وەک خۆی 1ـە؛ 1⊕1=01 \oplus 1 = 0؛ 1⊕1=01 \oplus 1 = 0؛ 1⊕0=11 \oplus 0 = 1؛ 0⊕1=10 \oplus 1 = 1 ← 10011.

Quick checkپشکنینی خێراWhat is binary 0110 (6) in Gray code? 0110 ⁦(6)⁩ بە چییە؟

Gray code to binaryلە کۆدی گرەیەوە بۆ دووانی

  1. The leftmost binary bit is the same as the leftmost Gray bit.

    لای چەپترین هەمان لای چەپترین .

  2. Moving right, XOR the binary bit you just made with the next Gray bit. The result is the next binary bit.

    بەرەو ڕاست، ئەو ئێستا دروستت کرد لەگەڵ دواتری XOR بکە. ئەنجامەکە دواتری .

b1=g1,bi=bi−1⊕gi    (i=2,3,… )b_1 = g_1,\quad b_i = b_{i-1} \oplus g_i \;\;(i = 2, 3, \dots)

For 5 bits: b2=b1⊕g2b_2 = b_1 \oplus g_2, b3=b2⊕g3b_3 = b_2 \oplus g_3, b4=b3⊕g4b_4 = b_3 \oplus g_4, b5=b4⊕g5b_5 = b_4 \oplus g_5.

بۆ 5 : b2=b1⊕g2b_2 = b_1 \oplus g_2، b3=b2⊕g3b_3 = b_2 \oplus g_3، b4=b3⊕g4b_4 = b_3 \oplus g_4، b5=b4⊕g5b_5 = b_4 \oplus g_5.

Gray 1 0 0 1 1 on top, binary 1 1 1 0 1 below. g1 is copied down to b1; each later binary bit comes from an XOR circle fed by the Gray bit above it and, by a diagonal line, the binary bit just made on its left.کۆدی گرەی 1 0 0 1 1 لە سەرەوە و دووانی 1 1 1 0 1 لە خوارەوە. g1 ڕاستەوخۆ دەچێتە b1؛ هەر بیتێکی دواتری دووانی لە بازنەیەکی XORـەوە دێت کە بیتی کۆدی گرەی لە سەرەوەی و، بە هێڵێکی لار، ئەو بیتی دووانییەی تازە لە لای چەپی دروست بوو دەیخۆنە ناوی.
Gray 10011 → binary 11101: each new binary bit feeds the next XOR. ⁦10011⁩ ← ⁦11101⁩: هەر نوێی دەچێتە ناو XORـی دواتر.CêIT

Check: 1; 1⊕0=11 \oplus 0 = 1; 1⊕0=11 \oplus 0 = 1; 1⊕1=01 \oplus 1 = 0; 0⊕1=10 \oplus 1 = 1 → 11101. It is the number we started from on the previous screen.

پشکنین: 1؛ 1⊕0=11 \oplus 0 = 1؛ 1⊕0=11 \oplus 0 = 1؛ 1⊕1=01 \oplus 1 = 0؛ 0⊕1=10 \oplus 1 = 1 ← 11101. ئەمە ئەو ژمارەیەیە کە لە شاشەی پێشوودا لێیەوە دەستمان پێکرد.

Quick checkپشکنینی خێراGray 1111 → binary? 1111 ← ؟

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

ExerciseڕاهێنانBinary to Grayدووانی بۆ کۆدی گرەی

Convert to Gray code: (a) 101101 (b) 11000110 (c) 10110.

بیانگۆڕە بۆ : (أ) 101101 (ب) 11000110 (ج) 10110.

Show answerوەڵامەکە پیشان بدە
  • (a) 1;  1⊕0=1;  0⊕1=1;  1⊕1=0;  1⊕0=1;  0⊕1=11;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1 → 111011
  • (b) 1;  1⊕1=0;  1⊕0=1;  0⊕0=0;  0⊕0=0;  0⊕1=1;  1⊕1=0;  1⊕0=11;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1;\; 0 \oplus 0 = 0;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1 → 10100101
  • (c) 1;  1⊕0=1;  0⊕1=1;  1⊕1=0;  1⊕0=11;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1 → 11101
  • (أ) 1;  1⊕0=1;  0⊕1=1;  1⊕1=0;  1⊕0=1;  0⊕1=11;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1، کەواتە 111011
  • (ب) 1;  1⊕1=0;  1⊕0=1;  0⊕0=0;  0⊕0=0;  0⊕1=1;  1⊕1=0;  1⊕0=11;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1;\; 0 \oplus 0 = 0;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1، کەواتە 10100101
  • (ج) 1;  1⊕0=1;  0⊕1=1;  1⊕1=0;  1⊕0=11;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1، کەواتە 11101
ExerciseڕاهێنانGray to binaryکۆدی گرەی بۆ دووانی

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) 1;  1⊕1=0;  0⊕0=0;  0⊕1=1;  1⊕1=01;\; 1 \oplus 1 = 0;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0 → 10010
  • (b) 1;  1⊕0=1;  1⊕0=1;  1⊕1=0;  0⊕1=1;  1⊕1=01;\; 1 \oplus 0 = 1;\; 1 \oplus 0 = 1;\; 1 \oplus 1 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0 → 111010
  • (c) 1;  1⊕0=1;  1⊕1=0;  0⊕0=0;  0⊕1=1;  1⊕1=0;  0⊕1=1;  1⊕1=01;\; 1 \oplus 0 = 1;\; 1 \oplus 1 = 0;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0 → 11001010
  • (أ) 1;  1⊕1=0;  0⊕0=0;  0⊕1=1;  1⊕1=01;\; 1 \oplus 1 = 0;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0، کەواتە 10010
  • (ب) 1;  1⊕0=1;  1⊕0=1;  1⊕1=0;  0⊕1=1;  1⊕1=01;\; 1 \oplus 0 = 1;\; 1 \oplus 0 = 1;\; 1 \oplus 1 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0، کەواتە 111010
  • (ج) 1;  1⊕0=1;  1⊕1=0;  0⊕0=0;  0⊕1=1;  1⊕1=0;  0⊕1=1;  1⊕1=01;\; 1 \oplus 0 = 1;\; 1 \oplus 1 = 0;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0، کەواتە 11001010
ExerciseڕاهێنانExam question: 122 in one byteپرسیاری تاقیکردنەوە: ⁦122⁩ لە یەک بایتدا

Use 1 byte to convert (122)10(122)_{10} to Gray code.

بە یەک (122)10(122)_{10} بگۆڕە بۆ .

Show answerوەڵامەکە پیشان بدە
  1. 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پاشماوە
    122÷2122 \div 2610
    61÷261 \div 2301
    30÷230 \div 2150
    15÷215 \div 271
    7÷27 \div 231
    3÷23 \div 211
    1÷21 \div 201
  2. Fill one byteیەک بایت پڕ بکەرەوە

    One byte is 8 bits, so add a 0 on the left: 01111010.

    یەک 8 ، بۆیە 0ـێک بخە لای چەپ: 01111010.

  3. Binary to Grayدووانی بۆ کۆدی گرەی

    0;  0⊕1=1;  1⊕1=0;  1⊕1=0;  1⊕1=0;  1⊕0=1;  0⊕1=1;  1⊕0=10;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 1 \oplus 1 = 0;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1;\; 1 \oplus 0 = 1 → 01000111.

    0;  0⊕1=1;  1⊕1=0;  1⊕1=0;  1⊕1=0;  1⊕0=1;  0⊕1=1;  1⊕0=10;\; 0 \oplus 1 = 1;\; 1 \oplus 1 = 0;\; 1 \oplus 1 = 0;\; 1 \oplus 1 = 0;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1;\; 1 \oplus 0 = 1 ← 01000111.

ExerciseڕاهێنانThere and backڕۆیشتن و گەڕانەوە

(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 → 1;  1⊕0=1;  0⊕0=0;  0⊕1=11;\; 1 \oplus 0 = 1;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1 → 1101.
  • (b) 1101 → 1;  1⊕1=0;  0⊕0=0;  0⊕1=11;\; 1 \oplus 1 = 0;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1 → 1001 = 9. The two conversions undo each other.
  • (أ) 9 = 1001؛ پاشان 1;  1⊕0=1;  0⊕0=0;  0⊕1=11;\; 1 \oplus 0 = 1;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1، کەواتە 1101.
  • (ب) 1101 → 1;  1⊕1=0;  0⊕0=0;  0⊕1=11;\; 1 \oplus 1 = 0;\; 0 \oplus 0 = 0;\; 0 \oplus 1 = 1 → 1001 = 9. هەردوو گۆڕینەکە یەکتر هەڵدەوەشێننەوە.
Quick checkپشکنینی خێراBinary 11111111 in Gray code is… 11111111 بە دەبێتە…

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.
  • و پێوەرە هێڵییەکان: دوگمەی دەنگ و پلەی گەرمی، دەستی ، ئامێرەکانی کارگە، ئاراستەی با.
  • : ڕیز و ستوونەکانی ئەم نەخشانە بە ڕیزبەندیی ناونراون، بۆ ئەوەی خانە دراوسێکان تەنها لە یەک گۆڕاودا جیاواز بن. لە وانەی نەخشەکانی کارنۆ بۆ سادەکردنەوەی بەکاریان دەهێنیت.
  • گەیاندنی : ئاستە دراوسێکانی کۆدێک وەردەگرن کە تەنها لە یەک جیاوازە، بۆیە هەڵەیەکی بچووکی خوێندنەوە تەنها یەک تێک دەدات.
  • کە بە تر دەخوێنرێنەوە: کاتێک بەشێکی دەخوێنێتەوە کە بەشێکی تر دەیگۆڕێت، بە دڵنیا دەکاتەوە کە خوێندنەوەکە یان بەها کۆنەکەیە یان نوێیەکە.
Quick checkپشکنینی خێراWhy are Karnaugh-map rows labelled 00, 01, 11, 10 instead of 00, 01, 10, 11?بۆچی ڕیزەکانی بە ⁦00⁩، ⁦01⁩، ⁦11⁩، ⁦10⁩ ناونراون نەک ⁦00⁩، ⁦01⁩، ⁦10⁩، ⁦11⁩؟

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

MistakeهەڵەFixچارەسەر
Using ordinary addition instead of XOR (1+1=101 + 1 = 10).بەکارهێنانی کۆکردنەوەی ئاسایی لە جیاتی XOR (1+1=101 + 1 = 10).XOR has no carry: 1⊕1=01 \oplus 1 = 0.XOR هیچ نییە: 1⊕1=01 \oplus 1 = 0.
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 بکە.
Review questionsپرسیارەکانی پێداچوونەوە
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 → 1;  1⊕0=1;  0⊕1=11;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1).

111 ( 101 ← 1;  1⊕0=1;  0⊕1=11;\; 1 \oplus 0 = 1;\; 0 \oplus 1 = 1).

Videoڤیدیۆ

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

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

How To Convert Gray Code to Binary and Binary to Gray CodeThe Organic Chemistry Tutor · Englishئینگلیزی · 490K viewsبینین · 9.1K likesلایک

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زاراوە سەرەکییەکان

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

ASCII and Character CodesASCII و کۆدەکانی نووسە

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

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