2.4 Overflow, BCD, and Character Codes
The previous section showed that a fixed bit pattern can receive different integer meanings. This section asks two related questions:
1. What happens when a calculated value does not fit the chosen width?
2. How can the same bits represent decimal digits or text instead of a binary integer?
Fixed-width addition keeps only available bits
An -bit register stores one of patterns. If addition produces an extra high bit, the register cannot keep it.
Using 4 bits:
The stored result is 0000; the leading 1 is reported as a carry out. Fixed-width unsigned arithmetic therefore behaves modulo :
This wraparound is a defined consequence of finite width. Whether it is acceptable depends on the intended calculation.
Carry and signed overflow answer different questions
The carry flag (CF) reports that an unsigned result needed an additional high bit.
The overflow flag (OF) reports that a two's-complement signed result lies outside the signed range.
For signed addition, overflow occurs when:
- two nonnegative operands produce a negative decoded result; or
- two negative operands produce a nonnegative decoded result.
Adding operands with opposite signs cannot overflow, because their mathematical sum lies between them.
With 4-bit patterns:
| Bit-pattern addition | Unsigned reading | Signed reading | CF | OF |
|---|---|---|---|---|
1111 + 0001 = 0000 | 1 | 0 | ||
0111 + 0001 = 1000 | 0 | 1 | ||
1000 + 1111 = 0111 | 1 | 1 | ||
0010 + 0011 = 0101 | 0 | 0 |
The same adder produces the result bits. The selected interpretation tells software which flag matters.
Lab 1 — construct every flag combination
Choose 4- or 8-bit operands as raw bit patterns. The lab simultaneously decodes each operand as unsigned and two's complement, then displays both CF and OF.
Complete all four missions. For each solution, explain the result once as unsigned arithmetic and once as signed arithmetic.
Bits can encode decimal digits instead of a binary integer
Binary-coded decimal (BCD) stores each decimal digit separately as a 4-bit binary value.
Decimal 59 becomes:
Packed together:
This is not the ordinary binary encoding of numeric 59:
Both use eight displayed bits here, but their group meanings differ.
Valid BCD nibbles run from 0000 through 1001. The six patterns 1010 through 1111 are invalid as individual decimal digits.
BCD is useful when decimal digits must be preserved exactly, such as some financial, display, and legacy interface tasks. It uses space less efficiently than pure binary: one decimal digit has ten possibilities but receives sixteen possible 4-bit patterns.
Character codes map numbers to symbols
A computer also stores text as numbers. A character code is an agreed mapping between numeric code points and characters.
ASCII is a foundational 7-bit code commonly stored in an 8-bit byte.
| Character | Decimal code | Hexadecimal | 8-bit pattern |
|---|---|---|---|
A | 65 | 41 | 01000001 |
C | 67 | 43 | 01000011 |
0 | 48 | 30 | 00110000 |
5 | 53 | 35 | 00110101 |
The character '5' is not the numeric integer 5:
If software subtracts the ASCII code for '0' from a character code '0' through '9', it obtains the corresponding numeric digit. That conversion is a program operation, not an automatic property of storage.
Modern text uses Unicode to cover many writing systems. Encodings such as UTF-8 translate Unicode code points into bytes. This chapter needs only the general principle: the receiver must know the encoding contract.
Lab 2 — repair BCD and ASCII transmissions
Each mission starts with one flipped bit. Toggle the BCD nibbles or ASCII bytes, inspect the live decoded payload, and restore the intended message.
After repairing a case, deliberately flip another bit. Compare an error that creates an invalid BCD nibble with one that remains valid but silently changes the digit or character.
Representation metadata is essential
Suppose memory contains 00110101. Without context, it could be:
- unsigned integer 53;
- signed two's-complement integer 53;
- two BCD digits
3and5, meaning decimal 35;
- ASCII character
'5';
- part of a machine instruction or another data structure.
Memory stores bits, not meanings. Meaning comes from the instruction being executed, the data type chosen by software, the device protocol, and the agreed encoding.
Chapter 2 synthesis
You can now:
- expand positional numerals into weighted contributions;
- convert among binary, octal, decimal, and hexadecimal;
- distinguish unsigned, sign-magnitude, ones'-complement, and two's-complement interpretations;
- detect unsigned carry and signed overflow separately;
- recognize BCD digits and ASCII character bytes.
Chapter 3 moves from representation to hardware logic: gates and truth tables will show how circuits transform these bit patterns.