Digital Circuit And Logic Design I

Lecture 2 Outline

„ Number System and Codes (2) 1. Codes 2. Error detecting and correcting

Panupong Sornkhom, 2005/2 2 Number System and Codes (2) 1. Codes

„ What is code „ Decimal number codes „ „ Character codes „ Code for action, condition and states

Panupong Sornkhom, 2005/2 4 What is code?

„ A digital system requires that all information be in binary form. „ The external world, however, uses various other symbols to represent information. „ A symbol is represented in a digital system by a string of „ A set of n- strings in which different bit strings represent different symbol is called a code. „ A particular pattern of string is called code word.

Panupong Sornkhom, 2005/2 5 Decimal number codes

„ Binary-Coded Decimal „ Excess 3 code (BCD) code ‰ Self-complementing but is ‰ Encode the digit 0 – 9 by not weighted code

their 4-bit unsigned binary ‰ Excess3 = BCD + 00112 representation, other code „ Biquinary code word are not used ‰ Uses 7 bits per code word ‰ BCD is a weighted code, the weight are 8,4,2,1. so, ‰ The first two bits indicate sometime this code called range of number 0-4 or 5-9, 8421 code. and the last five bits indicate which of the five „ 2421 code numbers in the selected ‰ Weighted code with self- range is represented complementing (9’s „ 1-out-of-10 code complement) ‰ Using 10-bit code word.

Panupong Sornkhom, 2005/2 6 Decimal codes

Decimal BCD 2421 Excess-3 Biquinary 1-out-of-10 digit (8421) 0 0000 0000 0011 0100001 1000000000 1 0001 0001 0100 0100010 0100000000 2 0010 0010 0101 0100100 0010000000 3 0011 0011 0110 0101000 0001000000 4 0100 0100 0111 0110000 0000100000 5 0101 1011 1000 1000001 0000010000 6 0110 1100 1001 1000010 0000001000 7 0111 1101 1010 1000100 0000000100 8 1000 1110 1011 1001000 0000000010 9 1001 1111 1111 1010000 0000000001

Panupong Sornkhom, 2005/2 7 Gray code

„ It is sometimes necessary for an input sensor to produce a digital value that indicates a mechanical position. „ The encoder has a problem when the disk is positioned at certain boundaries between the regions where more than one bit 3-bit changes „ This problem can be solved by devising a digital code in which only one bit changes between each pair of successive code words. Such a code is called Gray code

3-bit Gray code

Panupong Sornkhom, 2005/2 8 Gray code (cont.)

„ There are two convenient ways to Decimal 1 bit 2 bits 3 bits construct n-bit Gray code

‰ The first method is based on the 0 0 00 000 fact that Gray code is a reflected code 1 1 01 001 „ A 1-bit Gray code has two code words, 0 and 1. 2 - 11 011 „ The first 2n code words of an (n+1)-bit Gray code equal the 3 - 10 010 code words of an n-bit Gray code, written in order with a 4 - - 110 leading 0 append „ The last 2n code words of an 5 - - 111 (n+1)-bit Gray code equal the code words of an n-bit Gray 6 - - 101 code, written in reverse order with a leading 1 append 7 - - 100

Panupong Sornkhom, 2005/2 9 Gray code (cont.)

‰ The second method allows us Decimal Binary code Gray code to derive n-bit Gray-code code number word directly from the 0 000 000 corresponding n-bit binary code word 1 001 001

„ The bits of an n-bit binary or 2 010 011 Gray-code code word are numbered from right to left, 3 011 010 from 0 to n-1 „ Bit i of a Gray-code code word 4 100 110 is 0 if bits i and i+1 of the corresponding binary code 5 101 111 word are the same, else bit i is 1. (When i+1 = n, bit n of the 6 110 101 binary code word is considered to be 0.) 7 111 100

Panupong Sornkhom, 2005/2 10 Character codes

„ The most commonly used character code is ASCII, the American Standard Code for Information Interchange.

„ ASCII represents each character with a 8-bit string, in fact 7 bits are sufficient for English

„ The code contains the uppercase and lowercase alphabet, numerals, punctuation, and various nonprinting control characters.

Panupong Sornkhom, 2005/2 11 Character codes (cont.)

Pictures from http://www.jimprice.com/jim-asc.htm

Panupong Sornkhom, 2005/2 12 Character codes (cont.)

Pictures from http://www.jimprice.com/jim-asc.htm

Panupong Sornkhom, 2005/2 13 Codes for Actions, Conditions, and States

„ Characters and numbers are considered to be data but sometimes we need non-data codes to represent actions, conditions, or states. „ For example, consider a simple traffic-light controller.

State Lights

N-S Green N-S Yellow N-S E-W Green E-W Yellow E-W Code word Red Red N-S go ON OFF OFF OFF OFF ON 000

N-S wait OFF ON OFF OFF OFF ON 001

N-S delay OFF OFF ON OFF OFF ON 010

E-W go OFF OFF ON ON OFF OFF 100

E-W wait OFF OFF ON OFF ON OFF 101

E-W delay OFF OFF ON OFF OFF ON 110

Panupong Sornkhom, 2005/2 14 2. Error detecting and correcting

„ Introduction

„ Single error detecting

„ Error correcting and multiple errors detecting

„ Hamming code

Panupong Sornkhom, 2005/2 15 Introduction

„ An error in a digital system is the corruption of the data from its correct value to some other value caused by physical failure „ Failure can be either temporary or permanent „ Single error = failure affect only a single bit of data „ Multiple errors = two or more bits in error „ Error-detecting codes n ‰ A code that uses n-bit strings need not contain 2 valid code words

‰ Corrupting a code word will likely produce a bit string that is not a code word

Panupong Sornkhom, 2005/2 16 Single error detecting

„ A code detects all single errors Information Even-parity Odd-parity is the minimum distance bits code code between all possible pairs of 000 000 0 000 1 code words is 2 001 001 1 001 0 „ In general, we need n+1 bits to 010 010 1 010 0 construct a single-error- detecting code with 2n code 011 011 0 011 1 words. 100 100 1 100 0 ‰ The first n bits of a code word = information bits 101 101 0 101 1

‰ The last one bit = parity bit 110 110 0 110 1

111 111 1 111 0

Panupong Sornkhom, 2005/2 17 Error-correcting and multiple-error-detecting codes

„ Assume that failures affect at most one bit of each code word, then a noncode word with a 1-bit error will be closer to the originally code word than to any other code word. „ Therefore, we can correct the error by changing the noncode word to the nearest code word „ A code that is used to correct errors is called an error- correcting code „ If a code has minimum distance 2c+1, it can be used to correct errors that affect up to bits „ If a code’s minimum distance is 2c+d+1, it can be used to correct errors in put to c bits and to detect errors in put to d additional bits

Panupong Sornkhom, 2005/2 18 Error-correcting and multiple-error- detecting codes (cont.)

„ For example, a code with minimum distance 4 (c=1, d=1)

‰ Single-bit errors that produce noncode words 00101010 and 11010011 can be corrected

‰ However, a error that produces 10100011 can’t be corrected but can be detected Pictures from Textbook DDPP

Panupong Sornkhom, 2005/2 19 Error-correcting and multiple-error-detecting codes (cont.)

‰ A 3-bit error may be “corrected” to the wrong value.

‰ It may be acceptable if 3- bit errors are rarely to occur.

‰ However, if we concerned about 3-bit errors, we can Picture from Textbook DDPP change the decoding policy for the code by just flag all noncode words as uncorrectable errors.

Panupong Sornkhom, 2005/2 20 Hamming codes

„ In 1950, R. W. Hamming described a general method for constructing codes with a minimum distance of 3, now called Hamming codes „ For any value of i, his method yields a (2i – 1)-bit code (check bits = i bits and information bits = 2i –1 –i bits) „ The bit positions can be numbered from 1 through 2i-1.

‰ Any position whose number is power of 2 is a check bit

‰ The remaining positions are information bits

Panupong Sornkhom, 2005/2 21 Hamming codes (cont.)

„ Each check bit is grouped with a subset of the information bits, as specified by a parity-check .

‰ Each check bit is grouped with the information positions whose numbers have a 1 in the same bit when expressed in binary

‰ For example, check bit 2 (010) is grouped with information bits 3 (011), 6 (110), and 7 (111) „ For a given combination of information-bit values, each check bit is chosen to produce even parity, so the total number of 1s in its group is even.

Panupong Sornkhom, 2005/2 22 Hamming codes (cont.)

Parity-check matrices for 7-bit Hamming codes: (a) With bit positions in numerical order;

(b) With check bits and information bits separated

Panupong Sornkhom, 2005/2 23 Hamming codes (cont.)

„ Example

‰ Distance-3 Hamming code „ Information bits = 0000 Î Parity bits = 000 „ Information bits = 0100 Î Parity bits = 110 „ Information bits = 0111 Î Parity bits = 000 „ Information bits = 1100 Î Parity bits = 001 „ Information bits = 1101 Î Parity bits = 010 „ A distance-3 Hamming code can easily be modified to increase its minimum distance to 4.

‰ By adding one more check bit, chosen that the parity of all the bits is even.

Panupong Sornkhom, 2005/2 24 CRC codes

„ CRC stands for Cyclic-redundancy-check „ Two important applications of CRC codes are in disk drives and in data networks.

‰ In a disk drive, each block of data (typically 512 bytes) is protected by a CRC code

‰ In a data network, each packet of data ends with check bits in a CRC code „ The CRC codes for both applications were selected because of their burst-error detecting properties. Such errors are more likely than errors of randomly distributed bits, because of the likely physical causes of errors in the two applications – surface defects in disk drives and noise bursts in communication links.

Panupong Sornkhom, 2005/2 25 Checksum codes

„ Using technique of modular addition „ For example

‰ A computer stores information as a set of 8-bit bytes.

‰ Each byte may be considered to have a decimal value from 0 to 255.

‰ Therefore, we can use module-256 addition to check the bytes.

‰ We form a single check byte called, checksum, that is the sum of module256 of all the information bytes.

‰ The resulting checksum code can detect any single byte error, since such error will cause a recomputed sum of bytes to disagree with the checksum. „ Checksum codes can also use a different modulus of addition.

Panupong Sornkhom, 2005/2 26 Two-Dimensional codes

„ It is another way to obtain a code with large minimum distance „ The information bits are conceptually arranged in a two-dimensional array, and parity bits are provided to check both the rows and the columns.

„ A code Crow with minimum distance drow is used for the rows, and a possibly differrent code Ccol with minimum distance dcol is used for the columns. „ The row-parity bits are selected so that each row is a code word in

Crow and the column-parity bits are selected so that is a code word in Ccol „ The “corner” parity bits can be chosen according to either code. „ The minimum distance of the two-dimensional code is the product of

drow and dcol; in fact, two-dimensional codes are sometimes called product codes

Panupong Sornkhom, 2005/2 27 Two-Dimensional codes (con.)

Pictures from Textbook DDPP

Panupong Sornkhom, 2005/2 28