Binary Codes & Representations

Learning Objectives

By the end of this lecture, you will be able to:

  • Distinguish between signed and unsigned number representations

  • Understand and apply 1’s complement and 2’s complement arithmetic

  • Convert numbers to and from BCD (Binary Coded Decimal)

  • Work with Gray code and understand its applications

  • Encode and decode ASCII characters

  • Choose appropriate binary representation for different applications

Introduction to Binary Representations

Why Different Binary Representations?

  • Different applications need different representations

    • Arithmetic operations \(\to\) 2’s complement

    • Display systems \(\to\) BCD

    • Position sensing \(\to\) Gray code

    • Text processing \(\to\) ASCII

  • Trade-offs to consider:

    • Storage efficiency vs. ease of conversion

    • Arithmetic simplicity vs. error reduction

    • Hardware complexity vs. software complexity

Signed & Unsigned Numbers

Unsigned Numbers

  • Definition: Represent only non-negative integers (0, 1, 2, 3, ...)

  • Range for n-bit: 0 to \((2^n - 1)\)

4-bit Binary Decimal 8-bit Binary
0000 0 00000000
0001 1 00000001
0010 2 00000010
... ... ...
1111 15 00001111

Example: \(1010_2 = 1×2^3 + 0×2^2 + 1×2^1 + 0×2^0 = 8 + 2 = 10_{10}\)

Signed Numbers: The Challenge

  • Problem: How to represent negative numbers in binary?

  • Solution approaches:

    1. Sign-Magnitude representation

    2. 1’s Complement

    3. 2’s Complement (Most widely used)

Key Consideration: We need to represent both positive and negative numbers using only 0s and 1s, while keeping arithmetic operations simple.

Sign-Magnitude Representation

  • Method: Use leftmost bit as sign bit (0=positive, 1=negative)

  • Range for n-bit: \(-(2^{n-1}-1)\) to \(+(2^{n-1}-1)\)

4-bit Binary Decimal Notes
0000 +0 Positive zero
0001 +1
0111 +7 Largest positive
1000 -0 Negative zero!
1001 -1
1111 -7 Most negative

Problems: Two representations for zero, complex arithmetic

1’s Complement

1’s Complement Representation

  • Method: Invert all bits of positive number to get negative

  • Range for n-bit: \(-(2^{n-1}-1)\) to \(+(2^{n-1}-1)\)

Example: Representing -5 in 4-bit 1’s complement

\[\begin{aligned} +5_{10} &= 0101_2 \\ -5_{10} &= \overline{0101_2} = 1010_2 \text{ (invert all bits)} \end{aligned}\]

Decimal 4-bit 1’s Comp Decimal 4-bit 1’s Comp
+7 0111 -0 1111
+1 0001 -1 1110
+0 0000 -7 1000

Issue: Still has two zeros (0000 and 1111)

2’s Complement

2’s Complement Representation

  • Method: 1’s complement + 1

  • Range for n-bit: \(-2^{n-1}\) to \(+(2^{n-1}-1)\)

  • Advantages: Single zero, simple arithmetic

Example: Representing -5 in 4-bit 2’s complement

\[\begin{aligned} +5_{10} &= 0101_2 \\ \text{1's complement} &= 1010_2 \\ -5_{10} &= 1010_2 + 1 = 1011_2 \end{aligned}\]

Quick Method:

  1. Start from right, copy bits until first 1 (inclusive)

  2. Invert remaining bits

Example: \(0101 \rightarrow 1011\) (copy 01, invert 01 \(\to\) 10)

2’s Complement: Complete 4-bit Table

Decimal 4-bit 2’s Comp Decimal 4-bit 2’s Comp
+7 0111 -1 1111
+6 0110 -2 1110
+5 0101 -3 1101
+4 0100 -4 1100
+3 0011 -5 1011
+2 0010 -6 1010
+1 0001 -7 1001
0 0000 -8 1000

Note: Range is -8 to +7 (16 different values, one zero)

2’s Complement Arithmetic

Addition: Simply add binary numbers, ignore final carry

Example 1: \(3 + 5 = 8\)

\[\begin{aligned} 3_{10} &= 0011_2 \\ 5_{10} &= 0101_2 \\ \hline 8_{10} &= 1000_2 \end{aligned}\]

Example 2: \(3 + (-5) = -2\)

\[\begin{aligned} 3_{10} &= 0011_2 \\ -5_{10} &= 1011_2 \\ \hline -2_{10} &= 1110_2 \text{ (ignore carry)} \end{aligned}\]

Subtraction: Convert to addition: \(A - B = A + (-B)\)

Overflow in 2’s Complement

Overflow occurs when:

  • Positive + Positive = Negative result

  • Negative + Negative = Positive result

Example: \(7 + 2 = ?\) (in 4-bit)

\[\begin{aligned} 7_{10} &= 0111_2 \\ 2_{10} &= 0010_2 \\ \hline &= 1001_2 = -7_{10} \text{ {\color{darkred}(Overflow!)}} \end{aligned}\]

Detection: Check if sign of result differs from expected

BCD (Binary Coded Decimal)

BCD (Binary Coded Decimal)

  • Concept: Each decimal digit encoded separately in 4 bits

  • Range per digit: 0000 (0) to 1001 (9)

  • Unused codes: 1010, 1011, 1100, 1101, 1110, 1111

Decimal BCD Decimal BCD
0 0000 5 0101
1 0001 6 0110
2 0010 7 0111
3 0011 8 1000
4 0100 9 1001

Example: \(59_{10} = 0101 \, 1001_{\text{BCD}}\)

BCD vs Pure Binary

Example: Representing 123

Method Representation
Pure Binary \(123_{10} = 01111011_2\) (8 bits)
BCD \(123_{10} = 0001 \, 0010 \, 0011_{\text{BCD}}\) (12 bits)

Comparison:

  • BCD Advantages: Easy decimal conversion, used in displays

  • BCD Disadvantages: 25% storage waste, complex arithmetic

Applications: Digital clocks, calculators, meters, displays

BCD Arithmetic Example

Addition: Add BCD digits, adjust if result > 9

Example: \(27 + 35\) in BCD

\[\begin{aligned} 27_{10} &= 0010 \, 0111_{\text{BCD}} \\ 35_{10} &= 0011 \, 0101_{\text{BCD}} \\ \hline \text{Sum} &= 0101 \, 1100_{\text{BCD}} \end{aligned}\]

Correction needed: \(1100 > 1001\), so add 6 (0110)

\[\begin{aligned} 1100 + 0110 &= 10010 \\ \text{Final result} &= 0110 \, 0010_{\text{BCD}} = 62_{10} \end{aligned}\]

Gray Code

Gray Code Introduction

  • Property: Adjacent numbers differ by exactly one bit

  • Also known as: Reflected Binary Code, Unit Distance Code

  • Key advantage: Minimizes errors during transitions

3-bit Gray Code Sequence:

Decimal Binary Gray Code
0 000 000
1 001 001
2 010 011
3 011 010
4 100 110
5 101 111
6 110 101
7 111 100

Note: Each row differs from the next by only one bit!

Gray Code Construction

Method: Reflection technique

  1. Start with 1-bit: 0, 1

  2. For n+1 bits: Reflect n-bit sequence and prefix with 0 and 1

Example: Building 3-bit Gray code

\[\begin{aligned} \text{1-bit:} &\quad 0, 1 \\ \text{2-bit:} &\quad 00, 01, 11, 10 \\ \text{3-bit:} &\quad 000, 001, 011, 010, 110, 111, 101, 100 \end{aligned}\]

Pattern:

  • First half: prefix 0 to n-bit sequence

  • Second half: prefix 1 to reversed n-bit sequence

Binary to Gray Code Conversion

Algorithm:

  1. MSB of Gray = MSB of Binary

  2. For other bits: \(G_i = B_{i+1} \oplus B_i\)

Example: Convert \(1011_2\) to Gray

\[\begin{aligned} G_3 &= B_3 = 1 \\ G_2 &= B_3 \oplus B_2 = 1 \oplus 0 = 1 \\ G_1 &= B_2 \oplus B_1 = 0 \oplus 1 = 1 \\ G_0 &= B_1 \oplus B_0 = 1 \oplus 1 = 0 \end{aligned}\]

Therefore: \(1011_2 = 1110_{\text{Gray}}\)

Gray to Binary Code Conversion

Algorithm:

  1. MSB of Binary = MSB of Gray

  2. For other bits: \(B_i = G_i \oplus B_{i+1}\)

Example: Convert \(1110_{\text{Gray}}\) to Binary

\[\begin{aligned} B_3 &= G_3 = 1 \\ B_2 &= G_2 \oplus B_3 = 1 \oplus 1 = 0 \\ B_1 &= G_1 \oplus B_2 = 1 \oplus 0 = 1 \\ B_0 &= G_0 \oplus B_1 = 0 \oplus 1 = 1 \end{aligned}\]

Therefore: \(1110_{\text{Gray}} = 1011_2\)

Gray Code Applications

  • Rotary Encoders: Shaft position sensing

    • Prevents false readings during transitions

    • Critical in precision control systems

  • A/D Converters: Reduces conversion errors

  • Communication Systems: Error reduction in digital transmission

  • Karnaugh Maps: Logic simplification (next lectures)

Why Gray Code for Encoders?

  • Binary: 011 \(\to\) 100 (3 bits change simultaneously)

  • Gray: 010 \(\to\) 110 (only 1 bit changes)

  • Mechanical tolerances ensure only one sensor changes state

ASCII

ASCII (American Standard Code for Information Interchange)

  • Purpose: Standard for representing text characters

  • Original: 7-bit code (128 characters, 0-127)

  • Extended: 8-bit with parity or extended characters

  • Characters include: Letters, digits, symbols, control characters

Character Categories:

  • Control characters (0-31): Non-printable (CR, LF, TAB, etc.)

  • Printable characters (32-126): Letters, digits, symbols

  • DEL character (127): Delete

ASCII Table (Partial)

Char Dec Hex Binary Char Dec
Space 32 20 0100000 @ 64
! 33 21 0100001 A 65
" 34 22 0100010 B 66
# 35 23 0100011 C 67
... ... ... ... ... ...
0 48 30 0110000 96
1 49 31 0110001 a 97
2 50 32 0110010 b 98
... ... ... ... ... ...
9 57 39 0111001 z 122

Key Patterns:

  • Digits 0-9: ASCII 48-57 (add 48 to digit value)

  • Upper case A-Z: ASCII 65-90

  • Lower case a-z: ASCII 97-122 (upper case + 32)

ASCII Examples and Patterns

Example 1: Encode "Hi!" in ASCII

\[\begin{aligned} \text{H} &= 72_{10} = 01001000_2 \\ \text{i} &= 105_{10} = 01101001_2 \\ \text{!} &= 33_{10} = 00100001_2 \end{aligned}\]

Useful Patterns:

  • Case conversion: Toggle bit 5

    • ’A’ (01000001) \(\leftrightarrow\) ’a’ (01100001)

  • Digit to number: Subtract 48

    • ’7’ (55) - 48 = 7

  • Alphabetical order: Preserved in ASCII values

Applications: Text files, keyboards, displays, communication protocols

Comparison & Applications

When to Use Which Representation?

Representation Best For Key Advantages
Unsigned Binary Counters, addresses, positive-only data Simple, maximum range
2’s Complement Arithmetic operations, processors Simple arithmetic, single zero
BCD Displays, human interfaces Easy decimal conversion
Gray Code Position sensing, A/D conversion Error reduction, single-bit changes
ASCII Text processing, communication Standard, human-readable

System Design Considerations:

  • Storage requirements vs. processing speed

  • Error tolerance vs. computational complexity

  • Human interface requirements

Practice Problems

Problem 1: Convert the following:

  1. \(-13_{10}\) to 8-bit 2’s complement

  2. \(247_{10}\) to BCD

  3. \(1101_2\) to Gray code

  4. "ACE" to ASCII (in hex)

Problem 2: Identify the representation:

  • A system needs to display decimal numbers on 7-segment displays

  • A rotary encoder must track shaft position accurately

  • A processor performs signed integer arithmetic

  • A communication system transmits text messages

Summary

Key Takeaways:

  • Signed Numbers: 2’s complement is most practical for arithmetic

  • BCD: Trades storage efficiency for easy decimal interfacing

  • Gray Code: Single-bit transitions prevent errors in analog systems

  • ASCII: Universal standard for text representation

  • Choice depends on: Application requirements, hardware constraints, error tolerance

Next Lecture: Boolean Algebra & Logic Simplification

  • Fundamental theorems and postulates

  • Karnaugh Maps (using Gray code!)

  • Logic minimization techniques

Next: Boolean Algebra & Logic Simplification