Introduction
Welcome back! In our previous lesson, we delved into 2's complement arithmetic, focusing on how computers perform addition and subtraction efficiently and how to detect overflow—a critical concept for your GATE preparation. Those codes are designed for computation.
Today, we shift our focus from arithmetic codes to special-purpose codes used for data representation, interfacing, and error prevention. Our learning outcome is to represent numbers in BCD, Gray, and Excess-3 codes and perform conversions between them.
You'll learn why a simple seven-segment display on a digital watch uses BCD, how Gray code prevents errors in mechanical sensors, and how Excess-3 code can simplify certain arithmetic operations. Mastering these conversions is essential for scoring well on number system questions in the GATE exam.
1. Binary Coded Decimal (BCD)
While binary is the natural language of computers, humans think in decimal. Binary Coded Decimal (BCD) bridges this gap by representing each decimal digit (0-9) with a separate 4-bit binary number.
The most common form is the 8421 BCD code, where the name indicates the weights of the four bit positions (2³, 2², 2¹, 2⁰).
To understand the fundamentals of BCD and how to perform basic conversions from decimal, let's start with a video.
BCD Codes (Binary Coded Decimal Codes) Explained
This video from ALL ABOUT ELECTRONICS provides a clear introduction to what BCD is and how to convert decimal numbers into BCD.
Please watch the first three minutes of the video: What is BCD? (0:19 - 1:52): Pay attention to the concept of representing each decimal digit with a 4-bit code and the 8421 weighting. Decimal to BCD Conversion (1:52 - 3:06): Observe how the decimal number 56.79 is converted by replacing each digit with its 4-bit BCD equivalent.
As you saw, the conversion from decimal to BCD is a direct substitution.
For example, to convert to BCD:
2becomes00104becomes01009becomes1001
So, .
Important Note: The 4-bit combinations from 1010 (10) to 1111 (15) are invalid in 8421 BCD because they don't correspond to any single decimal digit.
Converting from Other Bases to BCD
What if the number isn't in decimal to begin with? The most reliable method is a two-step process:
- Convert the number from its given base (e.g., binary, hexadecimal) to decimal.
- Convert the resulting decimal number to BCD.
This prevents a common mistake of trying to group binary bits directly into BCD.
Digital Electronics - Code Conversion
The article from TutorialsPoint provides a clear, step-by-step example of converting a binary number to BCD, reinforcing this two-step process.
Read the section titled Binary to BCD Code Conversion. Notice how the binary number (100111)₂ is first converted to (39)₁₀ and then to (0011 1001)BCD.
The reverse process, BCD to Binary, follows the same logic: convert BCD to decimal first, then decimal to binary. You can review this in the subsequent section of the same TutorialsPoint article, "BCD to Binary Code Conversion."
2. Excess-3 (XS-3) Code
Excess-3 (XS-3) code is another 4-bit code used to represent decimal digits. It is derived directly from BCD by adding 3 (binary 0011) to each BCD code.
XS-3 code = BCD code + 0011
Key Properties of Excess-3 Code:
- Non-weighted: Unlike 8421 BCD, the bit positions in XS-3 do not have fixed weights.
- Self-complementing: This is its most significant advantage. The 9's complement of a decimal digit can be obtained by simply inverting the bits (0s to 1s and 1s to 0s) of its corresponding XS-3 code. This property simplifies subtraction logic in hardware.
Let's watch a video that explains these properties and the conversion process.
BCD Codes (Binary Coded Decimal Codes) Explained
We'll return to the ALL ABOUT ELECTRONICS video, which has excellent segments on Excess-3 code.
Please watch the following clips: Excess-3 Explained (10:30 - 11:44): This part introduces XS-3 as a non-weighted, sequential, and self-complementing code. Focus on the explanation of the self-complementing property. Decimal to Excess-3 Conversion (11:44 - 12:36): Observe the two-step process: Decimal -> BCD -> Excess-3.
Example of Self-Complementing Property:
- Decimal digit:
2 - BCD code:
0010 - XS-3 code for 2:
0010 + 0011 = 0101
Now, let's find the code for its 9's complement, which is 7.
- Decimal digit:
7 - BCD code:
0111 - XS-3 code for 7:
0111 + 0011 = 1010
Notice that 1010 is the bit-wise complement of 0101. This holds for all digits (0 and 9, 1 and 8, etc.), which is a powerful feature for arithmetic circuit design.
The conversion from XS-3 to BCD is simply the reverse process: subtract 0011 from each 4-bit XS-3 group.
3. Gray Code
Gray code is a unique binary code distinguished by a single, powerful property: only one bit changes between any two successive code words. This is why it's also called a unit-distance code.
This property is extremely valuable in systems where errors can be introduced during transitions between states, such as in electromechanical switches or position encoders. A change of multiple bits at once in standard binary could be misread during the transition, but Gray code minimizes this risk.
Binary-to-Gray Conversion
The conversion from a binary number to Gray code follows a simple set of rules involving the XOR (Exclusive OR) operation.
Let the binary number be and the Gray code be .
- The Most Significant Bit (MSB) of the Gray code is the same as the MSB of the binary number:
- For all other bits, the Gray code bit is the XOR of the corresponding binary bit and the binary bit to its left (the next more significant bit):
Gray-to-Binary Conversion
The conversion back to binary is slightly different but also uses the XOR operation.
- The MSB of the binary number is the same as the MSB of the Gray code:
- For all other bits, the binary bit is the XOR of the corresponding Gray code bit and the already calculated binary bit to its left:
Let's watch a video that demonstrates these conversion processes and the motivation behind Gray code.
Gray Code Explained | Gray code to Binary and Binary to Gray code Conversion
This video, also from ALL ABOUT ELECTRONICS, is dedicated entirely to Gray code and explains its properties and conversion methods very clearly.
Watch the following sections: Introduction to Gray Code (0:29 - 2:03): Understand the 'unit distance' property by comparing Gray code with standard binary. Application Example (2:03 - 5:37): The position encoder example perfectly illustrates why Gray code is used to prevent errors. This provides excellent context. Binary to Gray Conversion (5:37 - 9:16): Follow the step-by-step XOR process. Gray to Binary Conversion (9:16 - 12:51): Carefully observe how the previously calculated binary bit is used in the XOR operation for the next bit.
Here is a static diagram summarizing the Gray-to-Binary conversion process, which you may find helpful for quick reference.

Summary and Practice
Let's summarize the key characteristics of these three codes. Understanding their differences is crucial for GATE questions that ask you to "identify the correct statement."
| Property | 8421 BCD | Excess-3 (XS-3) | Gray Code |
|---|---|---|---|
| Type | Weighted | Non-weighted | Non-weighted |
| Self-complementing? | No | Yes | No |
| Unit Distance? | No | No | Yes |
| Primary Use | Interfacing with decimal I/O (displays, keyboards) | Arithmetic operations (subtraction) | Error minimization (encoders, K-maps) |
Practice Problem
Let's tackle a typical GATE-style question that combines these concepts.
Question: The decimal number is 53.
- Find its representation in BCD and Excess-3 code.
- Find its representation in binary, and then convert the binary representation to Gray code.
Solution Walkthrough:
-
BCD and Excess-3 Representation:
- First, represent
53in BCD by converting each digit:5->01013->0011- BCD:
(0101 0011)_{BCD}
- Next, find the Excess-3 code by adding
0011to each BCD nibble:0101(for 5) +0011=10000011(for 3) +0011=0110- Excess-3:
(1000 0110)_{XS-3}
- First, represent
-
Binary and Gray Code Representation:
- First, convert the decimal number
53to pure binary:- Binary:
- Now, convert to Gray code:
- Gray Code:
- First, convert the decimal number
Conclusion
In this lesson, you've learned about three fundamental codes used in digital electronics beyond standard binary. You can now represent numbers in these formats and confidently convert between them.
Key Takeaways:
- BCD (8421): A weighted code that represents each decimal digit with 4 bits. Essential for human-machine interfaces.
- Excess-3: A non-weighted, self-complementing code derived from BCD. Its self-complementing nature simplifies arithmetic circuits.
- Gray Code: A non-weighted, unit-distance code where only one bit changes between successive values. It's used to prevent transitional errors in digital systems.
- Conversion Strategy: For conversions involving BCD or XS-3 with bases other than decimal, always use decimal as an intermediate step. For Gray code, use the direct XOR-based rules for converting to and from binary.
Next Lesson Preview:
You have now covered all the fundamental number representation schemes in this module. The final lesson will be a practice-oriented session where you will solve a variety of GATE-style problems involving number representation, complements, and binary arithmetic. This will consolidate everything you've learned and prepare you for the exam.