Overview
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 :
-
Sign-Magnitude representation
-
1’s Complement
-
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
| 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
Quick Method :
-
Start from right, copy bits until first 1 (inclusive)
-
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\)
Example 2 : \(3 + (-5) = -2\)
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)
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
Correction needed : \(1100 > 1001\) , so add 6 (0110)
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
-
Start with 1-bit: 0, 1
-
For n+1 bits: Reflect n-bit sequence and prefix with 0 and 1
Example : Building 3-bit Gray code
Pattern :
-
First half: prefix 0 to n-bit sequence
-
Second half: prefix 1 to reversed n-bit sequence
Binary to Gray Code Conversion
Algorithm :
-
MSB of Gray = MSB of Binary
-
For other bits: \(G_i = B_{i+1} \oplus B_i\)
Example : Convert \(1011_2\) to Gray
Therefore: \(1011_2 = 1110_{\text{Gray}}\)
Gray to Binary Code Conversion
Algorithm :
-
MSB of Binary = MSB of Gray
-
For other bits: \(B_i = G_i \oplus B_{i+1}\)
Example : Convert \(1110_{\text{Gray}}\) to Binary
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
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:
-
\(-13_{10}\) to 8-bit 2’s complement
-
\(247_{10}\) to BCD
-
\(1101_2\) to Gray code
-
"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