Lecture 5: Data Representation
Download
Report
Transcript Lecture 5: Data Representation
Lecture # 5
Data Representation
Today
1.
2.
3.
4.
5.
6.
Questions: From notes/reading/life?
Prepare for Quiz # 1 (Multiple Choice)
Introduce: How do Computers store data?
Explain: Data Types: Binary numbers, Integers, Floating
Point, Booleans, Characters, Variables
Demo:
Do some binary conversions: on board
Practice: Do some binary examples: in head, on keyboard
Evaluate: We will share and evaluate your conversions
Re-practice: Turn the crank on binary as you go to sleep
Understand
Data Types,
Storage
Review
Programs
Today
1.
2.
3.
4.
5.
6.
Questions: From notes/reading/life?
Prepare for Quiz # 1 (Multiple Choice)
Introduce: How do Computers store data?
Explain: Data Types: Binary numbers, Integers, Floating
Point, Booleans, Characters, Variables
Demo:
Do some binary conversions: on board
Practice: Do some binary examples: in head, on keyboard
Evaluate: We will share and evaluate your conversions
Re-practice: Turn the crank on binary as you go to sleep
Understand
Data Types,
Storage
Review
Programs
Today
1.
2.
3.
4.
5.
6.
Questions: From notes/reading/life?
Prepare for Quiz # 1 (Multiple Choice)
Introduce: How do Computers store data?
Explain: Data Types: Binary numbers, Integers, Floating
Point, Booleans, Characters, Variables
Demo:
Do some binary conversions: on board
Practice: Do some binary examples: in head, on keyboard
Evaluate: We will share and evaluate your conversions
Re-practice: Turn the crank on binary as you go to sleep
Understand
Data Types,
Storage
Review
Programs
Data Representation
If we want to compare, sort and organize data,
we need to have a way to represent it in a
computer – electronically. Even the characters
you are now reading, as words, must be able to
be represented and encoded electronically and
numerically. The following slides will explain
how we represent and encode data in a computer,
and how we manipulate and reason about it in a
computer program.
How to Represent
Data in a Computer
Foundations of Computing
• Information Data Types
–
–
–
–
–
Binary numbers
Integers and Floating Point
Booleans (True, False)
Characters
Variables
• Programs
– Expressions
– Assignment
– Sequences
There are 10 kinds of people
in the world – those who
understand binary and those
who do not.
Information Data Types
Binary numbers
• Integers and Floating Point
• Booleans (True, False)
• Characters
• Variables
Decimal Numbers
• Each digit is a power of ten
12345 =
5 * 100 +
4 * 101 +
3 * 102 +
2 * 103 +
1 * 104
5*1
+
4*10 +
3*100 +
2*1000 +
1*10,000
Binary Numbers
• Each Digit is a power of two
10110 =
0 * 20 (1)+
1 * 21 (2)+
1 * 22 (4)+
0 * 23 (8)+
1 * 24 (16)
= 22
Binary Numbers
• Each Digit is a power of two
1011101 =
1 * 20 (1) +
0 * 21 (2) +
1 * 22 (4) +
1 * 23 (8) +
1 * 24 (16) +
0 * 25 (32) +
1 * 26 (64)
= 93
Why Binary?
• Any physical phenomenon that has two
states can be used to store a binary number
11010
= 26
Magnetism
Magnetic
Material
Read/Write Head
1
+-+-++
101011
= 43
Voltage
+5 0
0
+5 0 +5
100101
= 37
Holes in Paper
Light = 1
Dark = 0
Photo
Sensor
Light
101010
= 42
Binary Students
• Male = 0
• Female = 1
Why Binary?
• Any physical phenomenon that has two
states can be used to store a binary number
• Each binary digit is called a BIT
– 1010 - is a 4 bit number
– 01010100 - is an 8 bit number
• An 8 bit number is called a BYTE
Size of a Binary Number
• How many different numbers can you store in 2
bits?
• How many can you store in 8 bits?
• In general, 2(number of bits) numbers can be stored
• How many in 10 bits?
– 1024 = 1K
• How many in 20 bits?
– 1,048,576 = 1Meg
Information
• Binary numbers
Integers and Floating Point
• Booleans (True, False)
• Characters
• Variables
Integers
• 100
• 100245
• -45
Size of an Integer
• How many bits required for the number 17?
• 5 bits
• How many bits for the number 1023?
• 10 bits
Integer Expressions
• * means multiply
• / means divide
• 2*4+3 = 11
• 2*(4+3) = 14
• (7+9)/2 = 8
Floating point numbers
• Numbers with decimal points
• 1.23
• 5623.1232
• -0.00232
Floating point numbers
• Numbers with decimal points
• 1.23 = 0.123e1
• 5623.1232 = 0.56231232e4
• -0.00232=-0.232e-2
• Stored differently (sign + fraction + exponent)
Information
• Binary numbers
• Integers and Floating Point
Booleans (True, False)
• Characters
• Variables
Booleans
• 1 = true
• 0 = false
Boolean Expressions
• (7>3) is true
• ((2+4)<6) is false
– Note Arithmetic Expression in
Boolean Expression
> Greater than
< less than
<= less than or equal
>= greater than or equal
== equal
!= not equal
7<=6 is false
6!=5 is true
Information
• Binary numbers
• Integers and Floating Point
• Booleans (True, False)
Characters
• Variables
ASCII Code for Characters
• Every character is defined to have an 8 bit
(1 byte) number
A
B
C
D
E
F
G
H
I
J
K
...
Z
65
66
67
68
69
70
71
72
73
74
75
90
a
b
c
d
e
f
g
h
I
j
k
. . .
z
97
98
99
100
101
102
103
104
105
106
107
Store 103
122
What is special about the order of the numbers?
Type ‘g’
ASCII Code for Characters
A
B
C
D
E
F
G
H
I
J
K
...
Z
65
66
67
68
69
70
71
72
73
74
75
90
a
b
c
d
e
f
g
h
I
j
k
...
z
97
98
99
100
101
102
103
104
105
106
107
0
1
2
3
4
5
6
7
8
9
48
49
50
51
52
53
54
55
56
57
122
A numeric character is different from its ASCII number
ASCII Code for Characters
A
B
C
D
E
F
G
H
I
J
K
...
Z
65
66
67
68
69
70
71
72
73
74
75
90
a
b
c
d
e
f
g
h
i
j
k
...
z
97
98
99
100
101
102
103
104
105
106
107
0
1
2
3
4
5
6
7
8
9
48
49
50
51
52
53
54
55
56
57
.
+
@
$
%
&
space
*
(
)
46
45
43
64
36
37
38
32
42
40
41
122
Dad32@Abc
68 97 100 51 50
64
65 98 99
What about Chinese, Sanscrit,
Hebrew, Cyrillic, etc ??
A
B
C
D
E
F
G
H
I
J
K
...
Z
65
66
67
68
69
70
71
72
73
74
75
90
a
b
c
d
e
f
g
h
I
j
k
...
z
97
98
99
100
101
102
103
104
105
106
107
122
0
1
2
3
4
5
6
7
8
9
48
49
50
51
52
53
54
55
56
57
.
+
@
$
%
&
space
*
(
)
46
45
43
64
36
37
38
32
42
40
41
What about Chinese, Sanscrit,
Hebrew, Cyrillic, etc ??
• UNICODE rather than ASCII
• 2 Bytes per character
– Twice as much space
– 65,536 possible characters
(2^16)
Expressions with characters
A
B
C
D
E
F
G
H
I
J
K
...
Z
65
66
67
68
69
70
71
72
73
74
75
90
‘A’ + 2 = ‘C’
‘g’- ‘d’ = 3
‘F’ + 32 = ‘f’
a
b
c
d
e
f
g
h
I
j
k
...
z
97
98
99
100
101
102
103
104
105
106
107
0
1
2
3
4
5
6
7
8
9
48
49
50
51
52
53
54
55
56
57
.
+
@
$
%
&
space
*
(
)
46
45
43
64
36
37
38
32
42
40
41
122
‘A’ < ‘D’ is true
‘%’ >= ‘+’ is false
Information
• Binary numbers
• Integers and Floating Point
• Booleans (True, False)
• Characters
Variables
Variable
• A named place to store a value
– George = 32
– Size = 17
– Weight = 120
Today
1.
2.
3.
4.
5.
6.
Questions: From notes/reading/life?
Prepare for Quiz # 1 (Multiple Choice)
Introduce: How do Computers store data?
Explain: Data Types: Binary numbers, Integers, Floating
Point, Booleans, Characters, Variables
Demo:
Do some binary conversions: on board
Practice: Do some binary examples: in head, on keyboard
Evaluate: We will share and evaluate your conversions
Re-practice: Turn the crank on binary as you go to sleep
Understand
Data Types,
Storage
Review
Programs
Binary Exercise
• Binary devices include:
• The binary number 101101 = what in
decimal?
• How many bits are required to store the
decimal number 256?
Review
• Binary devices include:
paper and magnetic tape, disks, transistors
• The binary number 101101 = what in
decimal?
45
• How many bits are required to store the
decimal number 256?
9 bits
Today
1.
2.
3.
4.
5.
6.
Questions: From notes/reading/life?
Prepare for Quiz # 1 (Multiple Choice)
Introduce: How do Computers store data?
Explain: Data Types: Binary numbers, Integers, Floating
Point, Booleans, Characters, Variables
Demo:
Do some binary conversions: on board
Practice: Do some binary examples: in head, on keyboard
Evaluate: We will share and evaluate your conversions
Re-practice: Turn the crank on binary as you go to sleep
Understand
Data Types,
Storage
Review
Programs
Programs
• Computer Programs make use of a these
data types to perform a given task
• Standard arithmetic operations can be
applied to manipulate the input data types
and output a result