0% found this document useful (0 votes)
34 views

CMPUT 101 - Data Representation

This document discusses how different data types are represented and stored in binary format in computer memory. It explains that all data, including integers, real numbers, text, images, and more must be converted to binary before storage. Various encoding schemes are used such as ASCII for English text, Unicode for other languages, and RGB values for images. Examples are provided for converting between decimal and binary numbers. Programming techniques for binary conversion in Python are also presented.

Uploaded by

Omar Wazed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

CMPUT 101 - Data Representation

This document discusses how different data types are represented and stored in binary format in computer memory. It explains that all data, including integers, real numbers, text, images, and more must be converted to binary before storage. Various encoding schemes are used such as ASCII for English text, Unicode for other languages, and RGB values for images. Examples are provided for converting between decimal and binary numbers. Programming techniques for binary conversion in Python are also presented.

Uploaded by

Omar Wazed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

CMPUT 101

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

− However, the sequence of animations will time well with


the audio explanations if you play the audio immediately
after a new slide is up

− Do not click the mouse and do not press the down


arrow key while the audio is playing, in order not to skip
the sequence of animations timed with the audio

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?

Computers are bistable


Notion of electric current

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

All data types must be converted to binary!


6
Computer memory
■ How is the computer memory organized?
■ Memory is organized into bytes
■ Each byte is 8 bits
■ Each bit can store 0 or 1
■ Each byte has a separate address

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

An unsigned number is a positive number


8
The decimal numbering system
⮚ Consists of integers 0 – 9
⮚ Easy to use on a daily basis
⮚ Easy to read numbers in decimal
⮚ Not possible to store decimal numbers
in computer memory!
⮚ Integer and real numbers have to be
converted to binary

9
The decimal numbering system
■ The number 2569
= 2000 + 500 + 60 + 9
= 2 x 103 + 5 x 102 + 6 x 101 + 9 x 100

⮚ Decimal numbers are base 10

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)

⮚ Binary numbers are base 2

11
How to convert from
binary to decimal?
■ We refer to a binary digit as a ‘bit’

■ Starting from the right hand-side


■ We multiply each binary bit by 2 and raise the 2
to x, where x is the exponent corresponding to
the position of the bit in the binary sequence
(Note, the first position on the right is 0)
■ We then add up all the terms resulting from the
multiplication of each bit by 2x
12
Binary to decimal conversion
examples
■ The number 1000
= 1 x 2 3 + 0 x 22 + 0 x 2 1 + 0 x 20
= 8 (in decimal)

■ The number 10101


= 1 x 2 4 + 0 x 2 3 + 1 x 2 2 + 0 x 2 1 + 1 x 20
= 16 + 0 + 4 + 0 + 1
= 21 (in decimal)
13
How to convert from
decimal to binary?
■ We repetitively divide the number in
decimal by 2 until the result of the
division is 0
■ We save the remainder of each round
of the divisions
(Note, the remainder can be either 0 or 1)
■ The binary number is the sequence of
remainders from the divisions by 2
14
Conversion from
decimal to binary
■ Example: What’s 19 in decimal?
Remainder

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)

⮚ We now verified that 10011 in binary


equals 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

■ Negative numbers require Two’s complement


operation to represent signed numbers
■ Example: -1 in decimal equals 111 in two’s
complement representation

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

■ English strings are encoded using ASCII (


American Standard Code for Information Interchange)

■ Foreign language strings are encoded using


the UNICODE (Unicode Consortium)

24
Each English character corresponds to an ASCII code
that can be converted to binary and stored in memory.

For example, the string “Hello” is encoded as follow:


H = 72
e = 101
l = 108
l = 108
o = 111

Each code is then converted to binary and stored.

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

Each foreign character corresponds to a code in the


Unicode table and can be converted to binary then
stored into the computer memory.

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

■ The more pixels, the higher the resolution

■ Each pixel is represented with three numbers


called color intensities, in RGB

■ The color intensities are converted to binary


and stored into memory
29
Images representation
■ RGB color model
(stands for Red/Green/Blue)

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

You might also like