Binary Codes for UPSC ISS is the half of the number systems syllabus that nobody teaches separately, which is why candidates who have carefully practised conversions still lose these marks.
Quick answer. Codes and binary arithmetic have appeared 10 times in the last ten papers, an average of one question a year, or 2.5 marks. The questions cluster tightly around three things: which codes are weighted, how Gray code converts, and how negative numbers are stored in 2’s complement. Learn those three and you have the topic.
Statistics Paper I, Section IV. Syllabus item: number systems. Codes are not named separately in the syllabus, but ten questions in ten years say they belong here. With the conversions in Part 1, this module is worth 26 questions and about 6.5 marks a year.
A code is not a number system
This distinction is the entire topic, and it is where the marks are.
In a number system, the whole string carries one value. (1001)₂ is a single number, nine.
In a code, each decimal digit gets its own fixed binary pattern, and the string is a sequence of labels rather than one value. In 8421 BCD, 1001 is the label for the digit nine, and the number 91 is written 1001 0001, two separate four bit groups, not the binary for 91.
Every trap in this topic comes from that difference. A question showing a long binary string may be testing conversion, or it may be testing BCD. Look at whether the string splits cleanly into groups of four.
Weighted and non weighted codes
This is the most asked idea in the topic. It has come up in 2018, 2019 and 2020 in three different wordings.
A code is weighted if each bit position carries a fixed numeric weight, so you can find the digit by multiplying and adding. It is non weighted if the pattern is just an assigned label with no arithmetic meaning.
| Code | Weighted? | Weights | What it is |
|---|---|---|---|
| BCD, also called 8421 | Yes | 8, 4, 2, 1 | Each decimal digit in four bits |
| 8421 | Yes | 8, 4, 2, 1 | Another name for BCD, not a different code |
| 2421, 5211 | Yes | As named | Other weighted schemes |
| Excess-3 | No | None | BCD with 3 added to each digit |
| Gray code | No | None | Only one bit changes between neighbours |
| ASCII | No | None | A character set, not a numeric code |
Three traps in one table
BCD and 8421 are the same code. A question listing both as separate options is testing whether you know that. Both are weighted.
BCD is never a “1246” code. A 2018 statement claimed the weighted 4-bit BCD code is known as 1246 weighted code. The weights are 8, 4, 2, 1. The statement is false, and it has been reused.
ASCII is not a numeric code at all. It encodes characters, including letters and punctuation. It has no weights because there is nothing to weight. Whenever ASCII appears in a “which of these are weighted” list, it is the wrong answer.
Worked Example 1: BCD in both directions
Convert the decimal number 693 to 8421 BCD.
| Digit | Four bit pattern |
|---|---|
| 6 | 0110 |
| 9 | 1001 |
| 3 | 0011 |
Answer: 693 in BCD is 0110 1001 0011
Now the reverse. Convert 0100 0111 in BCD to decimal.
0100 is 4 and 0111 is 7, so the answer is 47.
Compare that with plain binary. 01000111 as a binary number is 71, not 47. Same bits, two completely different answers, and which one is correct depends entirely on whether the question said BCD. This is the most common way candidates lose this mark.
Worked Example 2: Excess-3
Excess-3 is BCD with 3 added to the decimal digit before encoding.
Convert 47 to Excess-3.
| Step | Working |
|---|---|
| 1. Take each digit | 4 and 7 |
| 2. Add 3 to each | 7 and 10 |
| 3. Write each result in four bits | 7 = 0111, 10 = 1010 |
Answer: 47 in Excess-3 is 0111 1010
Excess-3 is not weighted, because you cannot recover the digit by multiplying bit positions. You have to decode and then subtract 3. That is exactly why it fails the weighted test.
Worked Example 3: Binary to Gray code
Gray code is built so that only one bit changes between consecutive values. That property is why it is used in position encoders, and it is the reason the conversion works the way it does.
Rule. The first Gray bit is the first binary bit. Every bit after that is the XOR of the current binary bit and the one before it.
Convert binary 1011 to Gray code.
| Position | Working | Gray bit |
|---|---|---|
| 1 | Copy the first bit | 1 |
| 2 | 1 XOR 0 | 1 |
| 3 | 0 XOR 1 | 1 |
| 4 | 1 XOR 1 | 0 |
Answer: binary 1011 is Gray 1110
Worked Example 4: Gray code to binary
The reverse is not symmetric, and this is where candidates slip.
Rule. The first binary bit is the first Gray bit. Every bit after that is the XOR of the binary bit you just produced and the next Gray bit.
Convert Gray 1101 to binary.
| Position | Working | Binary bit |
|---|---|---|
| 1 | Copy the first bit | 1 |
| 2 | 1 XOR 1 | 0 |
| 3 | 0 XOR 0 | 0 |
| 4 | 0 XOR 1 | 1 |
Answer: Gray 1101 is binary 1001
Check it by converting back: 1001 to Gray gives 1, then 1 XOR 0 = 1, then 0 XOR 0 = 0, then 0 XOR 1 = 1, which is 1101.
The one thing to hold on to. Going into Gray you XOR two binary bits. Coming out of Gray you XOR your own previous answer with the next Gray bit. Forget this and you will produce a confident, clean, wrong string.
Representing negative numbers
Three methods exist. The papers have asked which one computers actually use, and why.
| Method | −5 in 4 bits | Problem |
|---|---|---|
| Signed magnitude | 1101 | Two representations of zero, and addition needs separate logic |
| 1’s complement | 1010 | Still two zeros, and needs an end around carry |
| 2’s complement | 1011 | One zero, and subtraction becomes ordinary addition |
2’s complement is the answer to both questions. It is the most popular method, and the reason is that it gives a single representation of zero and lets the same adder circuit perform subtraction. No extra hardware, no special case.
Worked Example 5: A negative number in 2’s complement
Represent −45 in an 8 bit word.
| Step | Working |
|---|---|
| 1. Write +45 in 8 bits | 00101101 |
| 2. Invert every bit, giving the 1’s complement | 11010010 |
| 3. Add 1 | 11010011 |
Answer: −45 in 8 bit 2’s complement is 11010011
Check it. In 2’s complement the leftmost bit carries a negative weight, so 11010011 is −128 + 64 + 16 + 2 + 1, which is −128 + 83, which is −45.
The range in 2’s complement
| Word size | Smallest | Largest |
|---|---|---|
| 8 bits | −128 | +127 |
| 16 bits | −32768 | +32767 |
| n bits | −2ⁿ⁻¹ | 2ⁿ⁻¹ − 1 |
The range is not symmetric. There is one more negative value than positive, because zero uses up one of the positive slots.
Worked Example 6: Bitwise operations
These operate on bits in matching positions, independently, with no carry.
Take A = 11010 and B = 10110.
| Operation | Rule | Result |
|---|---|---|
| A AND B | 1 only if both bits are 1 | 10010 |
| A OR B | 1 if either bit is 1 | 11110 |
| A XOR B | 1 only if the bits differ | 01100 |
A left shift by one multiplies by 2. A right shift by one divides by 2 and drops the remainder.
A reverse AND question has more than one answer, and that is the point. If a question asks which number ANDed with 101101 gives 100101, work position by position. Where the given number has a 1 and the result has a 1, the unknown must be 1. Where the given has a 1 and the result has 0, the unknown must be 0. Where the given has a 0, the unknown bit does not matter, so check which option satisfies only the positions that are forced.
How This Gets Asked
The same question, twice, three years apart [2022 and 2024]
The 2’s complement representation of the number −59 in an 8 bit word size computer system is [2022]
What is the representation of −59 in the 2’s complement system? [2024]
Same method. Same number. Reworded and asked again in 2024.
This is worth sitting with. It is the clearest evidence in the whole Computer section that the paper reuses its own material, and that working through past questions is not revision, it is preparation.
The weighted code question, in three costumes [2018, 2019, 2020]
Weighted 4-bit BCD code is known as 1246 weighted code [2018]
Which of the following codes is or are positional weighted codes? Gray code, BCD code, ASCII code, Excess-3 code [2019]
Which of the following are weighted codes? BCD code, GRAY code, ASCII code, 8421 code [2020]
Three papers, three formats, one fact. BCD and 8421 are the same weighted code with weights 8, 4, 2, 1. Gray, ASCII and Excess-3 are not weighted.
Learn one table and you have answered all three.
The Gray conversion [2021]
Binary equivalent of Gray coded number 10101110 will be
Gray to binary, so use the rule from Worked Example 4. Copy the first bit as 1, then XOR your running answer with each next Gray bit. The result is 11001011.
Eight bits, eight steps, under a minute once you have done it three times.
Quick Revision Table
| Point | Value |
|---|---|
| Weighted codes | BCD, 8421, 2421, 5211 |
| Non weighted codes | Gray, Excess-3, ASCII |
| BCD weights | 8, 4, 2, 1 |
| BCD and 8421 | The same code |
| Excess-3 | BCD with 3 added before encoding |
| Gray code property | Only one bit changes between neighbours |
| Binary to Gray | XOR each binary bit with the one before it |
| Gray to binary | XOR your previous answer with the next Gray bit |
| ASCII | Character set, not a numeric code |
| Negative numbers in computers | 2’s complement |
| Why 2’s complement | One zero, and subtraction uses the adder |
| 2’s complement steps | Invert all bits, then add 1 |
| 8 bit 2’s complement range | −128 to +127 |
| n bit 2’s complement range | −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1 |
| Leftmost bit in 2’s complement | Carries a negative weight |
Practice Set
Ten questions. No answer key below.
Write your answers in the comments, all ten in a single comment, in the format 1-b 2-a 3-d and so on. The full answer key is posted in this same thread after seven days.
A year in brackets means the question is from that paper. The rest are written in the same pattern.
- Consider signed magnitude, 1’s complement and 2’s complement representation of negative integers in computers. Generally 2’s complement is used because [2018]
- Which of the following are weighted codes? 1. BCD code 2. GRAY code 3. ASCII code 4. 8421 code [2020]
- Which is the most popular method used to represent negative numbers in the computer system? [2020]
- Which one of the following decimal numbers corresponds to the 8421 BCD code representation 010100100001.0110? [2020]
- Which one of the following binary numbers gives output as 100101 by performing an AND operation with the binary number 101101? [2021]
- Consider the following statements: I. The Excess-3 code for the decimal number 5 is 1000. II. Excess-3 is a weighted code. Which of the statements given above is or are correct?
- The Gray code equivalent of the binary number 1101 is
- A negative number is stored in an 8 bit word using 2’s complement. What is the largest positive integer that the same word can hold?
- Consider the following statements: I. In 8421 BCD, the decimal number 47 is written as 01000111. II. In plain binary, the same bit pattern 01000111 has the value 47. Which of the statements given above is or are correct?
- Which of the following are non weighted codes? 1. Excess-3 2. 8421 3. Gray code 4. ASCII
Question 9 is the one to slow down on. It is the entire difference between a code and a number system, written as a two statement question. If you can answer it without hesitating, you have this topic.
Frequently Asked Questions
How many questions come from binary codes in UPSC ISS?
Ten across the ten papers from 2017 to 2026, about one a year, worth 2.5 marks. Combined with the conversion questions in Part 1, the module is worth 26 questions.
What is the difference between BCD and binary?
In binary the whole string has one value. In BCD each decimal digit gets its own four bit group. The decimal number 47 is 101111 in binary but 0100 0111 in BCD, and the two are not interchangeable.
Is 8421 code the same as BCD?
Yes. 8421 refers to the weights of the four bit positions in standard BCD. Questions that list both as separate options are testing whether you know they are one code.
Why do computers use 2’s complement for negative numbers?
Because it gives a single representation of zero and lets the same adder circuit perform subtraction. Signed magnitude and 1’s complement both produce two zeros and need extra handling.
Is Gray code weighted?
No. Gray code has no positional weights. Its defining property is that only one bit changes between consecutive values, which is useful in position sensors but has no arithmetic meaning.
Is ASCII a weighted code?
No. ASCII is a character encoding standard that maps letters, digits and symbols to bit patterns. There are no positional weights because it is not encoding numeric value.
How do I remember the two Gray conversion directions?
Going into Gray, XOR two neighbouring binary bits. Coming out of Gray, XOR the binary bit you just produced with the next Gray bit. The second direction uses your own answer as it builds, which is the part people forget.
Related Reading
- Computer Application for UPSC ISS: Complete 16 Part Series
- Number Systems for UPSC ISS
- Inside the CPU for UPSC ISS
- UPSC ISS Previous Year Papers
Part 2 of 16 in the Computer Series for UPSC ISS. Previous: Number Systems for UPSC ISS. Next: Inside the CPU for UPSC ISS, published 5 October at 8:00 AM.