PreludeProgramming6ed pp02 PDF
PreludeProgramming6ed pp02 PDF
Data Representation
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
106
10,000,000 1,000,000
ten-millions
millions
105
104
103
102
101
100
100,000
10,000
1,000
100
10
tens
ones
hundred-
thousands
Expanded Nota6on
The
ten
digits
that
are
used
in
the
decimal
system
are
0,
1,
2,
3,
4,
5,
6,
7,
8,
and
9.
Any
number
in
the
decimal
system
can
be
wriben
as
a
sum
of
each
digit
mulRplied
by
the
value
of
its
column.
This
is
called
expanded
nota9on.
The
number
6,825
in
the
decimal
system
actually
means:
5*100 = 5*1
+ 2*101 = 2*10
20
+ 8*102 = 8*100
800
+ 6*103 = 6*1,000
6,000
6,825
27
26
25
24
23
22
21
20
128
64
32
16
29 is
less
than
32
but
greater
than
16
so
put
a
1
in
the
16s
(24)
column.
29 16 = 13
13
is
less
than
16 but
greater
than
8
so
put
a
1
in
the
eights
(23) column
13 8 = 5
5 is
less
than
8
but
greater
than
4
so
put
a
1 in
the
fours
(22)
column
5 4 = 1
1 is
less
than
2
so
there
is
nothing
in
the
twos
(21)
column
Put
a 0 in
the
twos
column
You
have
1 lee
so
put
a 1 in
the
ones
(20)
column
Therefore,
2910 = 111012
Power of 2
25
24
23
22
21
20
Decimal value
32
16
Binary representa9on
44 is
less
than
64 but
greater
than
32 so
put
a 1 in
the 32s (25) column
44 32 = 12
12
is
less
than
16
but
greater
than
8
so
put
a
0 in
the
16s
(24)
column
and
a
1 in
the
eights
(23)
column
12 8 = 4
Put
a
1 in
the
fours
(22)
column
4 4 = 0
Put
0s
in
the
last
two
columns
Power
of
2
Decimal
value
Binary
representa9on
27
26
25
24
23
22
21
20
128
64
32
16
Rarely
need
to
deal
with
anything
larger
than
the
164s column.
The
hexadecimal
system
makes
it
easier
for
humans
to
read
binary
notaRon.
Decimal
equivalent
164
163
162
161
160
16*16*16*16
16*16*16
16*16
16
65,536
4,096
256
16
Hexadecimal Digits
oThe
decimal
system
uses
10
digits
(0
through
9)
in
each
column
(base
10)
oThe
binary
system
uses
two
digits
(0
and
1)
in
each
column
(base
2)
oThe
hexadecimal
system
uses
16
digits
in
each
column
(base
16)
oHow
can
you
represent
10
in
the
ones
column
in
base
16?
o
no
way
to
disRnguish
ten
(wriben
as
10)
from
sixteen
(also
wriben
as
10
a
one
in
the
16s
column
and
a
zero
in
the
ones
column)
o
Use
uppercase
lebers
to
represent
the
digits
10
through
15
o
hexadecimal
digits
are
0
through
9
and
A
through
F
Therefore, 2310
= 1716
Power
of
16
163
162
161
160
256
16
Hexadecimal representa9on
875 is
less
than
4,096
but
greater
than
256
so
there
is
nothing
in
the
4,096s
(163)
column
Divide
875
by
256
to
see
how
many
256s
there
are
875 256 = 3 with
a
remainder
of
107
Put
a
3
in
the
256s
column
107 16 = 6 with
a
remainder
of
11
Put
a
6
in
the
16s
column
11
in
decimal
notaRon
= B in
hexadecimal
notaRon
Put
a
B
in
the
ones
column
Power
of
16
Therefore,
87510 = 36B16
163
162
161
160
256
16
163
162
161
160
256
16
Decimal
Binary
Hexadecimal
Decimal
Binary
Hexadecimal
0000
1000
0001
1001
0010
10
1010
0011
11
1011
0100
12
1100
0101
13
1101
0110
14
1110
0111
15
1111
Overow
If
you
try
to
store
an
unsigned
integer
that
is
bigger
than
the
maximum
unsigned
value
that
can
be
handled
by
that
computer,
you
get
a
condiRon
called
overow.
Store
the
decimal
integer
2310 in
a
4-bit
memory
locaRon:
range
of
integers
available
in
4-bit
locaRon
is
010
through
1510
Therefore,
abempRng
to
store
2310
in
a
4-bit
locaRon
gives
an
overow.
Store
the
decimal
integer
65,53710 in
a
16-bit
memory
locaRon:
range
of
integers
available
in
16-bit
locaRon
is
010
through
6553510
Therefore,
abempRng
to
store
this
number
in
a
16-bit
locaRon
gives
an
overow.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Range
0...255
16
0...65,535
32
0...4,294,967,295
64
0...18,446,740,000,000,000,000 (approximate)
Sign-and-Magnitude Format
The
simple
method
to
convert
a
decimal
integer
to
binary
works
well
to
represent
posiRve
integers
and
zero.
We
need
a
way
to
represent
negaRve
integers.
The
sign-and-magnitude
format
is
one
way.
The
leemost
bit
is
reserved
to
represent
the
sign.
The
other
bits
represent
the
magnitude
(or
the
absolute
value)
of
the
integer.
The
range
of
ones
complement
integers
is
the
same
as
the
range
of
sign-and-magnitude
integers.
BUT
there
are
sRll
two
ways
to
represent
the
zero.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
1 + 0 = 1
1
+
1
=
10
1 0
Example 1
Example 2
1
+
1 1
1 0
1 0 1
1 1
1 0
1 1 1
1 0 0
Example
3
1
1 0 1
1
1 0 0
1 0 1
1
1 1 0
11010101
Therefore,
4310
in
twos
complement
in
an
8-bit
locaRon
is
11010101
100000000
Recall
that
Step
4
in
the
rules
for
converRng
to
twos
complement
states
that,
aeer
the
addiRon
of
1
to
the
ones
complement,
any
digits
to
the
lee
of
the
maximum
number
of
bits
(here,
8
bits)
should
be
discarded.
Discard
the
leemost
1
Therefore, 010 in twos complement in an 8-bit locaRon is 000000002 which is exactly the same as +010
The
rst
column,
20
is
the
ones
column;
the
second
column,
21
is
the
twos
column;
and
so
on.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
The rst six columns of the frac9onal part of a number in the binary system
0.1
1/21
0.01
=
1/22 =
0.001
1/23 =
0.0001
0.00001
1/24 = 2 1/25 = 25
0.000001
1/26= 26
21
22
23
0.5
0.25
0.125
0.0625
0.03125
0.015625
halves
fourths
eighths
sixteenths
thirty-seconds
sixty-fourths
How
many
bits
are
allowed
for
the
fracRonal
part
of
a
given
number?
Create
a
chart:
Binary
21
22
23
24
25
26
Decimal
0.5
0.25
0.125
0.0625
0.03125
0.015625
Conversion
21
22
23
24
25
26
Decimal
0.5
0.25
0.125
0.0625
0.03125
0.015625
Conversion
Binary
21
22
23
24
Decimal
0.5
0.25
0.125
0.0625
Conversion
Scien6c Nota6on
Computers
are
used
for
many
scienRc
applicaRons
which
oeen
use
very
large
or
very
small
numbers.
Example:
the
distance
from
Earth
to
our
nearest
star
is
24,698,100,000,000
miles.
We
would
need
a
49-digit
binary
number
to
represent
this
in
a
computer.
Example:
The
diameter
of
an
atom
would
require
at
least
a
30-digit
binary
number.
Humans
deal
with
these
almost-impossible-to-read
numbers
with
scien9c
nota9on.
Exponen6al Nota6on
In
programming,
instead
of
wriRng
10power,
we
use
the
leber
E
followed
by
the
given
power.
This
is
called
exponen9al
nota9on.
NoRce,
you
must
include
the
sign
of
the
exponent.
Examples:
680,000 = 6.8E+5
1,502,000,000 = 1.502E+9
8,938,000,000,000 = 8.938E+12
0.068 = 6.8E-2
0.00001502 = 1.502E-5
0.000000000008938 = 8.938E-12
Base 10 Normaliza6on
Normalized
form
is
similar
to
scienRc
notaRon.
Each
normalized
number
has
two
parts:
the
scaled
por9on
and
the
exponen9al
por9on.
In
scienRc
notaRon,
the
decimal
point
was
moved
so
the
rst
non-zero
digit
was
immediately
to
the
lee
of
it.
In
normalized
form,
the
decimal
is
moved
so
the
rst
non-zero
digit
is
immediately
to
the
right
of
it.
The
value
of
the
number
is
always
maintained.
To
normalize
a
decimal
number,
aeer
moving
the
decimal
point,
the
number
is
mulRplied
by
10
raised
to
whatever
power
is
necessary
to
return
the
number
to
its
original
value.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Scaled Por9on
Exponen9al Por9on
Normalized Form
371.2
0.3712
103
0.3712 103
40.0
0.4
102
0.4 102
0.000038754
0.38754
104
0.38754 104
52389.37
0.5238937
105
0.5238937 105
Excess_127
The
Excess_127
system
Is
used
to
store
the
exponenRal
value
of
a
normalized
binary
number.
To
represent
an
8-bit
number
in
the
Excess_127
system:
oAdd
127 to
the
number
oChange
the
result
to
binary
oAdd
zeros
to
the
lee
to
make
up
8
bits
Examples:
(a)
To
represent
+910
add
9 + 127 = 136
Base 2 Normaliza6on
The
process
is
similar
to
the
one
followed
to
normalize
a
decimal
number.
The
point
is
moved
but
it
is
moved
so
that
the
rst
non-zero
number
(a
1)
is
immediately
to
the
lee
of
the
point.
Then
mulRply
the
number
by
the
power
of
2
needed
to
express
the
original
value
of
the
number.
Not
necessary
to
worry
about
the
sign
of
the
number
since,
in
normalized
form,
the
sign
bit
takes
care
of
this.
Examples
If the number is posiRve, put a 0 in the leemost bit. If it is negaRve, put a 1 in the leemost bit.
If there is an integer and a fracRonal part, convert the whole number to binary
Move the point so it is directly to the right of the rst non-zero number.
4.
Count
the
number
of
places
you
moved
the
point.
This
is
your
exponent.
6.
The
man9ssa:
Use
the
number
from
Step
3
is
used
to
nd
the
manRssa.
When
storing
the
normalized
part
of
the
number,
the
1
to
the
le^
of
the
point
is
discarded.
Everything
to
the
right
of
the
point
is
now
called
the
manRssa.
This
number
takes
up
8
bits
while,
in
single-precision
oaRng
point,
the
manRssa
is
23
bits
long.
You
must
add
15
0s
at
the
end
to
complete
23
bits.
Therefore,
29 1.00001011
as
a
single-precision
oaRng
point
number
is
1 01110110 00001011000000000000000
Hexadecimal Representa6on
It
is
much
easier
to
read
a
hexadecimal
number
than
to
read
a
long
string
of
0s
and
1s.
Single
precision
oaRng
point
numbers
are
oeen
changed
to
hexadecimal.
Its
easy
to
convert
binary
to
hexadecimal:
Divide
the
binary
number
into
groups
of
four
digits
Convert
each
group
to
a
single
hexadecimal
number
Example
(from
previous
slide):
1 01110110 00001011000000000000000
is
BB05800016