Number Representation Lecture 20 4.3.2001.

Download Report

Transcript Number Representation Lecture 20 4.3.2001.

Number Representation
Lecture 20
4.3.2001.
Topics


How are numeric data items actually
stored in computer memory?
How much space (memory locations) is
allocated for each type of data?


int, float, char, etc.
How are characters and strings stored
in memory?
Decimal number
system



Ten digits :
0,1,2,3,4,5,6,7,8,9
Every digit position
has a weight which
is a power of 10.
Base or radix is 10.
Binary number
system



Two digits : 0,1
Every digit position
has a weight which
is a power of 2.
Base or radix is 2.
Decimal Number

136.25 : What does this number
actually mean ?
102 * 1 = 100.0
101 * 3 = 30.0
100 * 6 = 6.0
10-1 * 2 = 0.2
10-2 * 5 = 0.05
Binary Number

1101.01: What does this number mean?
23 * 1 = 1000.0 (8 in decimal)
22 * 1 = 100.0 (4 in decimal)
21 * 3 =
0.0 (0 in decimal)
20 * 6 =
1.0 (1 in decimal)
2-1 * 2 =
0.0 (0.0 in decimal)
2-2 * 5 =
0.01 (0.25 in decimal)
First integers and their binary
equivalent
decimal
0
1
2
3
4
5
6
7
8
9
binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
(0*23 + 0*22 + 0*21 + 0*20)
(0*23 + 0*22 + 0*21 + 1*20)
(0*23 + 0*22 + 1*21 + 0*20)
(0*23 + 0*22 + 1*21 + 1*20)
(0*23 + 1*22 + 0*21 + 0*20)
(0*23 + 1*22 + 0*21 + 1*20)
(0*23 + 1*22 + 1*21 + 0*20)
(0*23 + 1*22 + 1*21 + 1*20)
(1*23 + 0*22 + 0*21 + 0*20)
(1*23 + 0*22 + 0*21 + 1*20)
Adding Binary Numbers

Basic Rules:




0+0=0
0+1=1
1+0=1
1+1=0 (carry 1)

Example:
01101001
00110100
------------10011101
Weighted number systems

N=
j=0
M
bj Bj
N: the number
 M : Number of digits
 b: The digit
 B : System’s radix

Examples
1. 101011  1x25 + 0x24 + 1x23 + 0x22 + 1x21 + 1x20
= 43
(101011)2 = (43)10
2. .0101
 0x2-1 + 1x2-2 + 0x2-3 + 1x2-4
= .3125
(.0101)2 = (.3125)10
3. 101.11  1x22 + 0x21 + 1x20 + 1x2-1 + 1x2-2
5.75
(101.11)2 = (5.75)10
Decimal-to-Binary Conversion


Consider the integer and fractional parts
separately.
For the integer part,



Repeatedly divide the given number by 2, and go on
accumulating the remainders, until the number
becomes zero.
Arrange the remainders in reverse order.
For the fractional part,

Repeatedly multiply the given fraction by 2.



Accumulate the integer part (0 or 1).
If the integer part is 1, chop it off.
Arrange the integer parts in the order they are
obtained.
Example 1 :: 239
2
2
2
2
2
2
2
2
2
239
119
59
29
14
7
3
1
0
--- 1
--- 1
--- 1
--- 1
--- 0
--- 1
--- 1
--- 1
(239)10 = (11101111)2
Example 2 :: 64
2
2
2
2
2
2
2
2
64
32
16
8
4
2
1
0
--- 0
--- 0
--- 0
--- 0
--- 0
--- 0
--- 1
(64)10 = (1000000)2
Example 3: .634
.634
.268
.536
.072
.144
:
:
x
x
x
x
x
2
2
2
2
2
=
=
=
=
=
1.268
0.536
1.072
0.144
0.288
(.634)10 = (.10100……)2
Example 4: 37.0625
(37)10 = (100101)2
(.0625)10 = (.0001)2
(37.0625)10 = (100101.0001)2
Hexadecimal Numbers

Base=16
Decimal
0
1
2
3
4
5
6
7
8
9
Binary Hex
00000 0
00001 1
00010 2
00011 3
00100 4
00101 5
00110 6
00111 7
01000 8
01001 9
Decimal Binary
10
01010
11
01011
12
01100
13
01101
14
01110
15
01111
16
00110
17
00111
18
01000
19
01001
Hex
10
11
12
13
14
15
16
17
18
19
Integers Representation