Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 2 – Introduction to Computer Codes. Computer Codes A code is a systematic use of a given set of symbols for representing information. As an example,

Similar presentations


Presentation on theme: "Topic 2 – Introduction to Computer Codes. Computer Codes A code is a systematic use of a given set of symbols for representing information. As an example,"— Presentation transcript:

1 Topic 2 – Introduction to Computer Codes

2 Computer Codes A code is a systematic use of a given set of symbols for representing information. As an example, a traffic light uses a code to signal what you need to do… Red = Stop Yellow = Caution; about to go to red Green = Go We will look at three important types of codes: numeric, character, and error detection & correction.

3 Fixed Point Numbers A fixed point number is used to represent either signed integers or signed fractions. In either case, sign magnitude, 2’s complement, or 1’s complement systems can be used to represent the signed value. A fixed point integer has an implied radix (decimal) point to the right of the least significant bit. A fixed point fraction has an implied radix point between the sign bit and the most significant magnitude bit.

4 Fixed Point Numbers...n-1n-2210 sign bit implied binary point. magnitude representation...n-1n-2210 sign bit implied binary point. magnitude representation (top) fixed point integer (bottom) fixed point fraction

5 Excess Representations An excess-K representation of a code C is formed by adding the value K to each code word of C. This sort of code is used frequently in the representation of the exponents of floating point numbers so that the smallest exponent value will be represented by all zeros. As an example, let’s compute the excess-8 code for 4-bit 2’s complement numbers (note all we have to do is add 8 = (1000) 2 to each code).

6 Excess-8 4-bit 2’s Complement Code Decimal2’s Complement Excess-8Decimal2’s Complement Excess-8 +70111111111110111 +601101110-211100110 +501011101-311010101 +401001100-411000100 +300111011-510110011 +200101010-610100010 +100011001-710010001 000001000-810000000

7 Floating-Point Numbers Floating-point numbers are similar to numbers written in scientific notation. In general, the floating-point form of a number N is written as: N = M x r E where M is the mantissa, a fixed-point number containing the significant digits of N, and E is the exponent, a fixed-point integer.

8 Mantissa Encoding In such a system, the mantissa and the exponent are generally coded separately. The radix is implied. The mantissa M is usually coded in sign- magnitude, usually as a fraction. It is usually written as: M = (S M.a n-1 a n-2 …a -m ) rsm The sign bit, S M is usually 0 for a positive number and 1 for a negative number.

9 Exponent Encoding The exponent E is often encoded in excess-K two’s complement notation. This representation of a number is formed by adding a bias of K to the 2’s complement integer value of the exponent. For binary floating-point numbers, K is usually selected to be 2 e-1 where e is the number of bits in the exponent. Therefore, -2 e-1 <= E < 2 e-1 0 <= E + 2 e-1 < 2 e

10 Exponent Encoding By examining this expression, we can see that the biased value of E is a number that ranges from 0 (at its most negative value) to 2 e –1 (at its most positive value). The excess-K form of E can be written as: E = (b e-1 b e-2 …b 0 ) excess-K The sign of E is indicated (in this form) by the bit b e-1, which will be 0 if E is negative and 1 if E is positive.

11 Normalization Note that more than one combination of mantissa and exponent can represent the same number. M = (0.11010101) 2 x 2 4 = (0.011010101) 2 x 2 5 = (0.0011010101) 2 x 2 6 In a digital system, it is useful to have one unique representation for each number. A floating-point number is normalized if the exponent is adjusted so that the mantissa has a nonzero value in its most significant digit.

12 Floating-Point Formats Floating-point formats used in computers often differ in the number of bits used to represent the mantissa and the exponent, and the method of coding for each. Most systems use a system where the sign is stored in the leftmost bit, followed by the exponent and then the mantissa. Typically, floating-point numbers are stored in one-word or two-word formats.

13 Floating-Point Formats A one-word format: A two-word format: Mantissa M (most significant part) Exponent ESMSM Mantissa MExponent ESMSM Mantissa M (least significant part)

14 IEEE Floating-Point Standards The Institute for Electrical and Electronic Engineers (IEEE) has defined a set of floating point standards. The single-precision IEEE standard calls for 32 bits, 1 sign bit, 23 mantissa bits, 8 exponent bits, and an exponent bias of 127. The single-precision IEEE standard calls for 64 bits, 1 sign bit, 52 mantissa bits, 11 exponent bits, and an exponent bias of 1023.

15 Binary Coding Decimal (BCD) The binary coded decimal (BCD) code is used for representing the decimal digits 0 through 9. It is a weighted code which means each bit position in the code have a fixed numerical weight associated with it. The digit represented by a code word is found by summing the weighted bits. BCD uses four bits, with the weights equal to those of a 4-bit binary integer (BCD is sometimes called a 8-4-2-1 code).

16 BCD Code Words DigitBCD Code DigitBCD Code 0000050101 1000160110 2001070111 3001181000 4010091001

17 Use of BCD Codes BCD codes are used to encode numbers for output to numerical displays (such as seven- segment displays). They are also used to represent numbers in processors which perform decimal arithmetic directly (instead of binary arithmetic). As an example, let’s encode the decimal number N=(9750) 10 in BCD. 9 -> 1001, 7 -> 0111, 5 -> 0101, 0 -> 0000 Therefore (9750) 10 = (1001011101010000) BCD.

18 ASCII The most widely used code for representing characters is ASCII. ASCII is a 7-bit code, frequently used with an 8 th bit for error detection (more about that in a bit). A complete ASCII table is located in your textbook, or all around the web. As an example, let’s encode ASCIIcode in ASCII.

19 ASCII Encoding CharacterASCII (binary) ASCII (hex) A100000141 S101001153 C100001143 I100100149 I100100149 c110001163 o11011116F d110010064 e110010165

20 Gray Codes A Gray code is defined as a code where two consecutive codewords differ in only one bit. The distance between two code words is defined as the number of bits in which the two words differ. Therefore, a Gray code has a distance of one. Let’s define a Gray code for the decimal numbers 0 through 15.

21 Gray Code Example DecimalBinaryGrayDecimalBinaryGray 00000 810001100 10001 910011101 2001000111010101111 3001100101110111110 4010001101211001010 5010101111311011011 6011001011411101001 7011101001511111000

22 Codes and Weights An error in binary data is defined as an incorrect value in one or more bits. A single error is an incorrect value in one bit and a multiple error is one or more bits being incorrect. Errors may be caused by hardware failure, external interference (noise), or other unwanted events. Certain types of codes allow the detection and sometimes the correction of errors.

23 Terminology C will refer to a code. I and J will refer to n-bit binary codewords. The weight of I, w(I), will be defined as the number of bits of I equal to 1. The distance between I and J, d(I, J) is equal to the number of bit positions in which I and J differ.

24 Terminology Example I = (01101100) J = (11000100) w(I) = 4 w(J) = 3 d(I, J) = 3

25 Error Detection and Correction Codes If the distance between any two code words in C is >= d min, the code is said to have minimum distance d min. For a given d min, at least d min errors are needed to transform one valid code word into another. If there are fewer than d min errors, a detectable noncode word results. If this noncode word is closer to one valid codeword than any other, the original code word can be deduced and the error corrected.

26 Error Detection and Correction Codes In general, a code provides t error correction plus detection of s additional errors if and only if the following inequality is satisfied… 2t + s + 1 <= d min

27 Error Detection and Correction 2t + s + 1 <= d min Examining this inequality shows: A single-error detection code (s=1, t=0) requires a minimum distance of 2. A single-error correction code (s=0, t=1) requires a minimum distance of 3. A single-error correction and double-error detection (s=t=1) requires a minimum distance of 4.

28 Parity Codes Parity codes are formed from a code C by concatenating (operator |) a parity bit, P to each code word of C. In an odd-parity code, the parity bit is specified to be either 0 or 1 as necessary for w(P|C) to be odd. In an even-parity code, the parity bit is specified to be either 0 or 1 as necessary for w(P|C) to be even. Information BitsP

29 Parity Code Example Concatenate a parity bit to the ASCII code for the characters 0, X, and = to produce both odd-parity and even-parity codes. CharacterASCIIOdd-Parity ASCII Even-Parity ASCII 001100001011000000110000 X10110000101100011011000 =01111001011110000111100

30 Parity Code Effectiveness Error detection on a parity code is easily accomplished by checking to see if a codeword has the correct parity. For example, if the parity of an odd-parity codeword is actually even, an error has occurred in this codeword. Parity codes are minimum-distance-2 codes and thus can detect single errors. Unfortunately, errors in an even number of bits will not change the parity and are therefore not detectable using a parity code.

31 Two-out-of-Five Codes A two-out-of-five code is an error detection code having exactly two bits equal to 1 and three bits equal to 0. Error detection is accomplished by counting the number of ones in a code word. An error has occurred if this number is not equal to two. These codes permit the detection of single errors and multiple errors in adjacent bits.

32 Two-out-of-Five Code Example As an example, let’s look at a two-out- of-five code for decimal digits. DigitCodewordDigitCodeword 000011501010 100101610010 201001701100 310001810100 400110911000

33 Hamming Codes A more complex error detection and correction system of codes was introduced by Richard Hamming in 1950. Hamming codes are similar to an extension of parity codes in that multiple parity, or check bits are used. Each check bit is defined over a subset of the information bits in a codeword. These subsets overlap such that each information bit is in at least two subsets.

34 Error Detection and Correction with Hamming Codes The error detection and correction properties of a Hamming code are determined by the number of check bits used and how the check bits are defined over the information bits. The minimum distance d min is equal to the weight of the minimum-weight nonzero code word (the number of ones in the codeword with the fewest). Hamming codes are complex in their formation and analysis. We will look at two different codes with different properties, but not discuss them in too much depth.

35 Hamming Code Example #1 This code has d min =3, so it can provide single error correction. Information Word Hamming Code Information Word Hamming Code Information Word Hamming Code 000000000000110011001111001100001 000100010110111011100011011101010 001000101011000100011111101110100 001100111101001100110011111111111 0100010011010101010010 0101010110110111011001

36 Hamming Code Example #1 Here, let’s assume we have a single error in the codeword 0100110 so that we read it as 1100110. Only the codeword in which the error occurred has distance 1 from the invalid word. So, the detection of the error word is equivalent to correcting the error, since the only possible single error that could have produced this error word is the codeword 0100110. So, this code offers single error correction.

37 Hamming Code Example #2 This code has d min =4, so it can provide single error correction and double error detection. Information Word Hamming Code Information Word Hamming Code Information Word Hamming Code 000000000000011001100011110011001001 000100011011011101111000110111010010 001000101101100010000111111011100100 001100110110100110011100111111111111 010001001110101010101010 010101010101101110110001

38 Summary You should now understand: Fixed-point and floating-point number representation BCD and ASCII character codes Gray codes and excess-k codes Simple error detection and error correction codes, including parity codes, 2-out-of-5 codes, and Hamming codes


Download ppt "Topic 2 – Introduction to Computer Codes. Computer Codes A code is a systematic use of a given set of symbols for representing information. As an example,"

Similar presentations


Ads by Google