Digital Electronics and Logic Design

Chapter 2
Number Systems and Digital Codes

Diploma in Computer Architecture and Networking
Digital Electronics and Logic Design   •   Diploma

Number Systems and Digital Codes

Introduction

Digital systems represent information using discrete values. At the physical level, electronic circuits commonly distinguish two voltage ranges, interpreted as logic 0 and logic 1. This makes the binary number system fundamental to computers, network equipment, controllers, and communication devices. However, engineers also use decimal, octal, and hexadecimal notation because long binary strings are difficult to read.

Digital codes extend number systems to represent decimal digits, characters, symbols, and error-control information. A networking or computer-architecture student must therefore be able to convert between bases, perform signed arithmetic, interpret fixed-point values, and select suitable codes for storage and communication.

Learning Objectives

After completing this chapter, the learner should be able to:

  • explain positional number systems and radix notation;
  • convert values among decimal, binary, octal, and hexadecimal;
  • perform binary arithmetic and use complements;
  • represent signed integers and fixed-point fractions;
  • describe BCD, Gray code, ASCII, Unicode, and parity;
  • identify the purposes and limitations of basic error-detection codes;
  • apply these concepts to a practical digital-system problem.

1. Positional Number Systems

A positional number system uses a base, or radix, r. Each digit has a value determined by both its digit symbol and its position. For a number with integer digits dₙ…d₁d₀ and fractional digits d₋₁…d₋ₘ:

N = ∑(i = −m to n) dᵢrⁱ

Here, r is the base, dᵢ is a digit satisfying 0 ≤ dᵢ < r, and i is the positional exponent. The formula applies to ordinary positional notation; the radix must be fixed throughout the number.

For example, in decimal, 472 means 4×10² + 7×10¹ + 2×10⁰. In binary, 1011₂ means 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 11₁₀.

SystemBaseValid symbolsTypical use
Binary20, 1Electronic logic and machine data
Octal80–7Compact representation of binary
Decimal100–9Human calculations and display
Hexadecimal160–9, A–FAddresses, machine code, bit patterns

A subscript identifies the base. If no subscript is shown, decimal is normally assumed.

Binary Place Values

From right to left, binary integer positions have weights 1, 2, 4, 8, 16, and so on. Fractional positions have weights 1/2, 1/4, 1/8, and so on.

For example:

1101.101₂ = 8 + 4 + 0 + 1 + 0.5 + 0 + 0.125 = 13.625₁₀

2. Base Conversion

Binary, Octal, and Hexadecimal

Because 8 = 2³, each octal digit corresponds to exactly three binary bits. Because 16 = 2⁴, each hexadecimal digit corresponds to exactly four bits.

HexadecimalBinaryOctalBinary
000000000
100011001
200102010
300113011
401004100
501015101
601106110
701117111
81000——
91001——
A1010——
B1011——
C1100——
D1101——
E1110——
F1111——

To convert binary to hexadecimal, group bits in fours from the binary point outward, padding with zeros when necessary. Thus, 11010110₂ = D6₁₆. To convert binary to octal, group bits in threes: 11010110₂ = 326₈.

Decimal Integer to Another Base

Repeated division by the target base gives the digits in reverse order. Convert 156₁₀ to hexadecimal:

  • 156 ÷ 16 = 9 remainder 12 (C)
  • 9 ÷ 16 = 0 remainder 9

Therefore, 156₁₀ = 9C₁₆.

For binary, repeated division by 2 gives 156₁₀ = 10011100₂.

Decimal Fractions

Repeated multiplication by the target base gives fractional digits from left to right. Convert 0.625₁₀ to binary:

  • 0.625×2 = 1.25 → first digit 1
  • 0.25×2 = 0.5 → second digit 0
  • 0.5×2 = 1.0 → third digit 1

Therefore, 0.625₁₀ = 0.101₂. Some decimal fractions do not terminate in binary. For example, 0.1₁₀ produces a repeating binary expansion, so a finite computer representation is an approximation.

3. Binary Arithmetic

Binary addition follows four rules: 0+0=0, 0+1=1, 1+0=1, and 1+1=10₂. The latter produces a sum bit of 0 and a carry of 1. Binary subtraction uses borrowing in the same way as decimal subtraction.

Example:

101101₂
+ 001111₂
= 111100₂

The decimal values are 45 and 15, and their sum is 60, confirming the result.

Binary multiplication uses shifted partial products. A left shift by k positions multiplies an unsigned binary value by 2ᵏ, provided overflow does not occur. A right shift by k positions performs integer division by 2ᵏ for unsigned values, discarding any remainder.

Overflow occurs when a result cannot be represented using the available number of bits. For unsigned n-bit arithmetic, the representable range is:

0 ≤ N ≤ 2ⁿ − 1

For example, an 8-bit unsigned register cannot represent 256; adding 1 to 255 produces 00000000₂ with a carry-out.

4. Complements and Signed Arithmetic

Complements simplify subtraction by converting it into addition.

One’s and Two’s Complement

The one’s complement of a binary word is obtained by changing every 0 to 1 and every 1 to 0. The two’s complement is obtained by adding 1 to the one’s complement.

For 8 bits, the two’s complement of 00000101₂ (+5) is:

  • invert: 11111010₂;
  • add 1: 11111011₂.

This represents −5 in two’s-complement notation.

For n-bit two’s-complement signed integers, the range is:

−2ⁿ⁻¹ ≤ N ≤ 2ⁿ⁻¹ − 1

Thus, 8-bit two’s complement represents −128 through +127. There is only one representation of zero, unlike one’s complement, which has both positive and negative zero.

Two’s-Complement Subtraction

To calculate A − B, add A to the two’s complement of B and discard any carry beyond n bits.

Example using 8 bits:

13 − 5 = 00001101₂ + 11111011₂ = 1 00001000₂

Discarding the carry gives 00001000₂ = 8.

Signed overflow occurs when two operands with the same sign produce a result with the opposite sign. Adding a positive and a negative number cannot produce signed overflow. Hardware often detects overflow using sign-bit logic or the exclusive-OR of the carry into and out of the sign bit.

5. Fixed-Point Representation

Fixed-point notation assigns a permanent position to the binary point. If an unsigned word has I integer bits and F fractional bits, its scale factor is 2ᴳ? No—care must be taken: the scale factor is 2ᶠ, and the represented value is the stored integer divided by 2ᶠ.

Value = StoredInteger ÷ 2ᶠ

Here, F is the number of fractional bits. For a signed two’s-complement fixed-point word, the stored integer is interpreted as a signed two’s-complement integer before division. The formula assumes the binary point position is known and fixed.

Example: In unsigned Q4.4 notation, 1011.0100₂ equals 11 + 4/16 = 11.25. The smallest positive step is 2⁻⁴ = 0.0625, and the maximum value is 15.9375.

Fixed-point arithmetic is efficient in embedded systems because it avoids floating-point hardware. Its limitations include restricted range, quantization error, and possible overflow. Designers must choose the integer and fractional widths according to the expected signal range and required precision.

6. Decimal Codes

Binary-Coded Decimal

Binary-coded decimal (BCD) represents each decimal digit separately using four bits. Valid digit codes are 0000 through 1001; 1010 through 1111 are invalid in ordinary 8421 BCD.

Decimal 59 is represented as 0101 1001 in BCD, not as the pure binary representation 111011₂. BCD is useful in calculators, financial displays, and systems where decimal digits must be preserved exactly, but it uses more storage than pure binary.

BCD addition requires correction when a four-bit digit exceeds 1001 or produces a carry. The correction is adding 0110 to that digit. For example, 8 + 5 produces 1101, which is invalid BCD; adding 0110 gives 1 0011, representing decimal 13.

Excess-3 Code

Excess-3 represents a decimal digit by adding 3 to the digit and encoding the result in four-bit binary. For example, decimal 4 becomes 4+3=7, or 0111. It is a non-weighted code and is self-complementing, a property useful in some decimal arithmetic circuits.

7. Gray Code

Gray code is a non-weighted code in which adjacent values differ by exactly one bit. This reduces the chance of an erroneous intermediate reading when a mechanical or optical position changes.

To convert binary to Gray code:

  • copy the most significant binary bit;
  • each following Gray bit is the exclusive-OR of two adjacent binary bits.

For binary 1011:

  • first Gray bit = 1;
  • next = 1 XOR 0 = 1;
  • next = 0 XOR 1 = 1;
  • next = 1 XOR 1 = 0.

Thus, 1011₂ converts to 1110 Gray code. Gray code is common in rotary encoders and in some address-transition applications. It is not normally used for arithmetic because its bit patterns do not have ordinary positional weights.

8. Character Codes

ASCII

ASCII, the American Standard Code for Information Interchange, traditionally uses 7 bits and represents 128 codes. It includes control characters, decimal digits, uppercase and lowercase English letters, punctuation, and symbols. In many systems ASCII is stored in an 8-bit byte, with the extra bit used for extended encodings or parity, although “extended ASCII” is not one universally defined standard.

ASCII codes are not the same as numeric values. The character '5' has an ASCII code distinct from the integer value 5. This distinction is essential in keyboard input, file processing, and network protocols.

Unicode

Unicode assigns code points to characters from writing systems throughout the world, including mathematical symbols and emoji. Unicode Transformation Formats encode these code points for storage and transmission. UTF-8 uses one to four bytes and is compatible with 7-bit ASCII for basic English characters. UTF-16 commonly uses one or two 16-bit code units, while UTF-32 uses fixed 32-bit code units.

Unicode separates character identity from encoding. A program must also consider normalization, visual appearance, combining characters, and security issues such as confusable characters. Correct text handling is therefore a software and governance concern, not merely a matter of converting numbers.

9. Parity and Error Detection

Digital signals may be corrupted by noise, interference, faulty hardware, or transmission errors. Error-detection codes add redundancy so that a receiver can identify some corrupted data.

Parity Bit

In even parity, the parity bit is selected so that the total number of 1s is even. In odd parity, the total number is odd. For data 1011001, which contains four 1s, the even-parity bit is 0 and the odd-parity bit is 1.

A single parity bit detects every odd number of bit errors, but it cannot detect an even number of bit errors. It does not identify the erroneous bit and cannot correct an error by itself.

Two-Dimensional Parity

Data can be arranged in rows and columns, with parity calculated in both directions. This can detect many multi-bit errors and may locate a single-bit error at the intersection of a failed row and column. It remains less powerful than modern cyclic redundancy checks for many communication applications.

Checksum and CRC

A checksum combines data units using arithmetic or logical operations and transmits the resulting check value. Checksums are simple but their detection capability depends on the algorithm and error pattern.

A cyclic redundancy check (CRC) treats a bit string as a polynomial over GF(2), where addition is XOR. The sender divides the data polynomial, after appending zeros, by a generator polynomial and transmits the remainder with the data. The receiver repeats the division; a nonzero remainder indicates a likely error. CRCs are effective at detecting common burst errors, but they do not guarantee detection of every possible corruption and are not encryption or authentication mechanisms.

Error detection differs from error correction. Detection says that data is probably wrong; correction uses additional structure to determine and repair the wrong bit or requests retransmission. Reliable systems also need framing, synchronization, timeouts, and, where security matters, authentication codes.

Worked Application: A Sensor Network Packet

Consider an illustrative temperature-sensor node that sends a packet containing:

  • an 8-bit sensor reading;
  • a 4-bit status field;
  • an 8-bit sequence number;
  • a CRC check value.

The sensor reading is 27.5 °C, represented in unsigned Q8.4 fixed-point format. The stored integer is:

27.5 × 2⁴ = 440₁₀ = 000110111000₂

Because Q8.4 provides 8 integer bits and 4 fractional bits, the 12-bit representation is 00011011.1000₂. If the packet format allocates only 8 data bits for the reading, the designer must instead specify a different scale, such as half-degree units, or use multiple bytes. This illustrates why representation and field width must be designed together.

Suppose the status field is 0101₂ and the sequence number is 00101100₂. The fields are serialized in a documented order, and the CRC is calculated over the defined header and payload. At reception, the CRC is recomputed. If it differs, the packet is discarded or retransmitted. The sequence number helps detect lost or duplicated packets, while the CRC helps detect corruption. Neither field proves who sent the packet; authentication would be required for that purpose.

Learning Points from the Application

  1. A physical measurement must be mapped to a defined numerical scale.
  2. Bit width determines range and precision.
  3. Field ordering and endianness must be documented.
  4. Error detection improves reliability but does not provide confidentiality or authentication.
  5. Protocol designers must test boundary values, overflow, invalid codes, and interoperability.

Challenges, Limitations, and Best Practices

Common errors include confusing BCD with pure binary, forgetting leading zeros during grouping, interpreting a signed value as unsigned, and assuming that a parity bit can correct an error. Engineers should always state the base, word length, signedness, scaling factor, and byte order.

Best practices include:

  • use hexadecimal for compact bit-pattern documentation;
  • specify whether arithmetic is signed or unsigned;
  • test minimum, maximum, zero, and overflow values;
  • validate invalid BCD and reserved code patterns;
  • document character encoding explicitly, preferably using Unicode terminology such as UTF-8;
  • select CRC parameters and packet fields from a documented protocol specification;
  • distinguish reliability controls from security controls;
  • use automated tests to compare conversions and boundary behavior.

Contemporary Relevance

Modern processors often support binary integer and floating-point arithmetic, but fixed-point representation remains important in microcontrollers, digital signal processing, motor control, and low-power Internet-of-Things devices. Hexadecimal remains central to memory addresses, permissions, machine instructions, and debugging. Unicode is essential for globally usable software, while CRCs continue to protect data frames in storage, industrial networks, and communications systems.

Artificial-intelligence systems also depend on numerical representation. Quantized models may use 8-bit or lower-precision integers to reduce memory and energy consumption. Such systems require careful analysis of range, scale, rounding, saturation, and accumulated error. The underlying principles are the same as those used in fixed-point digital design.

Chapter Summary

Positional notation assigns each digit a value based on its radix and position. Binary is the natural language of digital circuits, while octal and hexadecimal provide compact forms of binary. Decimal-to-base conversion uses repeated division for integers and repeated multiplication for fractions.

Binary arithmetic supports addition, subtraction, shifting, and multiplication, but word length limits the range and may cause overflow. Two’s-complement representation provides an efficient signed-integer system with one zero and a range of −2ⁿ⁻¹ to 2ⁿ⁻¹−1. Fixed-point representation trades range and precision according to the location of an implied binary point.

BCD preserves decimal digits individually, Gray code minimizes changes between adjacent values, ASCII represents a limited character set, and Unicode supports global text through encodings such as UTF-8. Parity, checksums, and CRCs add redundancy for error detection, although they do not replace authentication or encryption. Correct digital design requires explicit decisions about width, signedness, scaling, encoding, byte order, and error handling.

Key Terms

Radix: The base of a number system.

Positional notation: A representation in which digit value depends on position and radix.

Bit: A binary digit with value 0 or 1.

Nibble: A group of four bits.

Byte: A group of eight bits.

Overflow: A result exceeding the range available in a fixed word length.

One’s complement: A representation formed by inverting every bit.

Two’s complement: The one’s complement plus one, commonly used for signed integers.

Fixed-point: A representation with a permanently assigned binary-point position.

Quantization: Mapping a continuous or high-precision value to discrete levels.

BCD: Binary-coded decimal, encoding each decimal digit separately.

Gray code: A code in which adjacent values differ by one bit.

ASCII: A standard character code originally using seven bits.

Unicode: A universal character-set standard supporting many writing systems.

UTF-8: A variable-length Unicode encoding using one to four bytes.

Parity: Redundant information used to detect selected bit errors.

Checksum: A calculated value used to detect data changes.

CRC: Cyclic redundancy check based on polynomial division over GF(2).

Endianness: The ordering of bytes when multi-byte values are stored or transmitted.

Review Questions

Short Answer Questions

  1. What is the radix of hexadecimal notation, and which symbols represent values ten through fifteen?
  2. Why are octal and hexadecimal convenient representations of binary data?
  3. State the range of an n-bit unsigned integer.
  4. What is the main limitation of a single parity bit?
  5. Distinguish ASCII from Unicode.

Long Answer Questions

  1. Explain positional notation and demonstrate conversion of a binary number containing both integer and fractional parts to decimal.
  2. Describe two’s-complement representation, including its range, method of forming a negative value, and overflow condition.
  3. Explain fixed-point representation and discuss the trade-off between range and precision.
  4. Compare BCD, pure binary, and Gray code in terms of structure and applications.
  5. Explain how a CRC detects transmission errors and why it should not be considered an authentication mechanism.

Analytical / Application Questions

  1. A controller stores a signed 8-bit result of 127 + 1. Determine the stored bit pattern, explain whether signed overflow occurs, and interpret the pattern as both signed and unsigned.
  2. Design a compact packet field for a measurement ranging from 0 to 63.9375 using 10 bits. Determine a suitable fixed-point allocation and calculate its resolution.
  3. A communication link reports a parity failure, but the receiver cannot identify the corrupted bit. Explain what can be concluded, what cannot be concluded, and recommend an appropriate additional reliability mechanism.

Multiple Choice Questions with Answers

Question 1

What is the hexadecimal representation of 156₁₀?

A. 8C₁₆

B. 9C₁₆

C. A6₁₆

D. F6₁₆

Correct Answer: B. 9C₁₆

Question 2

Which range is represented by an 8-bit unsigned integer?

A. −128 to 127

B. 0 to 127

C. 0 to 255

D. −255 to 255

Correct Answer: C. 0 to 255

Question 3

What is the two’s complement of 00000101₂ using eight bits?

A. 00000110₂

B. 11111010₂

C. 11111011₂

D. 10000101₂

Correct Answer: C. 11111011₂

Question 4

Which statement about BCD is correct?

A. Each decimal digit is represented separately by four bits

B. Every four-bit pattern is a valid decimal digit

C. It always uses fewer bits than pure binary

D. It is designed primarily for character encoding

Correct Answer: A. Each decimal digit is represented separately by four bits

Question 5

What is the Gray-code conversion of binary 1011₂?

A. 1001

B. 1110

C. 1101

D. 1010

Correct Answer: B. 1110

Question 6

How many bits are used by traditional ASCII?

A. 4

B. 7

C. 8

D. 16

Correct Answer: B. 7

Question 7

Which Unicode encoding is variable length and compatible with 7-bit ASCII for basic English characters?

A. UTF-8

B. UTF-16 only

C. UTF-32 only

D. BCD

Correct Answer: A. UTF-8

Question 8

What does a single parity bit reliably detect?

A. Every error pattern

B. The exact location of any error

C. Every odd number of bit errors

D. Only errors affecting the parity bit

Correct Answer: C. Every odd number of bit errors

Question 9

In unsigned Q4.4 fixed-point notation, what is the value of 1011.0100₂?

A. 10.25

B. 11.25

C. 11.5

D. 12.25

Correct Answer: B. 11.25

Question 10

What is the principal purpose of a CRC?

A. To encrypt a message

B. To identify the sender

C. To detect many classes of transmission errors

D. To convert decimal values to binary

Correct Answer: C. To detect many classes of transmission errors

Digital Electronics and Logic Design
Chapter 2 of 15