Data Representation and Numbering Systems ACOE 161 1
Data Representation and Numbering Systems ACOE 161 1
The Decimal Numbering System • In the Decimal system a digit can take one out of ten different values (0. . 9) • A number in the decimal system is expressed by the following expression: (dndn-1…d 1 d 0)10 = (dn. X 10 n )+ (dn-1 X 10 n-1) + …+ (d 1 X 101) +( d 0 X 100) Where d = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} = {0. . 9} • Notes: N – A decimal system with N digits can represent the numbers from 0 to 10 -1. N – In a decimal system with N digits there are 10 different combinations. – The digit to the right of a number is called the Least Significant Digit (LSD). – The digit to the left of a number is called the Most Significant Digit (MSD). ACOE 161 2
The Binary Numbering System • Digital systems and computers use the Binary system because it has only two states (0 and 1) • A number in the Binary system is expressed by the following expression: (dndn-1…d 1 d 0)2 = (dn. X 2 n )+ (dn-1 X 2 n-1) + …+ (d 1 X 21) +( d 0 X 20) Where d = {0, 1} Examples: • (1011)2 = (1 X 23 )+(0 X 22)+(1 X 21) +(1 X 20) = 8+0+2+1= (11)10 • (10110)2 = (1 X 24 )+(0 X 23)+(1 X 22) +(1 X 21) +(0 X 20) = 16+0+4+2+0= (22)10 • (101100)2=(1 X 25 )+(0 X 24)+(1 X 23)+(1 X 22)+(0 X 21)+(0 X 20)=32+0+8+4+0+0=(44)10 ACOE 161 3
The Binary Numbering System (Cont. ) • • A binary digit is called the BIT (BInary digi. T). A group of eight bits is called the BYTE. The leftmost bit of a number is called the Most Significant Bit (MSB). The rightmost bit of a number is called the Least Significant Bit (LSB). N A binary system with N bits can represent the numbers from 0 to 2 -1. N In a binary system with N digits there are 2 different combinations. A binary number is multiplied by two, if we append a zero at the LSB. Prefixes in the binary system: 10 2 = 1, 024 = 1 K (Kilo) 20 2 = 1, 024 X 1, 024 = 1, 048, 576 = 1 M (Mega) 0 2 =1 1 2 =2 2 2 =4 3 2 =8 4 2 = 16 5 2 = 32 6 2 = 64 7 2 = 128 8 2 = 256 9 30 2 = 512 40 2 =1024=1 K 2 = 1 G (Giga) 2 = 1 T (Tera) ACOE 161 Powers of 2: 10 16 2 = 65536 4
A General Numbering System With Base R • A numbering system with base R can have digits in the range from 0 to R-1 • A number in a system with base R is expressed by the following expression: (dndn-1…d 1 d 0)R = (dn. XRn )+ (dn-1 XRn-1) + …+ (d 1 XR 1) +( d 0 XR 0) Where d = {0. . R-1} Examples: • A computer that uses light (color) to represent information will use a numbering system with a base of 3, since there are three basic colors. • Usually in computers we group the bit of a number for simplicity. A group of 4 bits yields a system with 16 combinations. Such a system is called the Hexadecimal system ACOE 161 5
Conversion from a system with base R to Decimal • To convert a number from a system with base R to decimal we replace R with the base of the system in the following expression: (dndn-1…d 1 d 0)R = (dn. XRn )+ (dn-1 XRn-1) + …+ (d 1 XR 1) +( d 0 XR 0) Examples: • (214)5 = (2 X 52 )+(1 X 51) +(4 X 50) = (2 X 25)+(1 X 5)+(4 X 1)=50+5+4= (59)10 • (2021)3 = (2 X 33)+(0 X 32)+(2 X 31)+(1 X 30)=(2 X 27)+(2 X 3)+(1 X 1)=54+6+1= (61)10 • (13 A)12 = (1 X 122)+(3 X 121)+(10 X 120)=144+36+10=(190)10 • (4 C)18 = (4 X 181)+(12 X 180)=72+12=(84)10 • (135)4 = Invalid. Digit 5 is greater than 4 which is the base of the system. ACOE 161 6
Examples Do the following conversions: • (234)6 = (? )10 • (2012)3 = (? )10 • (A 1 C)14 = (? )10 • (5 H)20 = (? )10 • (122)4 = (? )10 • (12200)4 = (? )10 • (122003)4 = (? )10 ACOE 161 7
Conversion from Decimal to a system with base R A decimal number can be converted into its equivalent in base R using the following procedure: Step 1: Perform the integer division of the decimal number by R and record the remainder. e. g. if the number is 70 and the base is 4 then 70/4 = 17 + 2/4 Step 2: Replace the decimal number with the result of the division in step 1. Repeat step 1, until a zero result is found. e. g. 17/4 = 4 + 1/4 4/4 = 1 + 0/4 1/4 = 0 + 1/4 Step 3: The number is formed by reading the remainders in reversed order. e. g. (70)10 = (1012)4 ACOE 161 8
Examples Do the following conversions: • (53)10 = (? )2 • (47)10 = (? )3 • (124)10 = (? )14 • (253)10 = (? )20 • (97)10 = (? )7 • (25)10 = (? )4 • (250)10 = (? )4 • (100)10 = (? )4 ACOE 161 9
Homework: Do the necessary ACOE 161 conversions to fill up the table below: 10
Homework Do the following conversions: • (244)5 = (? )10 • (2132)4 = (? )10 • (A 4 F)18 = (? )10 • (6 H)22 = (? )10 • (1202)3 = (? )10 • (120200)3 = (? )10 • (1202003)4 = (? )10 ACOE 161 11
The Octal and Hexadecimal Numbering Systems • Computers use the binary system to represent data. In most cases a number is represented with 16, 32 or more bits, which is difficult to be handled by humans. • To make binary numbers easier to manipulate, we can group the bits of the number in groups of 2, 3 or 4 bits. • If we take a group of 2 bits, then we can have 4 combinations or different digits in each group. Thus the new system is a system with the base of 4. (11)2 = 3 e. g. (00)2 = 0 (10110100)2= (10 11 01 00)2= (2310)4 (10)2 = 2 (01)2 = 1 • The system with the base 4 is not widely used because many digits are still required to represent typical numbers. ACOE 161 12
The Octal and Hexadecimal Numbering Systems (Cont. ) • If we take a group of 3 bits, then we can have 8 combinations or different digits in each group. Thus the new system is a system with the base of 8 and is called the Octal system. (110)2 = 6 e. g. (10110100)2= (10 100)2= (264)8 (010)2 = 2 (100)2 = 4 • If we take a group of 4 bits, then we can have 16 combinations or different digits in each group. Thus the new system is a system with the base of 16 and is called the hexadecimal or hex system. Letters A to F are used to represent digits from 10 to 15. e. g. (10110100)2= (1011 0100)2= (B 4)16 (1011)2 =11=B ACOE 161 (0100)2 = 4 13
Decimal, Binary, Octal and Hexadecimal Conversion Table Decimal. Binary Base 4 Octal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ACOE 161 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F 14
Examples ACOE 161 15
Homework Do the following conversions: • (53)10 = (? )2 = (? )4 = (? )8 = (? )16 • (1000111)2 = (? )10 = (? )4 = (? )8 = (? )16 • (1203)4 = (? )10 = (? )2 = (? )8 = (? )16 • (253)8 = (? )10 = (? )2 = (? )4 = (? )16 • (9 C)16 = (? )2 = (? )4 = (? )8 = (? )10 ACOE 161 16
Fractional number representation • Let us assume a radix point (. ) with the decimal part on the left and the fractional part on the right • Then • Example: ACOE 161 17
Examples (2 B. 1 A) ACOE 161 18
Converting fractional numbers from decimal number system to radix r • To convert a decimal fraction to a radix r number, repeatedly multiply the fraction part by r and retain the integer digit in sequence until the fraction is zero, or the number of digits required are obtained (the number of digits may be infinite). • Then the digits following the radix point from left to right with the integer digits, the earliest obtained first. ACOE 161 19
Example • • 0. 5625 2 = 1. 1250 Most significant 0. 1250 2 = 0. 2500 2 = 0. 5000 2 = 1. 0000 Least significant ACOE 161 20
Converting fractional numbers from binary to hex and from hex to binary • The same principle with integer numbers applies: Group four bits together, padding with zeros if necessary, but this time from left to right, and convert its 4 -bit group to its corresponding Hexadecimal number and vice versa • Examples: ACOE 161 21
Homework ACOE 161 22
The Binary Coded Decimal (BCD) System • In many applications it is required to encode each decimal digit to the equivalent 4 -bit binary number. This binary code is called the BCD code. 4 =(0100)2 e. g. (2 4 9)10= (0010 0100 1001)BCD 2 =(0010)2 9 = (1001)2 Examples: • (173)10 = (? )2 = (? )BCD • (1000 0111)BCD = (? )10 • (0100 0010)2 = (? )BCD • (53)16 = (? )BCD • (1011 0101)BCD = (? )10 ACOE 161 23
Addition in any numbering system • Addition in any numbering system can be performed by following the same rules used for decimal addition, where 10 is replaced by the base of the system (R). • Decimal Addition: 3 + 6 = 9 9/10 = 0 + 9/10 write 9 and carry 0 7 4 3 1 8 6+ 9 2 9 8 + 4 = 12 12/10 = 1 + 2/10 write 2 and carry 1 1+ 7 + 1 = 9 9/10 = 0 + 9/10 write 9 and carry 0 • Rules for addition in the decimal system: – Begin the addition by adding the 2 least significant digits first. – Perform the integer division of the sum with 10. Write down the remainder of the division and carry out the result to the next column. – Repeat the addition for the next columns by adding the two digits and the carry from the previous column. ACOE 161 24
Addition in any numbering system • Rules for addition in a system with base R: – Begin the addition by adding the 2 least significant digits first. – Perform the integer division of the sum with R. Write down the remainder of the division and carry out the result to the next column. – Repeat the addition for the next columns by adding the two digits and the carry from the previous column. Examples: Perform the following additions • (173)8+ (265)8 = (? )8 • (01101011)2+ (00111010)2 = (? )2 • (1243)5+ (234)5 = (? )5 • (1 A 79)16+ (C 827)16 = (? )16 • (1 A 79)18+ (C 827)18 = (? )18 ACOE 161 25
Subtraction in any numbering system • Subtraction in any numbering system can be performed by following the same rules used for decimal addition, where 10 is replaced by the base of the system (R). • Decimal Subtraction: 7 4 9 1 8 65 6 3 (9 6) 9 - 6 = 3 write 3 and borrow 0 from next column (4<8) borrow 1 10+4 -8=6 write 6 and borrow 1 from next column (7 -1 1) 7 - 1 = 5 write 5 and borrow 0 from next column • Rules for subtraction in the decimal system: – Begin the subtraction from 2 least significant digits first. – If the minuend is greater than the subtrahend then perform the subtraction. – If the minuend is less than the subtrahend then borrow 1 from the next column. Write down the result of (minuend + 10 - subtrahend). The one borrowed must be subtracted from the minuend of the next column. – Repeat the subtraction for the next columns. ACOE 161 26
Subtraction in any numbering system • Rules for subtraction in a system with base R: The rules for subtraction in a system with base R are the same as in decimal, except that a borrow into a given column adds R units to the minuend digit. Examples: Perform the following subtractions: • (476)10 - (285)10 = (? )10 • (285)10 - (476)10 = (? )10 • (173)8 - (265)8 = (? )8 • (01101001)2 - (00111010)2 = (? )2 • (423)5 - (234)5 = (? )5 • (61 A 9)16 - (C 827)16 = (? )16 • (61 A 9)18 - (C 827)18 = (? )18 ACOE 161 27
Subtraction using the complement’s method • In the previous examples, the subtraction {(285)10 - (476)10 } give a result equal to 809. The correct result is -191. If we examine carefully the subtraction we can observe that the subtraction of the last digit was carried out using a borrow, i. e. the result 809 is obtained after we borrow 1000. If we add 809 and 191 then we get 1000, which is what is borrowed in order to complete the subtraction. The number 809 is said to be the 10’s complement of the number 191. This shows that: – Negative numbers can be represented in the complement’s form. – Subtraction in the decimal system, can be carried out by adding to the minuend the 10’ s complement of the subtrahend. i. e. (285)10 - (476)10 = (285)10 + (-476)10 = (285)10 + [ (523)9’s ++ 1] = (285)10’s + (524)10’s = (809)10’s – Subtraction in a system with base R, can be carried out by adding to the minuend the R’ s complement of the subtrahend. ACOE 161 28
Examples on subtraction using the complement’s method Perform the following subtractions using the R’s complement method: • (476)10 - (285)10 = (? )10 • (285)10 - (476)10 = (? )10 • (173)8 - (265)8 = (? )8 • (01101001)2 - (00111010)2 = (? )2 • (423)5 - (234)5 = (? )5 • (61 A 9)16 - (C 827)16 = (? )16 • (61 A 9)18 - (C 827)18 = (? )18 ACOE 161 29
Homework: Do the necessary ACOE 161 operations to fill up the table below: 30
Negative Number Representation: Signed Magnitude • In the Signed Magnitude representation the most significant bit is used as the sign of the number. The sign bit is zero for positive numbers and one for negative numbers. • SM: 8 means Signed Magnitude with 8 bits (1 for the sign and 7 for the magnitude. • The problem with the SM is that there are two values for zero: (0000000 = +0, and 10000000 = -0). ACOE 161 31
Negative Number Representation: One’s Complement • The Ones Complement representation can be derived from the Signed Magnitude representation. If the number is positive then the one’s complement is the same as the SM. If the number is negative then the one’s complement is obtained by inverting all magnitude bits of the SM. The sign bit is unchanged. • The problem with the One’s Complement is that there are two values for zero: (0000000 = +0, and 1111 = -0). ACOE 161 32
Negative Number Representation: Two’s Complement • If the number is positive then the two’s complement is the same as the SM. If the number is negative then the two’s complement is obtained by adding 1 to the magnitude bits of the one’s complement. The sign bit is unchanged. • The two’s complement is widely used in computers to represent signed integers. In most languages such as Pascal and C an integer variable is represented in a 16 -bit two’s complement representation. ACOE 161 33
Negative Number Representation: Excess 2 N-1 • The Excess 2 N-1 of a number is obtained by adding 2 N-1 to the number and then convert it to binary. Where N is the number of bits used to represent the number. For example if 8 bits are used then 2 N-1 is 27 = 128 and the system is called the Excess 128 system. • Examples: (+38)10 = 128 + 38 = 166 = (10100110)ex-128 (-38)10 = 128 - 38 = 90 = (01011010)ex-128 (10101011) ex-128 = 171 = 128 + 43 = (+43)10 (0101) ex-128 = 85 = 128 -43 = (-43)10 • Note that the 2 N-1 can also be obtained by inverting the sign bit of the two’s complement of the number. ACOE 161 34
Negative Number Representation: Excess (2 N-1 -1) • The Excess (2 N-1 -1)of a number is obtained by adding (2 N-1 -1)to the number and then convert it to binary. Where N is the number of bits used to represent the number. For example if 8 bits are used then (2 N-1 -1) is (27 -1)= 127 and the system is called the Excess 127 system. • Examples: (+38)10 = 127 + 38 = 165 = (10100101)ex-127 (-38)10 = 127 - 38 = 89 = (01011001)ex-127 (10101011) ex-128 = 171 = 128 + 43 = (+43)10 (0101) ex-128 = 85 = 128 -43 = (-43)10 • Note that the (2 N-1 -1) can be obtained by inverting the sign bit of the one’s complement of the number, ONLY if the number is positive. • The (2 N-1 -1) representation is used in computers to represent the exponent of floating point numbers. ACOE 161 35
Negative Number Representation: 16’s Complement • Examples: (+38)10 = 127 + 38 = 165 = (10100101)ex-127 (-38)10 = 127 - 38 = 89 = (01011001)ex-127 (10101011) ex-128 = 171 = 128 + 43 = (+43)10 (0101) ex-128 = 85 = 128 -43 = (-43)10 • The (2 N-1 -1) representation is used in computers to represent the exponent of floating point numbers. ACOE 161 36
Examples • Fill up the table shown below: ACOE 161 37
Homework • Fill up the table shown below: ACOE 161 38
- Slides: 38