42
ACOE161 1 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas

Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas

  • Upload
    vanya

  • View
    47

  • Download
    1

Embed Size (px)

DESCRIPTION

Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas. 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: Notes: - PowerPoint PPT Presentation

Citation preview

ACOE161 1

Data Representation and Numbering Systems

Dr. Costas Kyriacou and Dr. Konstantinos Tatas

ACOE161 ACOE161 - Digital Logic for Computers - Frederick University

2

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:

• Notes:

– A decimal system with N digits can represent the numbers from 0 to 10N-1.

– In a decimal system with N digits there are 10N 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).

(dndn-1…d1d0)10 = (dnX10n )+ (dn-1X10n-1) + …+ (d1X101) +( d0X100) Where d = {0,1,2,3,4,5,6,7,8,9} = {0..9}

ACOE161 3

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:

Examples:• (1011)2 = (1X23 )+(0X22)+(1X21) +(1X20) = 8+0+2+1= (11)10

• (10110)2 = (1X24 )+(0X23)+(1X22) +(1X21) +(0X20) = 16+0+4+2+0= (22)10

• (101100)2=(1X25 )+(0X24)+(1X23)+(1X22)+(0X21)+(0X20)=32+0+8+4+0+0=(44)10

(dndn-1…d1d0)2 = (dnX2n )+ (dn-1X2n-1) + …+ (d1X21) +( d0X20) Where d = {0,1}

ACOE161 4

The Binary Numbering System (Cont.)• A binary digit is called the BIT (BInary digiT).• 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).• A binary system with N bits can represent the numbers from 0 to 2N-1.• In a binary system with N digits there are 2N different combinations.• A binary number is multiplied by two, if we append a zero at the LSB.• Prefixes in the binary system:

210 = 1,024 = 1K (Kilo)

220 = 1,024 X 1,024 = 1,048,576 = 1M (Mega)

230 = 1G (Giga)

240 = 1T (Tera)

Powers of 2:20 = 1

21 = 2

22 = 4

23 = 8

24 = 16

25 = 32

26 = 64

27 = 128

28 = 256

29 = 512

210=1024=1K

216 = 65536

ACOE161 5

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:

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

(dndn-1…d1d0)R = (dnXRn )+ (dn-1XRn-1) + …+ (d1XR1) +( d0XR0) Where d = {0.. R-1}

ACOE161 6

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:

Examples:

• (214)5 = (2X52 )+(1X51) +(4X50) = (2X25)+(1X5)+(4X1)=50+5+4= (59)10

• (2021)3 = (2X33)+(0X32)+(2X31)+(1X30)=(2X27)+(2X3)+(1X1)=54+6+1= (61)10

• (13A)12 = (1X122)+(3X121)+(10X120)=144+36+10=(190)10

• (4C)18 = (4X181)+(12X180)=72+12=(84)10

• (135)4 = Invalid. Digit 5 is greater than 4 which is the base of the system.

(dndn-1…d1d0)R = (dnXRn )+ (dn-1XRn-1) + …+ (d1XR1) +( d0XR0)

ACOE161 7

Examples

Do the following conversions:

• (234)6 = (?)10

• (2012)3 = (?)10

• (A1C)14 = (?)10

• (5H)20 = (?)10

• (122)4 = (?)10

• (1220)4 = (?)10

• (12200)4 = (?)10

• (122003)4 = (?)10

ACOE161 8

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/41/4 = 0 + 1/4

Step 3: The number is formed by reading the remainders in reversed order.e.g. (70)10 = (1012)4

ACOE161 9

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

ACOE161 10

Homework: Do the necessary conversions to fill up the table below:

Decimal Base 3 Base 9 Base 14 Binary67

1001112102

1356C

Base 18

6C

ACOE161 11

Homework

Do the following conversions:

• (244)5 = (?)10

• (2132)4 = (?)10

• (A4F)18 = (?)10

• (6H)22 = (?)10

• (1202)3 = (?)10

• (12020)3 = (?)10

• (120200)3 = (?)10

• (1202003)4 = (?)10

ACOE161 12

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.

e.g. (10110100)2= (10 11 01 00)2= (2310)4

• The system with the base 4 is not widely used because many digits are still required to represent typical numbers.

(10)2 = 2 (01)2 = 1

(11)2 = 3 (00)2 = 0

ACOE161 13

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.

e.g. (10110100)2= (10 110 100)2= (264)8

• 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 16and 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= (B4)16

(010)2 = 2 (100)2 = 4

(110)2 = 6

(1011)2 =11=B (0100)2 = 4

ACOE161 14

Decimal, Binary, Octal and Hexadecimal Conversion TableDecimal Binary Base 4 Octal Hex

0 0000 0 0 01 0001 1 1 12 0010 2 2 23 0011 3 3 34 0100 10 4 45 0101 11 5 56 0110 12 6 67 0111 13 7 78 1000 20 10 89 1001 21 11 910 1010 22 12 A11 1011 23 13 B12 1100 30 14 C13 1101 31 15 D14 1110 32 16 E15 1111 33 17 F

ACOE161 15

Examples

Decimal Binary Base 4 Octal Hex

51

10101011

1320

154

5F

ACOE161 16

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

• (9C)16 = (?)2 = (?)4 = (?)8 = (?)10

ACOE161 17

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:

rknn dddddd ).......( 21021

1n

ki

ii rdN

210110 101105102107)51.72(

ACOE161 18

Examples

102 (?))0101.1001(

2104 (?)(?))213.0(

(2B.1A) 10216 (?)(?)

ACOE161 19

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.

ACOE161 20

Example

• 0.5625 2 = 1.1250 Most significant• 0.1250 2 = 0.2500• 0.2500 2 = 0.5000• 0.5000 2 = 1.0000 Least significant

210 (?))5625.0(

210 )1001.0()5625.0(

ACOE161 21

Converting fractional numbers from 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:

216 001001100)(0.0101101(0.5A4C)

1622 )48.0()01001000.0()01001.0(

216 00100101)(00111011.(3B.25)

1622 )6.6()0110.0110()011.110(

ACOE161 22

HomeworkDecimal Binary Base 4 Octal Hex

12.25

101011.01

132.1

13.5

4A.21

ACOE161 23

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.

e.g. (2 4 9)10= (0010 0100 1001)BCD

Examples:

• (173)10 = (?)2 = (?)BCD

• (1000 0111)BCD = (?)10

• (0100 0010)2 = (?)BCD

• (53)16 = (?)BCD

• (1011 0101)BCD = (?)10

2 =(0010)2 9 = (1001)2

4 =(0100)2

ACOE161 24

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:

7 4 31 8 6 +9 2 9

• 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.

1+ 7 + 1 = 9 9/10 = 0 + 9/10 write 9 and carry 0

3 + 6 = 9 9/10 = 0 + 9/10 write 9 and carry 0

8 + 4 = 12 12/10 = 1 + 2/10 write 2 and carry 1

ACOE161 25

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

• (1A79)16+ (C827)16 = (?)16

• (1A79)18+ (C827)18 = (?)18

ACOE161 26

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 91 8 6 -5 6 3

• 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.

(7-11) 7 - 1 - 1 = 5 write 5 and borrow 0 from next column

(96) 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

ACOE161 27

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

• (61A9)16- (C827)16 = (?)16

• (61A9)18- (C827)18 = (?)18

ACOE161 28

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.

ACOE161 29

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

• (61A9)16- (C827)16 = (?)16

• (61A9)18- (C827)18 = (?)18

ACOE161 30

Homework: Do the necessary operations to fill up the table below:

Base 7 Base 9 Base 14 Binary

256410101100 5721 3A49

Base 18 Base 6

11011101+

3462 5646 596C3A49596C

+ + + +

+256410101100 5721 3A49

11011101-

3462 5646 596C3A49596C

- - - -

2435234212343443

ACOE161 31

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).

(+38) 10 = +(100110) 2 = (00100110) SM:8

+ve => Sign bit = 0

Magnitude

(00101011) SM:8 = +(0101011) 2 = (+43) 10

Sign bit = 0 => +ve

Magnitude

(-38) 10 = -(100110) 2 = (10100110) SM:8

-ve => Sign bit = 1

Magnitude

(10101011) SM:8 = -(0101011) 2 = (-43) 10

Sign bit = 1 => -ve

Magnitude

ACOE161 32

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 11111111 = -0).

(+38) 10 = (00100110) SM:8 = (00100110) 1's C:8

Sign bit

Magnitude

(-38) 10 = (10100110) SM:8 = (11011001) 1's C:8

Sign bit

Magnitude

(00101011) 1's C:8 = (00101011) SM:8 = (+43) 10

Sign bit

Magnitude

(11010100) 1's C:8 = (10101011) SM:8 = (-43) 10

Sign bit

Magnitude

ACOE161 33

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 1to 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.

(+38) 10 = (00100110) SM:8 = (00100110) 2's C:8

Sign bit

Magnitude

(-38) 10 = (11011001) 1's C:8 = (11011010) 2's C:8

Sign bit

Magnitude +1

(00101011) 2's C:8 = (00101011) SM:8 = (+43) 10

Sign bit

Magnitude

(11010101) 2's C:8 = (10101011) SM:8 = (-43) 10

Sign bit

Magnitude +1

ACOE161 34

Negative Number Representation: Excess 2N-1

• The Excess 2N-1 of a number is obtained by adding 2N-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 2N-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

(01010101) ex-128 = 85 = 128 -43 = (-43)10

• Note that the 2N-1 can also be obtained by inverting the sign bit of the two’s complement of the number.

ACOE161 35

Negative Number Representation: Excess (2N-1-1)

• The Excess (2N-1 -1)of a number is obtained by adding (2N-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 (2N-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

(01010101) ex-128 = 85 = 128 -43 = (-43)10

• Note that the (2N-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 (2N-1 -1) representation is used in computers to represent the exponent of floating point numbers.

ACOE161 36

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

(01010101) ex-128 = 85 = 128 -43 = (-43)10

• The (2N-1 -1) representation is used in computers to represent the exponent of floating point numbers.

ACOE161 37

Examples

• Fill up the table shown below:Dec. Binary 1's C:8 2's C:8 Ex-127

+57

SM:8 Hex 15's C 16's C

-57

+128

-128

-74

Ex-128

10000000

10000000

58

C5

11001100

00110000

ACOE161 38

Homework

• Fill up the table shown below:

Dec. Binary 1's C:8 2's C:8 Ex-127

10010011

SM:8 Hex 15's C 16's CEx-128

10010011

10010011

10010011

10010011

9A

C5

ACOE161 39

Binary Codes

ACOE161 40

Non-numeric codes

• Not all information processed by computer systems are numbers

• Computers process also text, images, speech, video etc.

• This information must also be represented in the computer using binary signals

• Non-numerical information is represented using a code

ACOE161 41

ASCII Code - Printable Characters

20 Space21 !22 ”23 #24 $25 %26 &27 ’28 (29 )2A *2B +2C ,2D -2E .2F /

30 031 132 233 334 435 536 637 738 839 93A :3B ;3C <3D =3E >3F ?

40 @41 A42 B43 C44 D45 E46 F47 G48 H49 I4A J4B K4C L4D M4E N4F O

60 `61 a62 b63 c64 d65 e66 f67 g68 h69 i6A j6B k6C l6D m6E n6F o

70 p 71 q72 r73 s74 t75 u76 v77 w78 x79 y7A z7B {7C |7D }7E ~ 7F Del

50 P51 Q52 R53 S54 T55 U56 V57 W58 X59 Y5A Z5B [5C \5D ]5E ^5F _

ACOE161 42

Grayscale images

• A grayscale (“black-and-white”) image uses one byte (8-bits), to encode each pixel. Therefore there are 2^8=256 shades of gray.

• Typically a value of all zeroes represents black, and a value of all ones represents white.