Binary Codes - BCD, ASCII, Unicode, Parity and Gray
In questa pagina 7
A code associates each element of a discrete set with a bit configuration, to ease processing, storage and transmission. An -bit code has configurations: 4 elements need 2 bits, 8 elements 3 bits, 16 elements 4 bits. If elements are fewer than configurations, some are unused.
BCD
Definition. BCD (Binary-Coded Decimal) encodes each decimal digit with its binary value on 4 bits; a number is encoded digit by digit.
| digit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| BCD | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 |
, whereas in plain binary it is : the two representations differ. The six configurations to are not BCD digits: they are unused, and become don't-care conditions in Karnaugh maps (Karnaugh Maps and Two-Level MinimizationA Karnaugh map is the truth table arranged in a grid with rows and columns in Gray code, so that adjacent cells (also across opposite edges) differ in one variable. Groups of 2^k ones eliminate k variables and give a product. A prime implicant is a maximal group, an essential one is the only cover of some minterm; the minimum cover has all essentials plus few other primes. For POS, group the zeros. Don't cares may be used as 1 or 0. XOR functions have a checkerboard map and do not simplify.Karnaugh Maps and Two-Level Minimization →). The number needs 8 bits in BCD (). Reading: (groups of 4); , not ; .
ASCII
ASCII uses 7 bits for characters: upper/lower case letters, digits, punctuation and non-printable control characters; usually stored in a byte. A ; a (lower case differs in one bit); 0 ; 5 . The ASCII code of a digit is 011 followed by its BCD: 5 .
Unicode and UTF-8
Unicode gives every character a unique number, the code point, written U+ plus hex digits (T = U+0054, ± = U+00B1, € = U+20AC). UTF-8 uses 1 to 4 bytes per code point and is compatible with ASCII (first code points are one byte).
| code point range | bytes | bit pattern |
|---|---|---|
| U+0000 - U+007F | 1 | 0xxxxxxx |
| U+0080 - U+07FF | 2 | 110xxxxx 10xxxxxx |
| U+0800 - U+FFFF | 3 | 1110xxxx 10xxxxxx 10xxxxxx |
| U+10000 - U+10FFFF | 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
The x bits are the code point bits, distributed left to right. ±: fits in 2 bytes; the 11 payload bits give and : . €: , 16 bits, 3 bytes: .
Parity bit
Definition. To detect (not correct) errors a bit is added so that the total number of 1s is even (even parity, most common) or odd.
1011001 has four 1s: even parity bit (01011001), odd parity bit (11011001). 1010100 has three 1s: even parity bit (11010100), odd parity bit .
The receiver counts the 1s: an odd count under even parity means at least one corrupted bit (it can ask for retransmission, NAK). An even count means no single-bit error, but two compensating errors are possible: parity detects errors on an odd number of bits. With even parity 00100010 and 11111111 are acceptable, 00100011 signals an error. Correction needs more check bits.
Hamming distance
Definition. The Hamming distance between two configurations is the number of bits in which they differ (not the algebraic difference).
and : distance ; and : distance ; (). -bit configurations are the vertices of a hypercube; distance 1 means joined by an edge (useful for Karnaugh maps).
Gray code
Definition. In a Gray code two consecutive numbers have Hamming distance .
Why. A position device with 3 binary switches goes from (3) to (4) changing all bits; since sensors do not switch simultaneously one may read , and is position 5: a big error. With Gray only one sensor changes between adjacent positions, so the reading is either the old or the new position. Used in position encoders, to order Karnaugh maps and to encode states.
Construction by reflection. Take the -bit code, write it again in reverse order below, prefix to the first half and to the second. 3 bits: . The first and last values are at distance 1: the code is cyclic.
Conversion binary to Gray. The MSB is copied; every other bit is . Inverse: .
: , so . .
| decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| binary | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
| Gray | 0000 | 0001 | 0011 | 0010 | 0110 | 0111 | 0101 | 0100 | 1100 | 1101 | 1111 | 1110 | 1010 | 1011 | 1001 | 1000 |
Number of words not a power of 2. For an even number of words (e.g. ): take the Gray code of the first words () and append their mirror image with the MSB at : ; the result is still cyclic ( and differ by one bit). For words (e.g. ) take the first of the normal Gray code.
Common mistakes
- Reading BCD as binary, or forgetting 4 bits per digit.
- Saying parity corrects errors (it detects, and only an odd number).
- Computing Hamming distance as numeric difference.
- Confusing 7-bit ASCII with an 8-bit code; assuming UTF-8 uses a fixed number of bytes.
Versione ripasso
- -bit code: configurations; extra ones are unused.
- BCD: one digit per 4 bits: . - unused (don't care in K-maps). needs 8 bits. ; .
- ASCII: 7 bits, 128 characters (32 control).
A,a,0,5; digit code + BCD. - UTF-8: 1-4 bytes, ASCII compatible. Patterns
0xxxxxxx,110xxxxx 10xxxxxx,1110xxxx 10xxxxxx 10xxxxxx,11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. ±U+00B1; €U+20ACneeds 3 bytes. - Parity: extra bit makes the count of 1s even (or odd);
101100101011001(even),11011001(odd). Detects errors on an odd number of bits, never corrects. - Hamming distance: number of differing bits: , , .
- Gray: consecutive numbers differ by one bit (position sensors: can be misread as in binary). Reflection: ; cyclic.
- Conversion: , ; , ; inverse .
- Gray with even words: first words plus mirrored ones with MSB 1 (10 words: ); words: first ones of the normal code.
- Mistakes: BCD read as binary; parity "corrects"; distance as numeric difference; ASCII as 8-bit; fixed-size UTF-8.