Digital Electronics / Number Systems and Codes

Number Systems and Codes

Learn how information becomes binary data, how bases are converted, why complements simplify hardware arithmetic, and how codes represent numbers, characters, and transitions inside real digital systems.

Introduction

Number systems are methods of representing quantities using symbols and a base. Digital electronics uses binary because physical circuits can reliably distinguish two voltage regions: LOW and HIGH.

Codes are special binary patterns used to represent decimal digits, characters, instructions, sensor values, and state information. Without number systems and codes, gates and flip-flops would have no meaningful data to process.

Why This Topic Matters

  • Industry relevance: processors, memory, communication frames, ADC outputs, embedded firmware, and digital instruments all store and move data in binary form.
  • Hardware relevance: adders, ALUs, counters, registers, decoders, and display drivers depend on binary arithmetic and coding rules.
  • Exam relevance: GATE and PSU questions frequently test base conversion, complements, BCD, Gray code, and overflow interpretation.
  • Interview relevance: strong candidates can explain why binary is used physically, not just how to convert numbers mechanically.

Prerequisites

  • Basic place-value arithmetic
  • Powers of 2, 8, 10, and 16
  • Simple addition and subtraction
  • Meaning of voltage levels LOW and HIGH
  • Basic idea of bits and digital storage
  • Comfort with repeated division and weighted sums

Basic Intuition

Think of a number system as a language for counting. Decimal uses ten symbols because humans traditionally count with ten fingers. Binary uses two symbols because electronic switches are naturally stable in two practical states: OFF and ON.

Binary is not chosen because it is short. It is chosen because it is electrically reliable.

Octal and hexadecimal are compact human-friendly ways to write binary. Engineers use hexadecimal because four binary bits map exactly to one hex digit, making machine data easier to read.

Core Theory Explanation

1. Positional Number Systems

In a positional system, the value of a digit depends on both the digit and its position. The rightmost position has weight base raised to zero. Moving left increases the power of the base.

For example, decimal 527 means 5 hundreds, 2 tens, and 7 ones. Binary 1011 means 1 eight, 0 fours, 1 two, and 1 one.

2. Binary Number System

Binary uses only 0 and 1. Each binary digit is called a bit. A group of bits can represent numbers, characters, machine instructions, addresses, or logic states.

3. Octal and Hexadecimal Systems

Octal groups binary bits in sets of three. Hexadecimal groups binary bits in sets of four. This grouping is useful because long binary numbers are hard for humans to read and easy to copy incorrectly.

4. Codes

A code is not always a normal number. BCD stores each decimal digit using four bits. Gray code changes only one bit between adjacent values. ASCII maps characters to binary patterns. Excess-3 is a self-complementing code used in decimal arithmetic concepts.

Step-by-Step Mathematical Derivation

1. General Positional Value

Suppose a number has digits from left to right. Each digit is multiplied by a power of the base. For base $$ r $$:

$$ (a_n a_{n-1} ... a_1 a_0)_r = a_n r^n + a_{n-1} r^{n-1} + ... + a_1 r + a_0 $$

Physical meaning: position gives weight. A bit in a higher position has greater electrical meaning because it contributes a larger numerical value to the binary word.

2. Binary to Decimal

For $$ (101101)_2 $$:

$$ 1\times2^5 + 0\times2^4 + 1\times2^3 + 1\times2^2 + 0\times2^1 + 1\times2^0 $$

$$ = 32 + 0 + 8 + 4 + 0 + 1 = 45 $$

3. Decimal to Binary

Repeatedly divide by 2 and record remainders. The remainders show which powers of 2 are needed to build the decimal number.

$$ 45_{10} = 101101_2 $$

4. Complements

One's complement flips every bit. Two's complement adds 1 to the one's complement. Hardware uses two's complement because subtraction becomes addition of a negative number.

$$ \text{2's complement} = \text{1's complement} + 1 $$

Working Principle

  1. Choose the required representation: binary, octal, decimal, hexadecimal, or code.
  2. Identify the base and allowed symbols.
  3. Use positional weights for direct value calculation.
  4. Use grouping for binary-octal or binary-hex conversion.
  5. Use complements when subtraction or signed representation is involved.
  6. Use codes when the binary pattern represents a decimal digit, character, or transition state instead of a pure binary number.

Diagram Explanation

Number Base Conversion Flow Diagram Here
Binary Weighted Position Diagram Here
BCD Code Table Here
Gray Code Transition Diagram Here

Important Formulas

General positional expansion

$$ N = \sum a_i r^i $$

Here $$ r $$ is the base and $$ a_i $$ is the digit at position $$ i $$. It explains why the same digit has different value at different positions.

Binary place values

$$ 2^0, 2^1, 2^2, 2^3, ... $$

Binary weights are powers of 2. A 1 at a position means that weight is included; a 0 means it is absent.

One's complement

$$ 0 \leftrightarrow 1 $$

Every bit is inverted. This is easy in hardware but has two zero representations, which is why two's complement is preferred.

Two's complement

$$ \text{2's comp.} = \text{1's comp.} + 1 $$

Two's complement gives one unique zero and makes subtraction compatible with binary addition circuits.

BCD digit range

$$ 0000 \text{ to } 1001 $$

BCD uses four bits for each decimal digit from 0 to 9. Patterns 1010 to 1111 are invalid for a single BCD digit.

Binary to Gray

$$ G = B \oplus (B >> 1) $$

Gray code is formed so adjacent values differ by one bit, reducing transition errors in encoders.

Real-World Applications

  • Microprocessor registers, addresses, and instruction words
  • Memory locations and hexadecimal debugging
  • ADC output representation in embedded systems
  • Digital display systems using BCD
  • Rotary encoders and position sensors using Gray code
  • Serial communication data frames using ASCII or binary codes
  • ALU subtraction using two's complement
  • Digital instruments, counters, calculators, and control panels

Solved Examples

Beginner Example

Convert $$ (1101)_2 $$ to decimal.

$$ 1\times2^3 + 1\times2^2 + 0\times2^1 + 1\times2^0 = 8+4+0+1=13 $$

So, $$ (1101)_2 = (13)_{10} $$.

Intermediate Numerical

Convert $$ (156)_{10} $$ to hexadecimal.

Divide by 16: $$ 156 = 9\times16 + 12 $$.

The remainder 12 is written as C in hexadecimal.

Therefore, $$ (156)_{10} = (9C)_{16} $$.

Advanced Problem

Find the 8-bit two's complement representation of $$ -37 $$.

First write $$ +37 $$ in 8-bit binary: $$ 00100101 $$.

Take one's complement: $$ 11011010 $$.

Add 1: $$ 11011011 $$.

So, $$ -37 $$ in 8-bit two's complement is $$ 11011011 $$.

Common Mistakes

  • Reading a binary number like a decimal number instead of using powers of 2.
  • Forgetting to reverse remainders during decimal-to-binary conversion.
  • Using BCD as if it were pure binary. BCD 1001 is 9, but 1010 is invalid for one digit.
  • Confusing one's complement and two's complement.
  • Forgetting fixed word length while representing negative numbers.
  • Assuming Gray code is used for arithmetic. It is mainly used for safer transitions.

Comparison Tables

SystemBaseSymbolsEngineering Use
Binary20, 1Logic circuits and storage
Octal80 to 7Compact binary grouping
Decimal100 to 9Human-readable values
Hexadecimal160 to 9, A to FMemory, machine code, debugging
CodeMain IdeaTypical Use
BCDEach decimal digit uses 4 bitsDisplays and decimal arithmetic
GrayAdjacent values differ by one bitEncoders and transition-safe counting
ASCIIBinary code for charactersText and communication
Excess-3Decimal digit plus 3 in binarySelf-complementing code concepts

Interview Questions

  • Why do digital circuits use binary instead of decimal internally?
  • Why is hexadecimal preferred while reading machine-level data?
  • What is the difference between BCD and pure binary?
  • Why is two's complement preferred for signed arithmetic?
  • What is the advantage of Gray code in rotary encoders?
  • What happens if a BCD digit becomes 1010?
  • How does word length affect signed-number representation?

Exam-Oriented Notes

  • For binary to decimal, write powers of 2 above each bit before calculating.
  • For binary to hexadecimal, group bits in fours from the right side.
  • For binary to octal, group bits in threes from the right side.
  • In BCD, each decimal digit is converted separately.
  • For two's complement, always confirm the word length before solving.
  • Gray code questions often check one-bit transition behavior, not arithmetic ability.

Revision Summary

  • Digital circuits use binary because two voltage states are reliable and noise-tolerant.
  • Every positional number system uses digit weight based on powers of its base.
  • Octal and hexadecimal are compact forms of binary representation.
  • One's complement flips bits; two's complement flips bits and adds 1.
  • BCD represents decimal digits; Gray code reduces transition error.
  • Key formula: $$ N = \sum a_i r^i $$.

Practice Questions

Conceptual

  • Explain why binary representation is suitable for electronic circuits.
  • Differentiate between BCD and hexadecimal representation.
  • Why does Gray code reduce transition errors?

Numerical

  • Convert $$ (10101101)_2 $$ to decimal and hexadecimal.
  • Convert $$ (247)_{10} $$ to binary.
  • Find the 8-bit two's complement form of $$ -58 $$.

MCQs

  • Which base is used in hexadecimal number system: 8, 10, 16, or 2?
  • How many bits are used to represent one BCD digit?
  • Which code changes only one bit between adjacent values?