Вы находитесь на странице: 1из 43

Data Representation and Numbering

Systems
Dr. Costas Kyriacou and Dr. Konstantinos Tatas

ACOE161 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…d1d0)10 = (dnX10n )+ (dn-1X10n-1) + …+ (d1X101) +( d0X100)


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

ACOE161 ACOE161 - Digital Logic for Computers 2


- Frederick University
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…d1d0)2 = (dnX2n )+ (dn-1X2n-1) + …+ (d1X21) +( d0X20)


Where d = {0,1}

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

ACOE161 3
The Binary Numbering System (Cont.)
• A binary digit is called the BIT (BInary digiT). Powers of 2:
0
• A group of eight bits is called the BYTE. 2 =1
• The leftmost bit of a number is called the Most Significant Bit (MSB). 1
2 =2
• The rightmost bit of a number is called the Least Significant Bit (LSB). 2
2 =4
N
• A binary system with N bits can represent the numbers from 0 to 2 -1. 3
N 2 =8
• In a binary system with N digits there are 2 different combinations. 4
2 = 16
• A binary number is multiplied by two, if we append a zero at the LSB. 5
2 = 32
• Prefixes in the binary system: 6
10
2 = 64
2 = 1,024 = 1K (Kilo) 7
2 = 128
20 8
2 = 1,024 X 1,024 = 1,048,576 = 1M (Mega) 2 = 256
9
30 2 = 512
2 = 1G (Giga)
10
40 2 =1024=1K
2 = 1T (Tera) 16
2 = 65536

ACOE161 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…d1d0)R = (dnXRn )+ (dn-1XRn-1) + …+ (d1XR1) +( d0XR0)


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

ACOE161 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…d1d0)R = (dnXRn )+ (dn-1XRn-1) + …+ (d1XR1) +( d0XR0)

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.

ACOE161 6
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 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

ACOE161 8
Conversion from decimal to binary
• Divide the number by 2 (24)10 = (?)2
obtaining quotient and
q r
remainder
24/2 12 0
• Divide the new quotient by 2 12/2 6 0
obtaining quotient and 6/2 3 0
remainder 3/2 1 1
• Repeat until quotient is 0 1/2 0 1
• The binary number digits are
the remainder digits in (24)10 = (11000)2
reverse order

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 Binary Base 3 Base 9 Base 14 Base 18


67
100111
2102
135
6C
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.

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

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.

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.
(110)2 = 6

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

ACOE161 14
Decimal, Binary, Octal and Hexadecimal Conversion Table
Decimal Binary Base 4 Octal Hex
0 0000 0 0 0
1 0001 1 1 1
2 0010 2 2 2
3 0011 3 3 3
4 0100 10 4 4
5 0101 11 5 5
6 0110 12 6 6
7 0111 13 7 7
8 1000 20 10 8
9 1001 21 11 9
10 1010 22 12 A
11 1011 23 13 B
12 1100 30 14 C
13 1101 31 15 D
14 1110 32 16 E
15 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 (d n1d n2 ...d 0 .d 1d 2 ...d k ) r
n 1
• Then N d
i  k
i r i

• Example: (72.51)10  7 101  2 100  5 10 1  110 2


ACOE161 18
Examples

(1001.0101) 2  (?)10
(0.213) 4  (?)10  (?) 2
(2B.1A) 16  (?)2  (?)10

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)10  (?) 2

• 0.5625  2 = 1.1250 Most significant


• 0.1250  2 = 0.2500
• 0.2500  2 = 0.5000
• 0.5000  2 = 1.0000 Least significant

(0.5625)10  (0.1001) 2
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:

(0.5A4C)16  (0.0101101001001100) 2

(0.01001) 2  (0.01001000) 2  (0.48)16

(3B.25)16  (00111011.00100101)2

(110.011) 2  (0110.0110) 2  (6.6)16


ACOE161 22
Homework
Decimal 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.
4 =(0100)2
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

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:
3 + 6 = 9  9/10 = 0 + 9/10  write 9 and carry 0
7 4 3
8 + 4 = 12  12/10 = 1 + 2/10  write 2 and carry 1
1 8 6+
9 2 9
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.

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:
(96)  9 - 6 = 3  write 3 and borrow 0 from next column
7 4 9
(4<8)  borrow 1  10+4-8=6  write 6 and borrow 1 from next column
1 8 6-
5 6 3
(7-11)  7 - 1 - 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.

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:

Binary Base 7 Base 9 Base 14 Base 18 Base 6

10101100 2564 5721 3A49 3A49 2435


11011101 3462 5646 596C 596C 2342
+ + + + +
1234
3443
+
10101100 2564 5721 3A49 3A49
11011101 3462 5646 596C 596C
- - - - -

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.

+ve => Sign bit = 0 -ve => Sign bit = 1

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

Magnitude Magnitude

Sign bit = 0 => +ve Sign bit = 1 => -ve

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

Magnitude Magnitude

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

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.

Sign bit Sign bit

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

Magnitude Magnitude

Sign bit Sign bit

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

Magnitude Magnitude

• The problem with the One’s Complement is that there are two values for zero:
(0000000 = +0, and 11111111 = -0).

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.

Sign bit Sign bit

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

Magnitude Magnitude +1

Sign bit Sign bit

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

Magnitude Magnitude +1

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

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 SM:8 1's C:8 2's C:8 Ex-127 Ex-128 Hex 15's C 16's C
+57
-57
-74
+128
-128
10000000
10000000
11001100
00110000
58
C5

ACOE161 38
Homework
• Fill up the table shown below:

Dec. Binary SM:8 1's C:8 2's C:8 Ex-127 Ex-128 Hex 15's C 16's C

10010011

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 Space 30 0 40 @ 50 P 60 ` 70 p
21 ! 31 1 41 A 51 Q 61 a 71 q
22 ” 32 2 42 B 52 R 62 b 72 r
23 # 33 3 43 C 53 S 63 c 73 s
24 $ 34 4 44 D 54 T 64 d 74 t
25 % 35 5 45 E 55 U 65 e 75 u
26 & 36 6 46 F 56 V 66 f 76 v
27 ’ 37 7 47 G 57 W 67 g 77 w
28 ( 38 8 48 H 58 X 68 h 78 x
29 ) 39 9 49 I 59 Y 69 i 79 y
2A * 3A : 4A J 5A Z 6A j 7A z
2B + 3B ; 4B K 5B [ 6B k 7B {
2C , 3C < 4C L 5C \ 6C l 7C |
2D - 3D = 4D M 5D ] 6D m 7D }
2E . 3E > 4E N 5E ^ 6E n 7E ~
2F / 3F ? 4F O 5F _ 6F o 7F Del
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.

ACOE161 43

Вам также может понравиться