Salta al contenuto
Note per Studenti Binary Codes - BCD, ASCII, Unicode, Parity and Gray Report

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 nn-bit code has 2n2^n 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

18510=0001  1000  0101BCD185_{10}=0001\;1000\;0101_{BCD}, whereas in plain binary it is 10111001210111001_2: the two representations differ. The six configurations 10101010 to 11111111 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 1010 needs 8 bits in BCD (0001 00000001\,0000). Reading: 0001 0101 1001BCD=1590001\,0101\,1001_{BCD}=159 (groups of 4); 00101000BCD=2800101000_{BCD}=28, not 4040; 00010010BCD=1210=C1600010010_{BCD}=12_{10}=C_{16}.

ASCII

ASCII uses 7 bits for 128128 characters: upper/lower case letters, digits, punctuation and 3232 non-printable control characters; usually stored in a byte. A =10000012=65=1000001_2=65; a =11000012=97=1100001_2=97 (lower case differs in one bit); 0 =01100002=48=0110000_2=48; 5 =01101012=53=0110101_2=53. The ASCII code of a digit is 011 followed by its BCD: 5 =011 0101=011\,0101.

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 128128 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. ±: B116=1011 00012B1_{16}=1011\,0001_2 fits in 2 bytes; the 11 payload bits 000 1011 0001000\,1011\,0001 give 0001000010 and 110001110001: 11000010  1011000111000010\;10110001. €: 20AC16=0010 0000 1010 1100220AC_{16}=0010\,0000\,1010\,1100_2, 16 bits, 3 bytes: 11100010  10000010  1010110011100010\;10000010\;10101100.

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 00 (01011001), odd parity bit 11 (11011001). 1010100 has three 1s: even parity bit 11 (11010100), odd parity bit 00.

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).

8=10008=1000 and 11=101111=1011: distance 22; 0=0000=000 and 7=1117=111: distance 33; d(0110,1101)=3d(0110,1101)=3 (0110⊕1101=10110110\oplus1101=1011). nn-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 11.

Why. A position device with 3 binary switches goes from 011011 (3) to 100100 (4) changing all bits; since sensors do not switch simultaneously one may read 011→001→101→100011\to001\to101\to100, and 101101 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 (n−1)(n-1)-bit code, write it again in reverse order below, prefix 00 to the first half and 11 to the second. 3 bits: 000,001,011,010,110,111,101,100000,001,011,010,110,111,101,100. 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 gi=bi⊕bi+1g_i=b_i\oplus b_{i+1}. Inverse: bi=bi+1⊕gib_i=b_{i+1}\oplus g_i.

b=1011b=1011: g=1, 0⊕1=1, 1⊕0=1, 1⊕1=0g=1,\ 0\oplus1=1,\ 1\oplus0=1,\ 1\oplus1=0, so g=1110g=1110. b=0110→g=0101b=0110\to g=0101.

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 nn of words (e.g. 1010): take the Gray code of the first n/2=5n/2=5 words (0000,0001,0011,0010,01100000,0001,0011,0010,0110) and append their mirror image with the MSB at 11: 1110,1010,1011,1001,10001110,1010,1011,1001,1000; the result is still cyclic (00000000 and 10001000 differ by one bit). For 2n−12^n-1 words (e.g. 77) take the first 77 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

  • nn-bit code: 2n2^n configurations; extra ones are unused.
  • BCD: one digit per 4 bits: 185=0001 1000 0101BCD≠101110012185=0001\,1000\,0101_{BCD}\ne10111001_2. 10101010-11111111 unused (don't care in K-maps). 1010 needs 8 bits. 0001 0101 1001BCD=1590001\,0101\,1001_{BCD}=159; 00101000BCD=2800101000_{BCD}=28.
  • ASCII: 7 bits, 128 characters (32 control). A=65=65, a=97=97, 0=48=48, 5=0110101=0110101; digit code =011=011 + BCD.
  • UTF-8: 1-4 bytes, ASCII compatible. Patterns 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx 10xxxxxx 10xxxxxx, 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. ±==U+00B1→11000010  10110001\to11000010\;10110001; €==U+20AC needs 3 bytes.
  • Parity: extra bit makes the count of 1s even (or odd); 1011001 →\to 01011001 (even), 11011001 (odd). Detects errors on an odd number of bits, never corrects.
  • Hamming distance: number of differing bits: d(1000,1011)=2d(1000,1011)=2, d(000,111)=3d(000,111)=3, d(0110,1101)=3d(0110,1101)=3.
  • Gray: consecutive numbers differ by one bit (position sensors: 011→100011\to100 can be misread as 101101 in binary). Reflection: 000,001,011,010,110,111,101,100000,001,011,010,110,111,101,100; cyclic.
  • Conversion: gmsb=bmsbg_{msb}=b_{msb}, gi=bi⊕bi+1g_i=b_i\oplus b_{i+1}; 1011→11101011\to1110, 0110→01010110\to0101; inverse bi=bi+1⊕gib_i=b_{i+1}\oplus g_i.
  • Gray with nn even words: first n/2n/2 words plus mirrored ones with MSB 1 (10 words: 0000,0001,0011,0010,0110,1110,1010,1011,1001,10000000,0001,0011,0010,0110,1110,1010,1011,1001,1000); 2n−12^n-1 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.

Teoria collegata