CMPUT 101 - Data Representation
CMPUT 101 - Data Representation
Introduction to Computing
Data representation
How to use the audio and
the slide animations together
− Please turn up your device speakers and run
the slides in presentation mode
− Click on the play button of the audio icon as
soon as a new slide is up
− The play button of the audio icon will appear
when you wave the mouse over the audio
icon and will look like the following picture:
2
How to use the audio and
the slide animations together
− Slide animations run automatically in a timed sequence
regardless of whether the audio is played or not
3
Objectives
■ To learn:
■ The binary numbering system
■ How data are stored in memory
■ Conversion
■ Binary to decimal
■ Decimal to binary
■ Binary arithmetic
■ Python programming examples
4
Why use binary numbers?
Two states: 0 or 1
True = 1; False = 0
Internal memory storage in binary
5
Data representation
and storage
How does the computer memory store data?
Different types of data include:
Boolean values
Textual data - characters and strings
Integer and real numbers
Images, video, and audio files
7
Memory storage
■ What’s the largest number that can be
stored into memory?
■ The largest unsigned number that can be
stored into a k-bit machine is 2k – 1
■ The largest unsigned number that can be
stored into a 32-bit machine is 232 – 1
9
The decimal numbering system
■ The number 2569
= 2000 + 500 + 60 + 9
= 2 x 103 + 5 x 102 + 6 x 101 + 9 x 100
10
The binary numbering system
■ The binary sequence 1111
= 1 x 2 3 + 1 x 22 + 1 x 2 1 + 1 x 20
=8+4+2+1
= 15 (in decimal)
11
How to convert from
binary to decimal?
■ We refer to a binary digit as a ‘bit’
19 2 1 19 in decimal is
10011 in binary
9 1
4 0
2 0
1
0 1
15
Verifying the result
■ Let’s convert 10011 back to decimal:
= 1 x 2 4 + 0 x 23 + 0 x 22 + 1 x 2 1 + 1 x 20
= 16 + 0 + 0 + 2 + 1
= 19 (in decimal)
16
Binary arithmetic
■ In binary:
1 + 1 = 10 (2 in decimal)
1 + 1 + 1 = 11 (3 in decimal)
Example:
What’s 110 + 101?
17
Binary addition
1 carry
1 1 0
+
1 0 1
10 1 1
For practice, convert each number to decimal and verify the result.
18
Binary multiplication
1 1 0 =4+2=6
x
1 1 1 =4+2+1=7
1 1 1
1 1 0
+ 1100
1 1 0 0 0
10 1 01 0 = 32 + 8 + 2 = 42
19
Converting data to binary
✔Integers in decimal to binary
⮚ Real numbers (fractional)
⮚ Negative numbers
⮚ Booleans
⮚ Strings
⮚ Images
20
Numbers representation
■ Real numbers are normalized so the most
significant bit is after the decimal point
■ Example: .10111 x 23
21
Booleans representation
Booleans = {True, False}
True = 1
False = 0
22
Converting data to binary
✔Integers in decimal to binary
✔ Real numbers (fractional)
✔ Negative numbers
✔ Booleans
⮚ Strings
⮚ Images
23
Strings representation
■ Strings are encoded character by
character using two schemes
24
Each English character corresponds to an ASCII code
that can be converted to binary and stored in memory.
25
ASCII characters
■ Characters include:
■ Small and capital alphabetical letters
■ Punctuation and special characters
■ Digits 0 – 9 in the decimal system
■ Each character occupies one byte in memory
■ one byte = 8 bits
■ There are 256 codes in the ASCII table
■ That is, 28 = 256
26
Section of the Unicode Table
27
Unicode characters
■ Characters include:
■ The English and European alphabets
■ Symbols from languages around the world
■ Punctuation, emoji, and special characters
■ Digits 0 – 9 in English and in Arabic
■ Each character occupies two bytes in memory
■ Two bytes = 16 bits
■ There are 65,536 codes in the Unicode table
■ That is, 216 = 65,536
28
Images representation
■ Images are made of thousands of pixels
Examples:
Black = (0 0 0)
White = (255 255 255)
Gray = (127 127 127)
Red = (255 0 0)
Green = (0 255 0)
Blue = (0 0 255) … etc.
30
Converting data to binary
✔Integers in decimal to binary
✔ Real numbers (fractional)
✔ Negative numbers
✔ Booleans
✔ Strings
✔ Images
31
Programming practice
■ Write a program that reads a number in
decimal and converts it to binary.
quotient = int(input("Enter a number: "))
while quotient > 0: # quotient is the decimal number
remainder = quotient % 2
print(remainder)
quotient = quotient // 2
32
Summary
■ All data types that we work with on a
daily basis are stored in the computer
memory in a binary numbering format
■ Different data types use different
encoding schemes
■ Codes (in decimal or hexadecimal) are
then converted to binary sequences
33