32
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

Embed Size (px)

Citation preview

Page 1: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

1

Representation of Data within the Computer

Oct., 1999(Revised 2001 Oct)

Page 2: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

2

The representation of numeric data Denary(十進制 ) system

265=2 x 102+6 x 101 + 5 x 1 Binary(二進制 ) system

1100=1 x 23 + 1 x 22 + 0 x 21 + 0 x 20

which is equal to 12 in the denary system.

1100 = 1210

Page 3: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

3

Octal and Hexadecimal system

Octal system2658 = 2 x 82 + 6 x 81 + 5 x 80 = 18110

Hexadecimal system2B516 = 2 x 162 + 11 x 161 + 5 x 160 = 69310

Page 4: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

4

Numeric system Conversion

A. From other systems to denary numbers 1100112 = 1 x 25 + 1 x 24 + 0 x 23 + 0 x 22 +

1 x 21 + 1 x 20 = 5110

3528 = 3 x 82 + 5 x 81 + 2 x 80 = 23410

3E16 = 3 x 161 + 14x 160= 6210

Page 5: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

5

Numeric system Conversion

B. From the denary system to other systems

25410 = 3 x 82 + 7 x 81 + 6 x 80 = 3768

In general, to convert a denary number to other number system with base b, we may use the following algorithm:

1 Divide the denary number by b repeatedly until the quotient is 0.

2 Copy the remainders obtained in reverse order to get the final answer.

Page 6: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

6

Exercise

Convert 200010 into a hexadecimal number. Answer is 7D0 Convert 5110 into a binary number. Answer is 1010001

Page 7: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

7

From binary to octal system

1 Group the digits of the binary number by three starting form the right.

2 Replace each group of three digits by an equivalent octal digit.

Convert 1100112 into octal number.

Page 8: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

8

From binary to and hexadecimal system

1 Group the digits of the binary number by four starting form the right.

2 Replace each group of four digits by an equivalent octal digit.

Convert 101101012 into a hexadecimal number.

Page 9: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

9

From octal and hexadecimal systems to binary

1 First convert each digit of the octal and hexadecimal number to a group of three and four binary binary digits respectively.

2 Then form the binary number with these groups of binary digits according to the sequence of digits of the original number.

Page 10: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

10

Example

Convert 3678 into a binary number. Answer is 11110111 Convert 5C16 into a binary number. Answer is 101110000010110

Page 11: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

11

Fixed point representation

There are three widely used systems for representing both positive and negative numbers.

! Sign-and-magnitude! 1’s-complement! 2’s -complement

Page 12: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

12

Sign-and-magnitude

1 the leftmost bit (refer to sign bit) is 0 for positive numbers and 1 for negative numbers.

2 The remaining bits represent the binary equivalent of the magnitude (absolute value) of the number.

Page 13: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

13

Sign-and-magnitude

Convert +4210 and -1810 to 8-bit binary codes using the sign-and-magnitude representation.

Page 14: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

14

Example

In the sign-and-magnitude system, what is the number represented by the following 8-bit binary codes?

1 000000002 100000003 11010111

Page 15: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

15

Two’s complement representation

— To obtain the 2’s complement of binary code, the following algorithm may be useful.

1 Obtain the 1’s complement by complementing each bit. That is, change all 0s to 1s to 0s and 1s to 0s.

2 Add 1 to the result binary code.

Page 16: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

16

Example

Find the 2’s complement of the binary code 01001011.

Page 17: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

17

Two’s complement representation

— The following is an algorithm for representing a denary number in the 2’s complement system.

1 Convert the denary number to equivalent binary number.

2 If the denary number is positive, do nothing. Otherwise, obtain the 2’s complement of the binary code.

Page 18: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

18

Example

Represent the number +42 and -18 in 8-bit 2’s complement system.

Page 19: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

19

Two’s complement representationBy reversing the above algorithm, we can convert the 2’s

complement representation to denary number. The following is the reversed algorithm.

1 If the leftmost bit is 0, the number is positive. The binary code is the binary equivalent of the number. Convert it to a denary number.

2 If the leftmost bit is 1, the number is negative. Obtain its 2’s complement before converting it to a denary number. The required number is the negative value of the denary number obtained.

Page 20: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

20

Example

What are the denary numbers represented by the following representation in 2’s complement system?

1 01010010

2 10100111

Page 21: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

21

Two’s complement representationThe 2’s complement system is much more

common than other systems because of the following:

1 Subtraction can be done by addition. Therefore, no additional circuit is needed.

2 A sign bit and other bits need not be handled separately.

3 There is no distinct +0 and -0 representations as in sign-and-magnitude and 1’s complement systems.

4 One more number, -128, can be represented.

Page 22: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

22

Overflow

— Arithmetic operation may not obtain the correct answer in some cases. This happens when the result is out of range that the binary codes can represent.

— For example, when 8-bit binary code is used to represent a number in the 2’s complement system, the range of numbers that can be represented is from -128 to +127. If the result of an operation lies outside this range, overflow occurs.

Page 23: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

23

Floating-point representation

— The fixed point representation is not sufficient for scientific calculations, hence, there is a need to easily accommodate both very large integers and very small fractions.

— In this case, the position of the binary point is variable and the binary point is said to float.

Page 24: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

24

Normalization of binary numbers

0.0001112 = .1112 x 2-3 = .1112 x 10-11

-1011002 = - .10112 x 26 = - .10112 x 10110

Page 25: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

25

Representation

Suppose 16-bit binary codes is used to represent floating point number. Let us use the leftmost bit to indicate the sign of the number. The next 8 bits to represent the mantissa and the rightmost 7 bits to code the exponent in 2’s complement.

Page 26: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

26

Example

Represent -10.37510

-1010.0112 = -.10100112 x 24

Therefore, the floating representation of -10.375 is

1101001100000100

Page 27: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

27

Truncation error

In the truncation scheme, any significant bit that cannot be accommodated in the bits for mantissa is ignored. For example, 101100011 is represented by .10110001. 0.000000001 is ignored. This is called a truncation error.

Page 28: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

28

Rounding error

In the rounding scheme, if the bits that cannot be accommodated correspond to a value less than half of the place value of the last bit used to represent mantissa, they are ignored, Otherwise, the place value of the last bit used to represent mantissa is added to the mantissa.

For example, the former example 101100011 is represented by .10110010.

Page 29: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

29

Representation of alphanumeric data and Chinese characters

— All letters, digits and symbols in a computer are referred to as alphanumeric characters.

— Usually, 8-bit binary is used to code one character. It leads to 28 = 256 different characters that can be represented.

— The most commonly used coding table follows the standard American Standard Code for Information Interchange, usually referred by its short form ASCII.

Page 30: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

30

Representation of Chinese characters

— Chinese characters are not made up of alphabet.— They need more bits to represent Chinese

characters. Indeed, we use 2 bytes, that is 16 bits to represent one single Chinese character.

— The most commonly used codes for traditional Chinese characters is the Big5 code and that for simplified Chinese characters is GB codes.

Page 31: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

31

Parity checking奇偶檢驗

— Parity checking is a simple method of checking the correctness of received data.

— An ASCII code is 8 bits long, but only the rightmost 7 bits are used to represent a character leaving the most significant bit 0. The 8th bit can be used as a parity bit.

Page 32: 1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)

32

Parity checking奇偶檢驗

— To maintain even parity 偶數奇偶檢驗 , the parity bit is set to 1 if the code being sent has an odd number of 1s.

— On the other hand, to maintain odd parity奇數奇偶檢驗 , the parity bit is set to 1 if the code being sent has an even number of 1s.