data representation

51
Data Representation Cutajar&Cutajar2003 1 DATA REPRESENTATION All data used by computers is in code form, based on the two digits ‘0’ and ‘1’. This is so, in order to reflect the two-state components of which these digital systems are made. Numbers represented using groups of bits (1s and 0s) are said to be BINARY NUMBERS. Binary numbers are said to be in base 2. Before we consider a number of different codes for storing numeric and character data, it is appropriate to recall some mathematical number system concepts that are a prerequisite for our discussion of data representation. The Denary System (RECALL) Let us first take a look at the denary system or the base-10 number system, which we are so accustomed to. The number 3768 is 3 thousand, 7 hundred and sixty eight: 3000 + 700 + 60 + 8 (3 x 1000) + (7 x 100) + (6 x 10) + (8 x 1) M.S.D L.S.D Note the weighting of each digit as we move to the left: 1000 100 10 1 or 10 3 10 2 10 1 10 0 Thus we can write the above number as: (3 x 10 3 ) + (7 x 10 2 ) + (6 x 10 1 )+ (8 x 10 0 ) Remarks : Any number to the power of zero is one The weight to each digit is technically known as the place (positional) value since the weight or value of the digit depends on its place (position) within the number. Hence the place value table of the denary system is 10 4 10 3 10 2 10 1 10 0 10 -1 10 -2 10 -3

Upload: khangminh22

Post on 26-Apr-2023

1 views

Category:

Documents


0 download

TRANSCRIPT

Data Representation

Cutajar&Cutajar2003 1

DATA REPRESENTATION All data used by computers is in code form, based on the two digits ‘0’ and ‘1’. This is so, in order to reflect the two-state components of which these digital systems are made. Numbers represented using groups of bits (1s and 0s) are said to be BINARY NUMBERS. Binary numbers are said to be in base 2. Before we consider a number of different codes for storing numeric and character data, it is appropriate to recall some mathematical number system concepts that are a prerequisite for our discussion of data representation.

The Denary System (RECALL)

Let us first take a look at the denary system or the base-10 number system, which we are so accustomed to. The number 3768 is 3 thousand, 7 hundred and sixty eight:

3000 + 700 + 60 + 8 (3 x 1000) + (7 x 100) + (6 x 10) + (8 x 1)

M.S.D L.S.D

Note the weighting of each digit as we move to the left: … 1000 100 10 1 … or … 103 102 101 100 … Thus we can write the above number as:

(3 x 10 3) + (7 x 10 2) + (6 x 10 1)+ (8 x 10 0) Remarks:

• Any number to the power of zero is one • The weight to each digit is technically known as the place (positional) value

since the weight or value of the digit depends on its place (position) within the number.

• Hence the place value table of the denary system is

… 104 103 102 101 100 10-1 10-2 10-3 …

Data Representation

Cutajar&Cutajar2003 2

Since the base (or radix) is ten, the positional weight is to the power of 10. There are 10 distinct digits to this system. • Digits in a base 10 system are: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 Consider the decimal number 3567.452: 3567.452 = 3000 + 500 + 60 + 7 + 0.4 + 0.05 + 0.002

= (3 x 1000) + (5 x 100) + (6 x 10) + (7 x 1) + (4 x 1/10) + (5 x 1/100) + (2 x 1/1000)

= (3 x 10 3) + (5 x 10 2)+ (6 x 10 1)+ (7 x 10 0)+ (4 x 10 –1)+ (5 x 10 –2) + (2 x 10 –3)

Note that the decimal point separates the whole part of the number (on the left hand side) from the fractional part of the number (the right hand side).

• One can add as many 0’s to the left of the number without in any way changing the number ex. 0004206 = 4206.

• One can SHIFT the number LEFT by inserting a 0 to the RIGHT of the number. Shifting the number left multiplies it by 10 (the base). Thus 42060 = 10*4206.

• One can SHIFT the number RIGHT by one position to DIVIDE it by 10. The LSD which DROPS OFF the right end is the remainder after dividing by 10. Thus 4206 /10 = 420 r 6.

• Unless the number base is clearly understood, a given number must have its base specified as a subscript at the end of the number, say 30510.

The basic concepts of number systems recalled in this section will now be used to introduce you to other number systems that are more convenient for use in a digital environment.

The Binary System

Number representation in a digital system can take only two values 1 or 0 because of the physical characteristics of the components of which it is built (as already pointed out). A brief explanation of actual physical representation of 0 and 1 values may be found in appendix A. In the binary system: • The base (radix) of the binary system is 2. • Hence, there are two distinct digits: 0 and 1.

Data Representation

Cutajar&Cutajar2003 3

• A single binary digit is called a bit – BInary digiT = BIT. • Place Value table (Remember concept of place value):

………… 28 27 26 25 24 23 22 21 20 . 2-1 2-2 2-3 2-4 …………

256 128 64 32 16 8 4 2 1 . 1/2 1/4 1/8 1/16 …

Example 1. : The number 1101.012 would represent: (1 x 2 3 ) + (1 x 2 2 ) + (0 x 2 1 ) + (1 x 2 0 ) + (0 x 2 –1 ) + (1 x 2 –2) i.e. (1 x 8) + (1 x 4) + (0 x 2) + (1 x 1) + (0 x ½) + (1 x ¼)

= 13.2510 ∴ 1101.012 = 13.2510.

Conversion: binary →→→→ decimal

Example 1: 11012 = (1 х 23)+ (1 х 22

) + (1 х 21 ) + (1 х 20)

= 8 + 4 + 0 + 1 = 1310 Example 2: 0.10112 = (1 х 2-1)+ (0 х 2-2

) + (1 х 2-3 ) + (1 х 2-4)

= 0.5 + 0 + 0.125 + 0.0625 = 0.937510 Example 3: 1.0112 = (1 х 21)+ (0 х 2-1

) + (1 х 2-2 ) + (1 х 2-3)

= 1 + 0 + 0.25 + 0.125 = 1.37510 Exercise 1: Convert the following binary numbers to decimal:

i. 1101011.0112 (Ans. 107.37510)

Integral numbers Fractional numbers

Binary point

Data Representation

Cutajar&Cutajar2003 4

ii. 10110111.00112 (Ans. 183.187510) iii. 101101.010112 (Ans. 45.3437510) iv. 101000.0000112 (Ans. 40.04687510)

Conversion: decimal →→→→ binary

i) by inspection ii) systematic method: repeated division/multiplication

In converting from decimal to binary, the integer part and the fractional part of the number are processed separately. For the integer part, division by 2 is repeated on the quotient only until the quotient becomes zero and the equivalent binary integer number part is obtained by taking the remainders. For the fractional part, multiplication by 2 is repeated on the fractional part until the fractional part becomes zero. For each successive multiplication, only the fractional part of the previous result is multiplied by the base 2. The equivalent binary fraction is obtained by taking the whole part of each intermediate multiplication result. After a number of multiplications the fractional part may not have become zero. Hence, the process is often terminated when the desired number of digits is obtained. Example 1: (by inspection) 1210 = 8 + 4 = (1 х 23) +(1 х 22) + (0 х 21) + (0 х 20) = 11002 Example 2: 0.87510 (by inspection) 0.87510 = 0.5 + 0.25 +0.125 = (1 х 2-1) +(1 х 2-2) + (0 х 2-3) = 0.1112 Example 3: (by inspection) 17.7510 = 16 + 1 + 0.5 + 0.25 = (1 х 24) +(0 х 23) + (0 х 22) + (0 х 21) + (1 х 20) +(1 х 2-1) + (1 х 2-2) = 10001.112 Exercise 2: Using inspection, show that 22.37510 = 10110.0112

Data Representation

Cutajar&Cutajar2003 5

Example 4: Convert 0.562510 to binary using repeated multiplication

0.5625 х 2 = 1.1250 0.1250 х 2 = 0.2500 0.2500 х 2 = 0.5000 0.5000 х 2 = 1.0000

∴ 0.562510 = 0.10012 Example 5: 13.812510

Converting integral part using inspection: 13 = 8 + 4 +1 = 11012

When converting the fractional part, if inspection seems difficult, use systematic method. Converting fractional part using repeated multiplication:

0.8125 х 2 = 1.6250 0.6250 х 2 = 1.2500 0.2500 х 2 = 0.5000 0.5000 х 2 = 1.0000

⇒0.812510 = 0.11012 ∴ 13.812510 = 1101.11012

Example 6: 145.92510 Converting the integer part 14510 into binary using repeated division: Note that integral part may always be easily converted using inspection. This example is only worked out using inspection for the sake of completeness

2 145

2 72 Remainder 1 ←gives any odd 20 (1) when grouping in 2s

2 36 Remainder 0 ←gives any odd 21 (2s) when grouping in 4s

2 18 Remainder 0 ←gives any odd 22 (4s) when grouping in 8s

2 9 Remainder 0 ←gives any odd 23 (8) when grouping in 16s

2 4 Remainder 1 ←gives any odd 24 (16) when grouping in 32s

2 2 Remainder 0 ←gives any odd 25 (32) when grouping in 64s

2 1 Remainder 0 ←gives any odd 26 (64) when grouping in 128s

0 Remainder 1 ←gives any odd 27 (128) when grouping in 256s

∴ 14510 = 10010001 (write remainders ↑ )

Data Representation

Cutajar&Cutajar2003 6

Converting 0.92510 to binary using repeated multiplication: Note the recurrence! ⇒0.92510 = 0.111 0110 0110 …2

∴145.92510 = 10010001.111011001100 …2

Exercise 3: Convert the following decimal numbers to binary:

i. 10.01562510 (Ans. 1010.0000012) ii. 21.12510 (Ans. 10101.0012)

iii. 1339.562510 (Ans. 10100111011.10012) iv. 43.437510 (Ans. 111111.01112)

Hexadecimal System Imagine having to remember and handle long strings of bits such as 0110110111010111. In computing, it is at times necessary to inspect and manipulate bit strings, for example when inspecting memory contents. In order to make it easier to manipulate binary strings it is usual to use a notation that could be easily translated to binary when required and yet more concise. The hexadecimal system (hex) matches such criteria and often, binary strings are expressed in hex. • The base of the hexadecimal system is 16. • Hence 16 digit symbols are required:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

Thus the hexadecimal digits are: Hex 0 1 2 3 4 5 6 7 8 9 A B C D E FDec. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Bin. 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

• Place Value table of hexadecimal system:

0.925 × 2 = 1.850 There was 1/2 in the original number (× 2) 0.850 × 2 = 1.700 There was 1/4 in the original number (× 2× 2) 0.700 × 2 = 1.400 There was 1/8 in the original number (× 2× 2× 2) 0.400 × 2 = 0.800 There was 1/16 in the original number (× 2× 2× 2× 2) .800 × 2 = 1.600 There was 1/32 in the original number (× 2× 2× 2× 2 …) 0.600 × 2 = 1.200 There was 1/64 in the original number (× 2× 2× 2× 2 …) 0.200 × 2 = 0.400 There was 1/128 in the original number (× 2× 2× 2× 2 …) 0.400 × 2 = 0.800 There was 1/256 in the original number (× 2× 2× 2× 2 …) 0.800 × 2 = 1.600 There was 1/512 in the original number (× 2× 2× 2× 2 …) …

Data Representation

Cutajar&Cutajar2003 7

… 163 162 161 160 . 16-1 16-2 16-3 …

… 4096 256 16 1 . 1/16 1/256 1/4096 …

Conversion: Hexadecimal →→→→ Decimal Example 1: Convert A0B16 to decimal: A0B16 = (A х 162) + (0 х 161) + (B х 160) = (102 х 56) + (0 х 16) + (11 х 1) = 2560 + 0 + 11 = 257110 Example 2: Convert 4CF.CD16 to decimal. 4CF.CD16 = (4 х 162) + (C х 161) + (F х 160) + (C х 16-1) + (D х 16-2) = (4 х 256) + (12 х 16) + (15 х 1) + (12 х 1/16) + (13 х 1/256) = 1231.7510

Exercise 4: Convert the following Hexadecimal numbers to decimal:

i. 26C.5816 (Ans. 620.3437510) ii. 3A4.0C16 (Ans. 932.04687510)

Conversion decimal →→→→ hexadecimal To convert from decimal to hexadecimal the same procedure as for binary numbers is adopted, but this time instead of 2 we use 16, as the hex system is base 16. Conversion again requires the processing of the integer and fractional parts separately. For the integer part, division by 16 is repeated on the quotient until the quotient becomes zero and the binary number is taken from the remainders of the successive divisions. For the fractional part, multiplication by 16 is repeated on the fractional part only, taking the integer part of the successive multiplications as the result. This process is repeated until either the fractional part becomes zero, or when the desired significant figures are obtained.

Integer part Fractional part

Hexadecimal point

Data Representation

Cutajar&Cutajar2003 8

Example 1: Convert 498010 to hexadecimal By repeated division:

∴498010 = 37416

Example 2: Convert 0.17610 to hexadecimal. By repeated multiplication:

0.176 * 16 = 2.8160 2 0.816 * 16 = 1.2456 1 0.2456 * 16 = 3.9296 3 0.9296 * 16 = 14.8736 E … …

∴0.17610 = 0.213E …16 Exercise 5: Convert the following decimal numbers to Hexadecimal:

i. 1103.187510 (Ans. 44F.316) ii. 174.562510 (Ans. AE.916)

iii. 3417.01562510 (Ans. D59.0416)

Conversion binary ↔↔↔↔ hexadecimal

Note the relationship between binary system and hexadecimal system: binary … 128 64 32 16 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32 … hex … 16 1 . 1/16 …

Consider the binary number 11010101001.101101 Converting from binary to hexadecimal: • We first divide the number into groups of four starting from the binary point.

Add (insignificant) 0s to make to the left and right of the number if necessary: 0110 1010 1001 . 1011 0100

4980

311

19

0

16

16

16

Rem 4

Rem 7

Rem 3

Data Representation

Cutajar&Cutajar2003 9

• Convert each group of four binary digits to a single hex digit:

Remember: Hex 0 1 2 3 4 5 6 7 8 9 A B C D E FBin. 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Hence, 0110 1010 1001 . 1011 0100 Becomes 6 A 9 . B 4 Converting from hexadecimal to binary: • Substitute each hex digit into its 4-bit binary equivalent.

Hence, D916 = 1101 10012 Exercise 6: Convert the following Hexadecimal numbers to binary:

i. AFC216 (Ans. : 1010 1111 1100 00102) ii. 136C16 (Ans. 1 0011 0110 11002)

iii. 7FA16 (Ans. 0111 1111 10102) iv. FF6016 (Ans. 1111 1111 0110 00002)

Octal System • The base of the octal system is 8. • Hence 8 digit symbols are required: 0, 1, 2, 3, 4, 5, 6, 7

• Place Value table for octal system: ………… 84 83 82 81 80 . 8-1 8-2 8-3 8-4 …………

4096 512 64 8 1 . 1/8 1/64 1/512 1/4096 …

Conversion: octal →→→→ decimal Example 1: 1018 1018 = (1х82) + (0 х 81) + (1 х 80) = 64 + 0 + 1 = 65

Integer part Fractional part

Octal point

Data Representation

Cutajar&Cutajar2003 10

Example 2: 45728 45728 = (4 х 83) + (5 х 82) + (7 х 81) + (2 х 80) = 2048 + 320 + 56 + 2 = 242610 Example 3: 45.78

45.78 = (4 х 81) + (5 х 80) + (7 х 8-1) = 32 + 5 + 7/8 = 37.87510 Exercise 7:

Convert the following octal numbers to decimal: i. 231.018 (Ans. 153.01562510)

ii. 71.448 (Ans. 57.562510) iii. 326.038 (Ans. 214.04687510)

Conversion: decimal →→→→ octal i) by inspection ii) by repeated division/multiplication Example 1: 1010 = 8 + 2 = 128 Example 2: Convert 78210 to octal by repeated division.

8 782

8 95 Rem 2

8 11 Rem 7

8 1 Rem 3

= Rem 1 ∴ 78210 = 13728

Example 3: Convert 0.486410 by repeated multiplication.

0.4864 * 8 = 3.8912 0.8912 * 8 = 7.1296 0.1296 * 8 = 1.0368 0.0368 * 8 = 0.2944 0.2944 * 8 = 2.3552 0.3552 * 8 = 2.8416

Data Representation

Cutajar&Cutajar2003 11

0.8416 * 8 = 6.7328 0.7328 * 8 = 5.8624 0.8624 * 8 = 6.9992 0.9992 * 8 = 7.9936 …

∴ 0.486410 = 0.3710226567 … Example 4: 409.37510

8 409 0.375 * 8 = 3.000

8 51 Rem 1

8 6 Rem 3

= Rem 6 ∴40910 = 6318 0.37510 = 0.38

∴ 409.375 10 = 631.38 Exercise 8:

Convert the following decimal numbers to octal: i. 229.3437510 (Ans. 345.268)

ii. 467.187510 (Ans. 723.148) iii. 45.125 10 (Ans. 55. 18)

Conversion: Binary ↔↔↔↔ Octal

Binary digits 000 001 010 011 100 101 110 111 Octal digits 0 1 2 3 4 5 6 7

Consider the binary number 11010101001.10111

Converting from binary to octal: • We first divide the number into groups of three starting from the binary point

and adding insignificant 0s to the left and right of the number if necessary 011 010 101 001 . 101 110

• Converting each group to a single digit becomes 3 2 5 1 . 5 6

Converting from octal to binary: • Substitute each octal digit with the 3-bit binary equivalent. Hence, 3318 = 110110012

Data Representation

Cutajar&Cutajar2003 12

Exercise 9: Convert the following octal numbers to binary:

i. 50728 (Ans. 101 000 111 0102) ii. 23458 (Ans. 10 011 100 1012)

iii. 75318 (Ans. 111 101 011 0012) iv. 7658 (Ans. 111 110 1012)

Remarks: • To convert from octal to hex: octal → binary → hex • To convert from hex to octal: hex → binary → octal

Conversion Summary

Decimal

10

Binary

2

Octal

8

Hex 16

In groups of 4 and convert each group

Continuous division /

multiplication by 8

Continuous division /

multiplication by 16

In groups of 3 and convert each group

Continuous division /

multiplication by 2

Digit(n) x 8n and add Digit(n) x 16n and add

Digit(n) x 2n and add

Convert each digit to 4 bit binary

Convert each digit to 3 bit binary

Data Representation

Cutajar&Cutajar2003 13

Binary Quantities. • 1 Binary digit is called a bit and can have either a value of zero or 1 • 4 bits are called a nibble. • 8 bits are called a byte • 16 bits are called a word • 32 bits are called a double word (Dword) • 64 bits are called a quad word (Qword) • 1024 bytes are called 1 Kilo Byte (1K or 1Kb) • 1024 x 1024 bytes = 1024 K bytes are called 1 Mega Byte (1M or 1 Mb) • 1024 x 1024 x 1024 bytes = 1024 Mbytes = 1 Giga Byte (1G or 1 Gb)

• 1024 x 1024 x 1024 x 1024 bytes = 1024 Gbytes = 1 Tera Byte

Value ranges for unsigned numbers. A single bit can represent one of TWO POSSIBLE values. Using one bit we can

only represent any data that can have only two possible values, ex. 1 and

0,True and False, Male and Female.

Suppose our data are seasons. Clearly one bit is not enough, because a single

bit can represent only 2 things, and there are four seasons. So one must use

more than one bit. Suppose one uses 2 bits. The latter give four possible

PATTERNS – which can be used to represent the four different seasons. The

seasons are in human readable form. Inside the computer they will be

represented in MACHINE READABLE FORM as a code of 2 signals (2 bits)

Using bits to represent positive whole numbers (unsigned integers) Consider the following: - With a single bit just 2 values can be represented 0 or 1 - With 2 bits, 4 values can be represented 00, 01, 10 or 11 (0, 1, 2, 3) - With 3 bits, 8 values can be represented

000, 001, 010, 011, 100, 101, 110 or 111 (0, 1, 2, 3, 4, 5, 6, 7)

b b b b b b b b b

Least Significant Bit (L.S.B) – the bit with

the least weight

Most Significant Bit (M.S.B) – the bit with the

highest weight

Data Representation

Cutajar&Cutajar2003 14

- By analyzing the above sequence one can notice that given n bits, the values that can be represented are 2n values in number, ranging from 0 to (2n – 1).

- Verification with 4 bits:

One can represent 24 values = 16. These values range from 0 to (24 – 1) = 0 to 15:

0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111.

Remark: Note that unlike standard science values where 1 K represents exactly 1,000 and 1 M exactly 1,000,000, in computing these quantities are different. - 1 Kilo = 210 = 1,024 - 1 Mega = 210 x 210 = 220 = 1,024x 1,024 = 1,048,576 - 1 Giga = 210 x 210 x 210 = 230 = 1024x1024x1024 = 1.0737 x 109

Representations in binary. Let us now consider how many bits are needed to represent a certain number of values in binary. We derived that n bits can represent 2n values. Let us call these X values. Thus, X = 2n

∴Taking logs10 on both sides

∴log10(X) = log10(2n)

∴log10(X) = n * log10(2)

∴n = log10(X) log10(2)

Hence n = Log10(x)

Log10(2) Example 1:

(a) How many numbers can be expressed using 16 bits? (b) What is the range of values that can be represented using 16 bits?

(a) There are 2n distinct numbers that can be expressed using n bits

Given n = 16

Data Representation

Cutajar&Cutajar2003 15

⇒216 = 65536 numbers can be expressed. (b) The range of these numbers is from 0 to 2n-1

Given n = 16 ⇒range: 0 to 216-1 i.e. 0 to 65535

Example 2:

How many bits are needed to be able to express 32768 numbers

Given, n = Log10(X) / Log10(2) where X = number of distinct values needed

n = number of bits

hence, n = log10(32768) / log10(2) = 4.5154 / 0.301

⇒15 bits Remark: In general, the result is not an integer number but n must be an integer number. (number of bits!!). Thus if the result is not a whole number we take the nearest whole number higher than the result. Example3:

How many bits are required to be able to represent 9 numbers:

n = log10(X) / log10(2)

but X = 9

hence,

n = log10(9) / log10(2)

= 0.954 / 0.301

= 3.17

3.17 is rounded to 4 bits (upper bound). In fact with just 3 bits one can

represent only from 0 to 7 as seen earlier!

Shortcut to avoid logs. It is quite easy to remember the first 2n numbers where n = 1,2, .. 9,

10,20,30.

N 0 1 2 3 4 5 6 7 8 9 10 ………… 20 30

2n 1 2 4 8 16 32 64 128 256 512 1 K ………… 1 1

Data Representation

Cutajar&Cutajar2003 16

M G

Using the property that 2(x+y) = 2x . 2y

We can convert the number into K, M or G and calculate accordingly.

Example 1: Required, number of bits needed to represent 256 numbers: Directly from table,

we can see that the number of bits is 8.

Example 2: Required, the number of bits needed to represent 131072 numbers:

131072 / 1024 = 128 K

hence, 131072 = 128 . 210

From the table we see that 128 = 27

Hence, 131072 = 27. 2 10

= 217 Thus we need 17 bits to represent 131072 numbers.

Arithmetic operations in different number systems. All four arithmetic operations can be done directly without converting to decimal. Let us consider the following operations in different number systems:

− addition − multiplication − subtraction − division

Data Representation

Cutajar&Cutajar2003 17

1. Addition Addition in binary.

Carry every group of two! Example 1: 110111 + 1110 Remember to align binary point!

1 1 1 1

1 1 0 1 1 1 + 1 1 1 0

1 0 0 0 1 0 1 Let us consider all possible combinations that can occur when adding two binary digits:

Carry brought forward

Digit A

Digit B

Sum

Carry carried forward

0 0 0 0 0

0 0 1 1 0

0 1 0 1 0

0 1 1 0 1

1 0 0 1 0

1 0 1 0 1

1 1 0 0 1

1 1 1 1 1

Addition in octal.

Carry every group of eight! Example 1: 412 + 7507 Remember to align octal point. 1 1 1 4 1 2 + 7 5 0 7

1 0 1 2 1

Carry (Group of 2)

Carry (group of 8)

Data Representation

Cutajar&Cutajar2003 18

Addition in hex. Carry every group of sixteen! Example 1: A18 + F7 Remember to align hexadecimal point. 1 A 1 8 + F 7

B 0 F Exercise 10: Add the following binary numbers:

i. 100112 + 100112 + 1010112 (Ans. 10100012) ii. 1110102 + 0110112 (Ans. 10101012)

2. Subtraction. Subtraction in binary.

Borrow a group of 2!

Example 1: 1 1 0 0 1 1 – 1 1 1 0 1 (2) (1) (2) 1 1 0 0 1 1 - 1 1 1 0 1

= 1 0 1 1 0 Subtraction in octal. Borrow a group of 8! Example 1: 5016 – 765 (7) (8) 5 0 1 6 - 7 6 5

4 0 3 1

Borrow (group of 2)

Data Representation

Cutajar&Cutajar2003 19

Subtraction in hex. Borrow a group of 16! Example 1: 421 – A6 (16) (16) 4 2 1 - A 6

3 7 B Exercise 11: Subtract the following binary numbers:

i. 1000000002 – 10011002 (Ans. 101101002) ii. 11112 -01012 (Ans. 10102)

iii. 1110112 -0101012 (Ans. 1001102) iv. 100112 – 11112 (Ans. 01002)

3. Multiplication. Remember the long multiplication technique used in decimal multiplication: We multiply the multiplicand with each digit of the multiplier in turn; then sum up these partial products to produce the result. Multiplication in binary.

In binary this process is much easier since the digits of the multiplier can only be either a 0 or 1. Thus the partial products are either 0 or equal to the multiplier with added right-trailing zeros if necessary. Hence,

- if the bit is 1 then the multiplicand is copied beneath the bit in question. - if the bit is 0 then 0’s are entered in that position

1 0 0 1 x (9) 1 0 1 1 (11)

1 0 0 1 1 0 0 1 0 Partial products 0 0 0 0 0 0 1 0 0 1 0 0 0

1 1 0 0 0 1 1 (99) product

Data Representation

Cutajar&Cutajar2003 20

The 4-bit multiplication aside can have at most an 8-bit result

Exercise 12: Multiply the following binary numbers:

i. 10101112 * 10112 (Ans. 11101111012) ii. 110102 * 10112 (Ans. 1000111102)

iii. 11102 * 11012 (Ans. 101101102) iv. 1012 * 102 * 1012 (Ans. 1100102)

4. Division. When performing long division on decimal numbers, we perform the following

actions:

− Try dividing the first digit of the dividend by the divisor to see if it fits. − If it fits, then we find out by how much it fits, write this in the quotient and

then subtract the divisor multiplied by how much it fits from the considered part of the dividend.

− We bring another digit of the dividend and repeat the same procedure until no further digits remain in the dividend.

The remaining number is the remainder of the division.

86

79 245634 237

3109

711

79

734

23

Quotient Divisor

Dividend

Remainder

Data Representation

Cutajar&Cutajar2003 21

Division in binary.

In binary, this process is easier since it either doesn’t fit or just fit once. Performing the binary long division below:

− Try dividing 1 by 1101 and it doesn’t fit so we successively try dividing 10, 100 and 1000 by 1101 without success.

We then find 1101 fits in 10001 so we write 1 in the quotient and subtract 1101 from 10001 which leaves 100. We bring another digit of the dividend and repeat the same procedure until no further digits remain in the dividend. The remaining number is the remainder of the division Exercise 13:

Divide the following binary numbers: i. 100102 / 102 (Ans. 10012)

ii. 111000012 / 1012 (Ans. 1011012) iii. 10010111012 / 11102 (Ans. 1010112 R 112) iv. 101110102 / 11102 (Ans. 11012 R 1002)

1101 1101

10101

10000

1101

1101

1110

1

Quotient

Divisor

Dividend

Remainder

100010010

Data Representation

Cutajar&Cutajar2003 22

Numeric Codes. Unsigned number representation. Binary Code for unsigned numbers. In order to represent unsigned numbers, code based on binary number system is used. Given an 8-bit register to store unsigned binary integer numbers:

27 26 25 24 23 22 21 20 binary point

.

Or 128 64 32 16 8 4 2 1 binary point

.

Example 1:

0 1 1 0 1 0 1 0

Represents 0 + 64 + 32 + 0 + 8 + 0 + 2 + 0 = 10610 In general, given an n-bit register, the value of each register bit would be:

2n 2n-1 22 21 20

… .

Given an n-bit register to store unsigned binary fractional numbers.

Binary point 2-1 2-2 2-3 2-4 2-n+2 2-n+1 2-n

. …

Example1: Given that the 8-bit register stores an unsigned fraction:

0 1 1 0 1 0 1 0

The decimal fraction stored is: 0 + 1/4 + 1/8 + 0 + 1/32 + 0 + 1/128 + 0 = 0.4140625

Data Representation

Cutajar&Cutajar2003 23

Binary Coded Decimal Code (BCD code)

BCD code is usually used to store unsigned numbers. Given a denary number, each (decimal) digit is coded separately in binary. Since there are 10 decimal digits, a minimum of 4 bits is required to store each of these ten digits.

Decimal digit Binary equivalent

0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001

Example 1: Represent 57110 in BCD code. From the above table, 5 is replaced by 0101 7 is replaced by 0111 1 is replaced by 0001

∴5 7 110 ≡ 0101 0111 0001 in BCD code Because the 4-bit binary equivalent number is being used to code each digit, this form of BCD code is generally known as the BCD 8421-weighted code. Exercise 14: Represent 93710 in BCD code. (Ans. 1001 0011 0111 in BCD code) Remarks: i) There exist other BCD codes with different weightings say BCD 2421-weighted

code. Note that with these 2421 weights you may still obtain the required ten decimal digits.

ii) Since four bits are the minimum number of bits possible to code each of the ten decimal digits, we speak of a PACKED BCD. Two BCD coded digits are packed in a single byte (8 bits).

Data Representation

Cutajar&Cutajar2003 24

iii) A number to be coded in BCD must be in decimal form. If not, we must first convert the number in question into decimal and then code (each decimal digit) in BCD code.

iv) Whereas conversion to/from BCD code to denary is simple (even in implementation circuitry) [ADVANTAGE], arithmetic using BCD coded operands is quite complex [DISADVANTAGE]. Hence BCD codes are used in (commercial) applications where there is the likelihood that a large input/output is required and only simple calculations are involved. (Ex. advertisement running displays, pocket calculators). Also not so efficient since 6 representations are not being used (wasted).

How is the unary sign (±) represented in actual computers? The numeric codes to be now discussed will provide methods for representing signed numbers. Signed number representation. Two numeric codes used to represent signed numbers in a computer system are:

• Sign & Magnitude code • Two’s Complement code

Sign and Magnitude Code. Using this notation, the sign and the magnitude of the number are coded separately. Hence a bit, called the sign bit is used to code the sign of the number, while a number of bits are used to represent the magnitude of the number. Consider, Representing the sign of the number and its magnitude separately.

+ 17

sign magnitude

Magnitude (absolute value) of the number

Sign Bit 0: Positive Number 1: Negative Number

Data Representation

Cutajar&Cutajar2003 25

.

Example 1: Represent + 17 in an 8-bit register using sign-and-magnitude code.

Hence also represent –17.

Representing -17 using 8 bits:

1 0 0 1 0 0 0 1 Note that the code for ‘–17’ is the same as that for ‘+17’ except for the sign bit (since magnitude is the same). Example 2: Represent 0.40625 in an 8-bit register using S-M code 0.40625 (13/32) = 1/4 + 1/8 + 1/32 Sign

bit 1/2

1/4

1/8

1/16

1/32

1/64

1/128

∴ + 0.40625: 0 0 1 1 0 1 0 0 and, - 0.40625: 1 0 1 1 0 1 0 0

Remarks: i) If 8 bits are available to represent signed numbers, then the range of numbers

that can be represented is: ii)

1111 1111 - 0111 1111 i.e. [-127 , +127] Biggest magnitude -ve number

Biggest +ve possible number

In general, the range of an n-bit register is: [-2 n-1 + 1, +2 n-1 – 1]

and is symmetric.

Appended zeroes

Binary equivalent Sign bit

0 0 0 1 0 0 0 1

Data Representation

Cutajar&Cutajar2003 26

iii) Note that there are TWO representations of 0, i.e. there is: The positive zero: - say, 0 0000 on five bits including sign The negative zero: - say, 1 0000 on five bits including sign

iv) Arithmetic operations cannot be performed directly on a negative number

represented in Sign and Magnitude. Example: Consider the addition of +5 and –3 assuming a 5-bit register.

Exercise 15: 1. Using an 8-bit register storing integers represent the following in sign and

magnitude format: i. 4 and -4 (Ans. 0000 0100, 1000 0100)

ii. 13 and -13 (Ans. 0000 1101, 1000 1101) iii. 30 and -30 (Ans. 0001 1110, 1001 1110) iv. 123 and -123 (Ans. 0111 1011, 1111 1011)

2. Using an 8-bit register storing fractions represent the following in sign and

magnitude format: i. 0.046875 and -0.046875 (Ans. 0.000011, 1.000011)

ii. 0.015625 and -0.015625 (Ans. 0.000001, 1.000001) iii. 0.5625 and -0.5625 (Ans. 0.1001, 1.1001)

Two’s Complement Code. Consider an 8-bit register:

-128 64 32 16 8 4 2 1

+5 0 0101 + -3 1 0011 -8 1 1000

This is evidently a wrong answer

Normal binary

(positive) place values

MSB represents

negative quantity

Data Representation

Cutajar&Cutajar2003 27

Decimal Number

Two’s complement code

+127 0111 1111 Μ +1 0000 0001 0 0000 0000 (-128+127): -1 1111 1111 (-128+126): -2 1111 1110 … … … (-128 + 1): -127 1000 0001 -128 1000 000

Remarks: i) If 8 bits are available to represent signed numbers, then the range of numbers

that can be represented is: 1000 0000 - 0111 1111 i.e. [-128, +127] In general, the range of an n-bit register is: [-2 n-1, + 2 n-1 – 1]

and is not symmetric. There is always one more negative then positive numbers. ii) For positive numbers two’s complement code yields the same code as for the

sign-and-magnitude representation. iii) Quick method to represent negative numbers in two’s complement code:

Step 1: Convert number into binary using the required number of bits Step 2: Change all 0s → 1s and 1s → 0s Step 3: Add 1 (to LSB)

Example 1: Represent -126 in an 8-bit register using two’s complement code.

(1) +126: 0111 1110

(2) 1000 0001 (3) 1 +

∴ -126 → 1000 0010

Note that this method also converts a number in twos complement negative form to positive form. (Algorithm toggles representation by successive applications)

Data Representation

Cutajar&Cutajar2003 28

Example 2: -17 → 1110 1111

(1) 0001 0000 (2) 1 + (3) 0001 0001 ⇒ + 17

[Fastest informal method: starting from the LSB copy any trailing 1s (up to the first 1 from the right) then change the rest of the digits from 1→ 0 and from 0→1.] Example 3: Represent 0.40625 in an 8-bit register using two’s complement code.

-1

1/2

1/4

1/8

1/16

1/32

1/64

1/128

0.40625 (13/32) = 1/4 + 1/8 + 1/32

∴ + 0.40625: 0 0 1 1 0 1 0 0 and, 1 1 0 0 1 0 1 1 + 1 - 0.40625: 1 1 0 0 1 1 0 0

iv) There is one representation of 0 i.e.

The positive zero 0 0000 on five bits including sign (Adding 1 to 1 1111 results again in 0 0000)

v) Arithmetic operations can be performed directly on a negative number

represented in twos complement Example 1: Consider the addition of +5 and –3 on 5 bits including sign. +5 0 01 0 1 +

-3 1 1 1 0 1 +2 0 00 1 0

1

The carry out from the sign bit is simply ignored here

The result is directly correct.

Data Representation

Cutajar&Cutajar2003 29

Example 2: Perform the following subtraction using two’s complement: 29 – 7

Rem: 29 – 7 = 29 + (-7) Using an 8 bit register: 29 : 0001 1101 -7 : 1111 1001 +

adding: _______ (ignore carry)1↵ 0001 0110→ required result ( +22)

Example 3: Perform the following subtraction using two’s complement: 29 - 31

Rem: 29 – 31 = 29 + (-31)

Using an 8 bit register: 29 : 0001 1101 -31 : 1110 0001 +

adding: _______ negative result ⇐ no carry ⇐ 0↵ 1111 1110→ required result (-210) When adding numbers held in two’s complement one uses the same binary addition rules but ignores the carry out. Addition of numbers with a different sign never produces an overflow. The latter can only occur when adding numbers having the same sign (both +ve or both –ve) and the result has a sign different from the signs of the addends. When addition overflow occurs the carries into and out of the sign bit are different.

vi) A matter that is confusing when interpreting signed binary numbers is the

interpretation of negative numbers. One must be careful that:

� If the number starts with 0 (i.e. sign bit = 0) it is positive and can be read directly.

� If the number starts with 1 (i.e. sign bit = 1) it cannot be read directly if not in S&M. It must be reconverted to positive to be able to read it in binary, and then add the negative sign

[An alternative is from first principles by noting the place value headings of each bit. Remember that the MSB is negative whereas the rest of the bits are positive.]

Data Representation

Cutajar&Cutajar2003 30

Exercise 16: 1. Using an 8-bit register storing integers represent the following in two’s

complement format: i. 22 and -22 (Ans. 0001 01102, 1110 10102)

ii. 120 and -120 (Ans. 0111 10002, 1000 10002) iii. 51 and -51 (Ans. 0011 00112, 1100 11012)

2. Evaluate the following binary numbers stored in two’s complement format:

i. 1000 11002 (Ans. -11610)

ii. 0010 00102 (Ans. 3410) iii. 1010 10102 (Ans. -8610)

3. Perform the following subtractions using two’s complement 8-bit registers. Detect also whether overflow occurs in anywhere:

i. 13 – 4 (Ans. 0000 10012) ii. 4 – 13 (Ans. 1111 01112)

iii. -4 -13 (Ans. 1110 11112) iv. -128 – 99 (Ans. 0001 11012 Overflow) v. 64 + 66 (Ans. 1000 00102 Overflow)

Numeric Data Representation Fixed-point representation.

In the methods of coding described above – sign and magnitude code, twos complement code – both for integers and fractions, the binary point has been in a fixed position

For integers – at the extreme right of the register For fractions – after the MSB

This type of number representation is called FIXED-POINT BINARY REPRESENTATION. Fixed-point binary representations are used to represent integer numbers in computers. Though the representation is to full accuracy, however the range of values that may be represented in a given register is very limited.

Assuming twos complement code: Given an 8-bit register, range [-128, +127] For a 16-bit register, range [-32,768, +32,767]

This is not sufficient for many applications!

Data Representation

Cutajar&Cutajar2003 31

Floating-point representation. Floating point representation extends the range of numbers that can be represented by a given number of bits. As in pocket calculators, when the numbers get very large or very small (fractions) the display turns in exponent form and for example 9,000,000,000 would be shown as 9 x 109 or 0.000000009 would be displayed as 9 x 10-9 Floating point representation is similar in nature to this scientific notation (standard form of number representation) Assuming decimal numbers: 148.95 = 0.14895 * 103 = 1.4895 * 102 = … 0.014895 = 0.14895 * 10-1 = 1.4895 * 10-2 = … Floating point representation uses the same principle, but obviously in base 2. Say, 1011.1 = 0.10111 * 24 Consider a register used to represent floating point numbers; some bits are used to represent the mantissa, while the rest of the bits are used to represent the exponent. Consider a 12-bit register in which 8 bits are used to represent the fractional part of the floating-point number and 4 bits are used to represent the exponent part of the number.

8 bit 4 bit MANTISSA EXPONENT Using this format of the 12 bit register: 1 0 1 1 . 1 = 0 . 1 0 1 1 1 * 2 4 0 . 1 0 1 1 1 0 0 0 1 0 0

exponent Fractional

part:

Mantissa

Data Representation

Cutajar&Cutajar2003 32

Example 1: Represent +13.75 as a floating point number using a 12 bit register where the first 8 bits are used to code the mantissa in twos complement and the remaining bits represent the exponent in twos complement code. Step 1: convert +13.75 to binary 13.75 = 1 1 0 1 . 1 1 Step 2: represent the resulting binary number in ‘standard’ form i.e. 1 1 0 1 . 1 1 = 0 . 1 1 0 1 1 1 * 2 100 (4) Step 3: write mantissa and exponent using available bits i.e. 0 1 1 0 1 1 1 0 0 1 0 0 Step 4: if mantissa /exponent is negative, complement the relevant group of bits (not applicable)

∴ + 13.75 = 0 1 1 0 1 1 1 0 0 1 0 0 Example 2: Using the same register format as in example 1, represent –13.75. Steps 1-2-3 remain the same as in example 1. Step 4: given + 13.75 = 0 1 1 0 1 1 1 0 0 1 0 0 ∴ - 13.75 = 1 0 0 1 0 0 1 0 0 1 0 0 Example 3: Using the above 12-bit register format, represent –17.375 in floating point.

- 17.37510 = -10001.0112 -10001.011 = -0.10001011 * 2101 (+5) Note: Mantissa: -ve Exponent: +ve -0.100 0101 +0101 ⇒ 1011 1011 0101

+ve exponent remains unchanged

Complement –ve mantissa

Data Representation

Cutajar&Cutajar2003 33

Remarks: In the above examples, the mantissa is a fraction between 1/2 and 1. In fact if you follow the above steps, ‘represent binary number in standard form’ ensures that (but for some exceptional cases) the fraction is always between 1/2 and 1. When the mantissa of a floating-point number is expressed as a fraction between 1/2 and 1, the floating-point number representation is said to be NORMALISED. Normalisation ensures (i) single representation of a number

0.100000 0001: +1 - normalised 0.010000 0010: +1 0.001000 0011: +1

(ii) the maximum number of bits are available to store the fractional part of

the number

Consider 10111.011

0.0001011 (1011) 0111 - not normalised 4 bits lost 0.1011101 (1) 0101 - normalised

1 bit lost

Exercise 17:

1. Represent the following as normalised floating point numbers using a 16-bit register where the first 10 bits are used to code the mantissa in twos complement and the remaining bits represent the exponent in twos complement code.

i. 10.1875 and –10.1875 (Ans. 0.101000110 000100, 1.010111010 000100) ii. 7.125 and –7.125 (Ans. 0.111001000 000011, 1.000111000 000011)

iii. –30 (Ans. 1.000100000 000101) iv. –123 (Ans. 1.000010100 000111)

2. Evaluate the following, given that the first 5-bits are used to store the

exponent and the rest store the mantissa both in two’s complement form. i. 01000 01111000110 (Ans. 241.5)

ii. 00010 10111010101 (Ans. –2.17) iii. 10101 01000000000 (Ans. 0.00024)

not normalised

Data Representation

Cutajar&Cutajar2003 34

Also mention floating-point representation using sign and magnitude. Example 1: Represent +13.75 as a floating point number using a 12-bit register where the first 8 bits are used to code the mantissa in sign and magnitude and the remaining bits represent the exponent in twos complement code. Step 1: convert +13.75 to binary 13.75 = 1 1 0 1 . 1 1 Step 2: represent the resulting binary number in ‘standard’ form i.e. 1 1 0 1 . 1 1 = 0 . 1 1 0 1 1 1 * 2 100 (4) Step 3: write mantissa and exponent using available bits i.e. 0 1 1 0 1 1 1 0 0 1 0 0 For positive numbers steps are the same as when the mantissa stored numbers in twos complement format. Example 2: Using the same register format as in example 1, represent –13.75. Steps 1-2-3 remain the same as in example 1. Step 4: given + 13.75 = 0 1 1 0 1 1 1 0 0 1 0 0 ∴ - 13.75 = 1.1 1 0 1 1 1 0 0 1 0 0 Example 3: Using the above 12-bit register format, represent –17.375 in floating point.

- 17.37510 = -10001.0112 -10001.011 = -0.10001011 * 2101 (+5) Note: Mantissa: -ve Exponent: +ve -1.10001011 +0101 ⇒ 1.10001011 0101

+ve exponent remains unchanged

Sign bit changes to a 1 ( –ve )

Data Representation

Cutajar&Cutajar2003 35

Same exercise as 17 Question 1: Represent the following as normalised floating point numbers using a 16-bit register where the first 10 bits are used to code the mantissa in sign and magnitude and the remaining bits represent the exponent in twos complement code.

i. 10.1875 and –10.1875 (Ans. 0.101000110 000100, 1.101000110 000100) ii. 7.125 and –7.125 (Ans. 0.111001000 000011, 1.111001000 000011)

iii. –30 (Ans. 1.111100000 000101) iv. –123 (Ans. 1.111101100 000111)

Range of floating point number representation.

Assume a 12-bit register with an 8-bit mantissa and 4-bit exponent, both in twos complement code.

_ _ _ _ _ _ _ _ _ _ _ _

-1 . 1/2 1/4 1/8 1/16 1/32 1/64 1/128 -8 4 2 1

Range: (Remember normalisation!) Floating-point binary bit pattern for maximum magnitude positive number: In decimal: (1 – 1/128) * 27 = +127 Floating-point binary bit pattern for minimum magnitude positive number:

+∞ -∞ Minimum positive

Maximum negative

Maximum positive

0

Minimum negative

Binary point to extreme right for largest positive number

Largest positive fraction possible

0 1 1 1 1 1 1 1 0 1 1 1

Binary point to extreme left for smallest positive number

0 1 0 0 0 0 0 0 1 0 0 0

Data Representation

Cutajar&Cutajar2003 36

In decimal: (+0.5) * 2-8 = 0.001953125 Floating-point binary bit pattern for minimum magnitude negative number: - 0 1 0 0 0 0 0 1 -1 0 0 0 In decimal: - (1/2 + 1/128) * 2-8 = -0.001983642578125 Floating-point binary bit pattern for maximum magnitude negative number: In decimal: -(1) * 27 = -128 ∴ Range of 12-bit register in floating point format as described above is [ -128 , +127 ]

Binary point to extreme left for smallest negative number

Smallest negative fraction possible

1 0 1 1 1 1 1 1 1 0 0 0

Binary point to extreme right for largest magnitude negative number

Largest negative fraction possible

1 0 0 0 0 0 0 0 0 1 1 1

-∞

-0.001983643 +∞

0 +0.001953125 -128

+127

Data Representation

Cutajar&Cutajar2003 37

Note that a small range around zero [-0.001983643, 0.001953125] is not represented. Exercise (in class): Show that the range of numbers in floating point, for a 16-bit register with a 10-bit twos complement mantissa and a 6-bit twos complement exponent is:

[-(2.1475 * 109), +(2.1433* 109)]. Compare range of integers in a 16-bit register: [-32 768, +32 767] (Ans. Max +ve = 0.111111111 011111, Min +ve = 0.100000000 1000000 Min –ve = 1.000000000 011111, Max –ve = 1.011111111 100000 Remarks: i) Note how the range of floating point representation explodes with the addition

of just a few bits

Range of 12-bit (8M/4E) register: [ -128 , +127 ] Range of 16-bit (10M/6E) register: [-(2.1475 * 109), +(2.1433* 109)]

ii) Given an n-bit register for representing floating point numbers: Suppose that x bits are used to represent the mantissa and y bits are used to represent the exponent.

Hence n = x + y If y is made large (hence x made small), you would be increasing the range of numbers that can be represented in floating point using the n-bit register but sacrificing on the precision. Likewise, if x is made large (hence y made small), you would be increasing the precision of the representation (since more bits are available to represent the mantissa) but losing on the range of numbers that may be represented by the register. iii) In floating point format, we have no representation of zero. In practice, the

smallest positive number is used as a representation of zero. Any other result

… …

x-bit mantissa

y-bit exponent

Data Representation

Cutajar&Cutajar2003 38

falling between the smallest magnitude positive number and the smallest magnitude negative number is represented by zero.

Addition using twos complement floating point numbers.

Assume a 12-bit register using an 8-bit mantissa and a 4-bit exponent. Method: i) If exponents of the two numbers are not the same, make the smaller

exponent equal to the bigger exponent and shift binary point right accordingly.

ii) Add the mantissas of the two numbers iii) If carry 1 to MSB – move binary point 1 left and increment exponent by 1. Example 1: Consider 0 . 1 0 1 0 0 0 0 0 0 1 0 + 0 . 1 0 0 0 0 0 0 0 1 0 0

1. 0 . 0 0 1 0 1 0 0 0 0 + 0 1 0 0 0 . 1 0 0 0 0 0 0 0 1 0 0

2. 0 . 1 0 1 0 1 0 0 0 0 0 1 0 0

3. no carry! Example 2: Consider 0 . 1 1 1 0 0 0 0 0 0 1 0 + 0 . 1 0 1 1 1 0 0 0 0 1 1

1. 0 . 0 1 1 1 0 0 0 0 + 0 0 1 1 0 . 1 0 1 1 1 0 0 0 0 1 1

2. 1 . 0 0 1 0 1 0 0 0 0 0 1 1

carry 3. 0 . 1 0 0 1 0 1 0 0 1 0 0

+1

Data Representation

Cutajar&Cutajar2003 39

Subtraction of floating point numbers using twos complement codes.

Use the same algorithm as for addition. Remember we perform subtraction using twos complement of minuend (number being subtracted). Example 1: Consider 0 . 1 1 1 0 0 0 0 0 0 1 1 - 0 . 1 0 1 1 1 0 0 0 0 1 0

0 . 1 1 1 0 0 0 0 0 - 0 0 1 1 0 . 0 1 0 1 1 1 0 0 0 1 1

⇒ 0 . 1 1 1 0 0 0 0 + 1 . 1 0 1 0 0 1 0 (2s complement)

___________ 1↵ 0 . 1 0 0 0 0 1 0 0 0 1 1 Example 2: Consider 0 . 1 0 1 1 1 0 0 0 0 1 0 - 0 . 1 1 1 0 0 0 0 0 0 1 1

0 . 0 1 0 1 1 1 0 0 - 0 0 1 1 0 . 1 1 1 0 0 0 0 0 0 1 1

⇒ 0 . 0 1 0 1 1 1 0 0 + 0 0 1 1 (2s compl,) 1 . 0 0 1 0 0 0 0 0 0 1 1 ___________ no carry↵ 1 . 0 1 1 1 1 1 0 0 0 0 1 1

Errors in numeric representations.

When representing numeric data, the number of bits available for the representation is limited. Hence errors may occur. 1. PRECISION – associated with the number of bits available to represent the

fractional part of a given number.

Data Representation

Cutajar&Cutajar2003 40

Consider:

1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 0 . 1 0 1 1 1 1 0 1

Using a 9-bit register: precision is 1/256 or 1/(2-8) Consider:

1/2 1/4 1/8 1/16 1/32 1/64 1/128 0 . 1 0 1 1 1 1 0

Using an 8-bit register: precision is 1/128 or 1/(2-7) The more the number of bits for mantissa, the greater the precision.

2. ACCURACY – is a measure of the closeness of an approximation to the true value. Consider the true (binary) number 0 . 1 0 1 1 1 1 0 1 1 1 1 Using a 10-bit register: 0 . 1 0 1 1 1 1 0 1 1 1 1 (error of 2-10 + 2-11) …………(i)

Using an 8-bit register:

0 . 1 0 1 1 1 1 0 1 1 1 1 (error of 2-9 +2-10 + 2-11) ……(ii)

Note that approximation (i) is more accurate than approximation (ii). Full accuracy for the true numbers requires 12 bits.

3. RANGE – is the set of all numbers that can be represented by a particular system.

Example 1: Assuming the following system; Fixed point representation in an 8-bit register using twos complement code to represent integer numbers: Range of this system is [-27 , 27 – 1] = [-128 , +127] Example 2: Assuming the following system: Floating-point representation using a 16-bit register with a 10-bit twos complement mantissa and a 6-bit twos complement exponent. For representing real numbers: Range of system is [-(2.1475 * 109), +(2.1433* 109)].

Data Representation

Cutajar&Cutajar2003 41

1

4. RESOLUTION - is the magnitude of the difference between the last two (least significant) adjacent digits or numbers. In the case of an 8-bit integer representation, the resolution is a constant 1 over the entire range (-128, +127). In the case of floating point representation, the resolution is not constant over the entire range and varies according to the value of the exponent being used. Example: Finding the resolution of a register which stores unsigned numbers in the format iiiiii.ff. Smallest number: 000000.00 = 0 Biggest number: 111111.11 = 63.75 Thus the range is 0…63.75 Although the range of the format is 0..63.75 it does not mean that we can represent ANY number in this range. Since the register is 8-bits wide, there are still only 255 distinct values which can be represented. In this case the resolution is 000000.01 = 0.25. In other words this format can represent numbers from 0..63.75 IN STEPS of 0.25. 0.25, 0.5, 0.75, 1.0, 1.25, … , 63.75 (256 numbers in all) Values in between these numbers cannot be represented accurately. The positioning of the point in fixed point representation is a compromise between RANGE and RESOLUTION.

5. TRUNCATION

Consider 0 . 1 0 1 1 1 1 0 1 1 1

to be represented in an 8-bit register.

0 . 1 0 1 1 1 1 0 1 1 1 ← truncated bits truncation error is : ( 2-8 + 2-9 +2-10 )

6. ROUNDING

Consider 0 . 1 0 1 1 1 1 0 1 1 1

to be represented in an 8-bit register.

0 . 1 0 1 1 1 1 0 1 1 1 ← bits that cannot be represented

Data Representation

Cutajar&Cutajar2003 42

When rounding a binary number, only the next consecutive digit in the binary bit sequence is considered.

rounding error is : ( 2-9 + 2-10 -2-7 ) 7. OVERFLOW occurs when the upper bound of the range of the register in

question is exceeded.

UNDERFLOW is a term usually used in conjunction with floating point representations. When a value falls out of range in the unrepresented range: ] smallest magnitude negative number, smallest magnitude positive number[ an underflow is said to occur. Programs respond to underflow conditions in different ways. Some report an error while others approximate as best they can and continue processing (discussed above). Consider a 4-bit register: Range of 4-bit register is [-8 , +7 ] Hence any number x, x > 7 or x < -8, cannot be represented in this 4-bit register Suppose adding 0 1 0 1 (+5) and 0 1 0 0 (+6) will result in overflow.

8. Errors introduced from decimal to binary conversions

Decimal numbers are usually converted into binary and, for the majority of numbers an error (decrepency from true value) is already introduced at this stage before the numbers are used in calculations. Consider the following examples: Representing these numbers in a 4-bit register: 0.7510 = 0.112 ⇒ 0 1 1 0 represented with no error but, 0.0937510 = 0.000112 ⇒ 0 0 0 1 represented with error and furthermore, most numbers that are not multiples of binary fractions can never be represented exactly irrespective of the number of bits available. 0.92510 = 0.1110 1100 1100 1100 1100 …2 can never be represented exactly!

Data Representation

Cutajar&Cutajar2003 43

Errors like these happen when most decimal fractions are input to a computer. However, it is usual to have a large number of binary digits, and this means that the decimal number is represented to a sensible number of places. Describing computational errors: (i) Actual error = exact value – computed value where exact value is the value obtained in theory without any error

computer value is the value obtained from the computer after it has been processed

(ii) Relative error = actual error / exact value (iii) Absolute error = actual error

(Magnitude of actual error) (iv) Absolute relative error = relative error (Magnitude of relative error) (v) Percentage error = relative error * 100 per cent Example: Consider 0.33 as an approximation of 1/3 Exact value = 1/3 Computed value = 0.33 Actual error = 1/3 – 33/100 = (100 – 99)/300 = 1/300 Relative error = (1/300) / (1/3) = 1/100 Percentage error = (1/100) * 100 = 1%

Data Representation

Cutajar&Cutajar2003 44

Character Codes. As already stated, all data used by computers is in code form, namely binary code i.e.

Data may be either numeric or character in type. We commonly refer to this data as alphanumeric data Numeric codes have already been discussed. We now discuss characters and how these are represented in digital format. Characters may be • Printable characters

- Letters A..Z, a..z - Digits 0..9 - Special characters ; , . ? etc -

• Non printable characters Characters that cannot be displayed (printed) on screen or printed and are normally used for control purposes Examples: <delete>, <carriage return>, etc

In order to code characters, a unique binary bit pattern is associated with each (symbol) character. All binary digits are stored in memory as positively and negatively charged magnetic spots. Because of the need to communicate information from one computer system to another, a unique set of codes for representing the different characters is required. Various standard coding systems have been developed (to answer needs and requirements of the time) to represent data in the computer and to conveniently transmit it between systems.

transfers

computer stores

manipulates

data in code form.

Data Representation

Cutajar&Cutajar2003 45

The set of characters that can be coded is called the CHARACTER SET of the computer or programming language.

BCD characters (historical value only)

Widely used on second generation computing systems. Six bits are used to represent the different characters.

Zone bit: 00 numeric bits interpreted as decimal digits 0..9 01 11 numeric bits interpreted is a letter or special symbol 10 With this coding scheme, only 64 different characters are possible. ( n bits ⇒ 26 = 64 possibilities) As computing systems became larger and more powerful, there was a need for more characters including the lower case letters and special symbols. This led to the development of EBCDIC and ASCII.

EBCDIC character code.(Pronounced eb-see-dic) EBCDIC is short for Extended Binary Coded Decimal Interchange Code. It is an 8-bit character code (hence 256 character codes possible) developed for the exchange of documents between IBM (mainframe) computers. EBCDIC is not much used outside the IBM and similar mainframe environments. Note that as such, EBCDIC is a set of character sets i.e. there are a number of versions of EBCDIC and when exchanging files it must be ensured that computer systems involved are set up for the relevant code set. ASCII character code.

ASCII is short for American Standard Code for Information interchange. This code is similar to EBCDIC, but usually on 7 bits, instead of 8. Since computers use

Numeric bits

Zone bits

Data Representation

Cutajar&Cutajar2003 46

memory locations that are a multiple of 8 (bits), the extra 8th bit is used for error checking, called parity checking. There are two such schemes:

• Even Parity – the parity bit is adjusted such that the number of 1’s composing the character in binary is always even.

Example: ASCII ‘A’ = 100 0001 gets 0100 0001

• Odd Parity - the parity bit is adjusted such that the number of 1’s composing the character in binary is always odd.

Example: ASCII ‘A’ = 100 0001 gets 1100 0001

Sometimes the 8th bit is used to increase (effectively doubles) the characters available, such as graphics symbols, language dependent characters, non-English characters, mathematical sysmbols … etc. This is called the Extended ASCII Code. ASCII has been widely used as the basic code set on personal and workstation computers. Microcomputers usually use ASCII code and so do most microcomputer applications (store files using this code). General Remarks to character sets: (i) Notice that the only difference between the upper and lower case letters is a

single bit (5th bit in ASCII), so a translation from upper to lower case or vice versa can be accomplished by simply flipping this one bit.

(ii) If the eight bits used to represent the letters are interpreted as binary

numbers, then the letters follow a sequence with "A" having the lowest value and "Z" having the largest value. This property is critical when sorting data alphabetically. The computer can interpret the bit patterns as binary integers, perform the sort, and then reinterpret the bit patterns as characters. The order used in arranging characters is called the collating sequence. Note that this order may differ for different character codes.

Unicode

Unicode is the universal character code standard. Need for such a code arose with the development of the Internet and the widespread use of networking the world over. Unicode provides a consistent way of encoding multilingual plain text. It provides the capacity to encode all the characters used for the written languages of the world. Each character is assigned a unique numeric value (code point), adopting the simplicity and consistency of previously developed codes such as the

Data Representation

Cutajar&Cutajar2003 47

ASCII, and name (codespace name). The majority of common use characters fit into the first 64K (216) code points, an area of the codespace that is called the basic multilingual plane (BMP). The Unicode standard also reserves code points for private use. Unicode has three encoding forms: (UTF: Unicode Transformation Format) UTF-8: This encoding form is a way of transforming all Unicode characters into a variable length encoding of bytes. Unicode characters corresponding to the familiar ASCII characters have the same byte values. UTF-16: The most heavily used characters fit into the first 16-bit code, while other characters are accessible via pairs of 16-bit code units. This encoding form is used in environments that need to balance efficient access to characters with economical use of storage. UTF-32: Each Unicode character is encoded using a 32-bit code unit. This encoding form is used where memory space is of no concern, and fixed width, single code unit access to characters is required. Note that all three encoding forms need at most 4 bytes (32 bits) of data for each character. For full details on Unicode check out Internet site http://unicode.org/ Example1: Given that 6 bits are to be used to code a new character set, what is the maximum possible number of characters of this new character set? 6 bits ⇒ 26 different binary bit patterns ⇒ 64 characters may be coded. Example 2: A new character set consists of 30 characters. How many bits (minimum number of bits) are required to code this character set? With 4 bits…24 = 16 possibilities 5 bits…25 = 32 possibilities…. 30 characters ⇒ 30 different binary bit patterns required ⇒ 5 bits are required to code this character set. Alternatively, using a systematic method: 2n = x possibilities.

Data Representation

Cutajar&Cutajar2003 48

Given n = number of bits x = number of different characters

2n = 30 ⇒ log 2n = log 30 ⇒ n log 2 = log 30 ⇒ n = (log 30) / (log 2) ⇒ n = 4.906890595609 but n must be integer ∴ n = 5

Data Representation

Cutajar&Cutajar2003 49

Appendix A

Physical representation of 0 and 1 in digital systems.

Inside a system the values are represented by means of voltages. In analogue systems, values expressed in decimal, have to be very precise since a small variation in the voltage can represent another value. Thus components for such systems are very expensive. To overcome this problem the digital system was adopted where the values can be either 1 or 0. In a digital system, we consider two threshold levels: an upper threshold voltage level and a lower threshold voltage. All values above the upper level are considered to be 1 and all values below the lower level are considered to be 0. The range of values between these two threshold values is considered to be in the forbidden range. In a digital system, there is a certain tolerance to which a voltage can rise or fall without affecting the particular logic level.

Any voltage within this range is considered to be a 0 (Logic 0 tolerance)

Any voltage within this range is considered to be a 1 (Logic 1 tolerance)

Forbidden Range

Vmin 1

Vmax 1

Vmin 0

Vmax 0

Volts

Time

Data Representation

Cutajar&Cutajar2003 50

“ ? ~ | 1111

= > ; + 1110

‘ - ) ( 1101

@ % * < 1100

# , $ . 1011

: ! ¢ SM 1010

z r i 1001

q h 1000

x p g EOT PRE IL DEL 0111

w o f UC EOB BS LC 0110

v n e RS LF NL HT 0101

u m d PN BYP RES PF 0100

t l c 0011

s k b 0010

j a / 0001

- & SP NULL 0000

11001011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 0000

bits 7654 Bits 3210

EBCDIC

Data Representation

Cutajar&Cutajar2003 51