0% found this document useful (0 votes)
17 views102 pages

Ch02 Data and Number Representations 1127

Uploaded by

Mayssoune Bv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views102 pages

Ch02 Data and Number Representations 1127

Uploaded by

Mayssoune Bv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 102

Introduction to Computer Science

Chapter 2 Data and Number


Representations

Liuying Wen
School of Computer Science
Email : [email protected]
Outline
 Data Representation
 Data and information
 Data Types
 Digital Data Representation
 How to Represent Different Types of Data
 Number Representation
 Number Systems
 Conversion Between Number Systems
 Integer Representation
 Operations On Bits
 Arithmetic Operations
 Logical Operations

2
Objectives
 Differentiate between data and information.
 Explain how text, image, audio are represented
in computers.
 Explain decimal notation, binary notion,
hexadecimal notation, and octal notation.
 Apply conversions from one number system to
another.

3
Objectives

 Explain how integers are stored in computers


 Apply bit operations such as arithmetic operations,
logic operations, and shift operations.

4
Part 1

Data Representation

5
Overview

 What’s data?
 Data refers to the symbols that a computer uses to
represent facts(such as people, events and things and idea).
 What’s information?
 The words, numbers, and graphics used as the basis for
human actions and decisions.
 The difference between data and information:
 Data becomes information when it is presented in a format
that people can understand and use.

6
Data VS Information
Data Information

Meaning 1. Data is raw, unorganized facts When data is processed,


that need to be processed. organized, structure or
2. Data can be something simple presented in a given
and seemingly random and useless context so as to make it
until it is organized. useful, it is called
information.

Example Each student’s test score is one The average score of a


piece of data. class or of the entire school
is information that can be
derived from the given
data.

7
Overview(con.)

 Data entered into a computer is called input.


 The processed information are called output.
 The cycle of input, process, output , and storage is
call the information processing cycle.

8
Overview(con.)
 There are different types of data

Data

Text Number Image Audio Video

The computer industry uses the term


“multimedia” to define information that
contains numbers, text, images, audio, and video

9
Question: How do you handle all these data
types(text, number, image, audio and
video)?

10
Solution

 The most efficient solution is to use a uniform


representation of data.
 All data types from outside a computer are
transformed into this uniform representation
when stored in a computer and then
transformed back when leaving the computer.
 This universal format is call a bit pattern.

11
Digital Data Representation
 Digital device works with discrete, distinct data or
digits, such as 1 and 0
 Analog device works with continuous data.

Most computers are digital computers


because the digital is a relatively simple.
Dependable, and adaptable technology
12
Some important concepts

Bit—short for binary digit, is the smallest unit


of data that can be stored in a computer.
In the binary system, each 0 and 1 is called a
bit.
In a two-state on/off arrangement(such as a
switch), one state can represent a 1 digit(on), the
other represents a 0 digit(off).
Computers use sequences of bits(bit pattern) to
represent all kinds of data.
13
Some important concepts(con.)
 Bit pattern is a sequence or a string of bits.
 It is the responsibility of I/O devices or programs to
interpret a bit pattern as a number, text, or some type of
data.

Byte is a kind of bit pattern. Its length is 8 bits.


1 byte = 8 bits
14
Digital Data Representation(con.)
How to represent different types of data:
 Text Representation
 Image Representation
 Audio Representation
 Number Representation

In a computer, numbers are represented


used the binary system.

15
Text Representation
 A piece of text in any language is a sequence of
symbols.
 Symbol examples:
 26 uppercase letters(A,B,C,…,Z)
 26 lowercase letters(a, b, c, … , z)
 10 numeric character(0,1,2,…,9)
 Others(?;blank, newline, and table…)
 In a bit pattern, the number of bits to represent a
symbol depends on how many symbols are in the set.
 More symbol mean a longer bit pattern.
16
Text Representation(con.)

17
Text Representation(con.)
 The relationship between the length of the bit pattern
and number of symbols is logarithmic.
For example:
If you need 4 symbols, the length is 2 bits.
Log2 4=2 The forms are: 00, 01, 10 and 11.

18
Text Representation(con.)
Different sets of bit patterns have been designed
to represent text symbols.
Each set is called a code, or code scheme.
There are some common codes:
 ASCII
 Extended ASCII
 EBCDIC
 Unicode
 ISO
19
Popular Code Schemes
ASCII Code
Stands for American Standard Code for
Information Interchange.
It was developed by ANSI(American National
Standards Institute).
It is used on most microcomputers, many
minicomputers, and some mainframe computers.
This code uses 7 bits for each symbol, which mean
128(27)different symbols can be defined.

20
Popular Code Schemes
Features of ASCII Code
Range of 7-bit pattern is from 0000000 to 1111111.
The first pattern 0000000 represents the null
character.
The last pattern 1111111 represents the delete
character.
There are 31 control characters.
……

21
22
Popular Code Schemes
Extended ASCII Code
To make the size of each pattern 1 byte(8 bits), the
ASCII bit patterns are augmented with an extra 0
at the left.
The code is from 00000000 to 01111111.
The left bit (0) is extended bit.

23
Image Representation
Image

Bitmap Vector
Bitmap
 An image is divided into a matrix of pixels.
 Each pixel is assigned a bit pattern.
 To represent a black an white image, 1 represents white
pixel and 0 represents black one.
 To represent a color image, a pixel is decomposed into
three colors(RGB)
24
Image Representation(con.)

25
26
Image Representation(con.)

For a color image, each pixel has three bit


pattern: one to represent the intensity of the red
color, one to represent the intensity of the green
color, and one to represent the intensity of the
blue color.

27
Image Representation(con.)
Vector
 An image is decomposed into a combination of curves
and lines.
 Each curve or line is represented by a mathematical
formula.
 For example, a line may be described by the
coordinates of its endpoints, and a circle may be
described by the coordinates of its center and the
length of its radius.
 They use much less storage space than bitmap images,
but do not look as realistic as bitmap images.
28
29
vector graphic----
a set of instructions that recreates a picture
draw circle
center 0.5, 0.5
radius 0.4
fill-color yellow
stroke-color black
stroke-width 0.05
draw circle
center 0.35, 0.4
radius 0.05
fill-color black
draw circle
center 0.65, 0.4
radius 0.05
fill-color black
draw line
start 0.3, 0.6
end 0.7, 0.6
stroke-color black
stroke-width 0.1
Audio Representation

Audio is a representation of sound or music.


Music, voice and sound effects can all be recorded
as waveform, which is by nature analog data.
Samples of the sound are collected as periodic
intervals and stored as numeric data.
Sampling rate : the number of times per second
that the sound is measured during the recording
process. Expressed in hertz(Hz)

31
Audio Representation(con.)

The steps to change audio data to bit patterns:


1. The analog data is sampled. Sampling means
measuring the value of the signal at equal interval.
2. The samples are quantized. Quantization means
assigning a value to a sample.
3. The quantized values are changed to binary
pattern.
4. The binary patterns are stored.

32
Audio Representation(con.)

33
Part 2

Number Representation

34
Some essential concepts
 Number system: any system of naming or
representing numbers, also called number
representation system, or numeration.
 Base/ Radix : the number of digits in a number
system.
 Carry: happens when the sum or product of two
or more digits equals or exceeds the base of the
number system
 Position value: the value associated with each
digit place, also called weight or positional value.

35
For example: for the decimal number 263,
the position values associated with the
character 2, 6, 3 are respectively 102, 101,
100, therefor,

(263)10=2*102 + 6* 101 + 4* 100

36
37
Different Number System

Number
Base Digits(Symbols)
System
Decimal 10 0,1,2,3,4,5,6,7,8,9
Binary 2 0,1
Octal 8 0,1,2,3,4,5,6,7
Hexadecimal 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

38
Question: How do we tell which number
system we are using?

Subscript method:
(101)2 Binary 101B
(101)8 Octal 101O
(101)10 Decimal 101D
(101)16 Hexadecimal 101H

Prefix method:
Postfix method:
39
1. Decimal System
Nowadays , there are two dominant
number systems in the world: decimal system
and binary system.
It has ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

40
2. Binary System

Computer store, process and transmit data


with binary form.
Its base is 2. It has only two digits, 1 and 0.

41
2. Binary System

42
Binary Position Value Table

43
How computer capacity is
expressed: bit by bit
 The following terms are used to denote
capacity:
 Bit: In the binary system, the binary digit(bit)-0
or 1-is the smallest unit of measurement.
 Byte: A group of 8 bits is called a byte, and a byte
represents one character, digit, or other value.
 Kilobyte: A kilobyte(K, KB) is about 1000 bytes.
(Actually, it’s precisely 1024, that is 210 bytes)

44
How computer capacity is
expressed: bit by bit
 Megabyte: A megabyte(M, MB) is about 1 million
bytes(220).
 Gigabyte: A gigabyte(G, GB) is about 1 billion
bytes(230).
 Terabyte: A terabyte(T, TB) represents about 1
trillion bytes(240).
 Petabyte(250): A new measurement accommodates
the huge storage capacities of modern database-a
petabyte represents about 1 million gigabytes!

45
How computer capacity is
expressed: bit by bit

1 byte = 8 bit
1KB (KiloByte) = 210 B
1MB (MegaByte)= 210 KB
1GB (GigaByte)= 210 MB
1TB (Terabyte) = 210 GB

46
3. Octal System

Its base is 8.
It has eight digits: 0, 1, 2, 3, 4, 5, 6 and 7.
A 3-bit pattern can be represented by an
octal digit, and vice versa.

47
4. Hexadecimal System
Its base is 16..
It has sixteen digits : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
A, B, C, D, E and F.
A 4-bit pattern can be represented by a
hexadecimal digit, and vice versa

48
49
Conversion between number
systems

50
Binary to Decimal Conversion

Step 1: multiply each binary digit by its


corresponding position value.
Step 2: add all multiplication results together
to get the decimal number.

51
Binary to Decimal
Conversion(con.)

Binary Number 0 1 0 1 1 0 1

Position 6 5 4 3 2 1 0
Position value 26 25 24 23 22 21 20

0× 26 + 1 × 25 + 0 × 24 + 1 ×23 + 1 × 22 + 0 × 21 + 1 ×20

Result 0 + 32 + 0 + 8 + 4 + 0 + 1 = (45)10

52
Decimal to Binary Conversion

 Step 1: divide the number by 2 and write the quotient


and remainder.
 Step 2: use the remainder(from step 1) as the
corresponding binary digit(from right to left), 1 or 0.
 Step 3: check whether the quotient is 0 or not.
If it is zero, skip to the Step 4;
otherwise, use the quotient as the number and go
back to Step 1.
 Step 4: stop and put all remainders together to get the
binary number.

53
Example: convert the decimal
number 35 to binary.
Stop when the
quotient is 0 Quotient

0 1 2 4 8 17 35

1 0 0 0 1 1

Remainder
Binary Number: 100011
54
Questions:

 What if the number has a fraction part?


 How do we convert octal to decimal, and
decimal to octal?
 How do we convert hexadecimal to
decimal, and decimal to hexadecimal?

55
Binary to Octal Conversion

Step 1: Organize the pattern into groups of


3(from right to left).
Step 2: Transform each group into an octal
digit.

56
Binary to Octal Conversion(con.)

1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0

1 7 6 3 4 4

If the leftmost bit pattern does not


contain 3 digits, add extra 0s to the
left.
57
Octal to Binary Conversion

Transform each octal digit into a 3-bit binary


pattern.

58
Binary to Hexadecimal Conversion

Step 1: Organize the pattern into groups of


4(from right to left).
Step 2: Transform each group into an
hexadecimal digit.

59
Binary to Hexadecimal Conversion(con.)

1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0

F C E 4

If the leftmost bit pattern does not


contain 4 digits, add extra 0s to the
left.
60
Hexadecimal to Binary Conversion

Transform each octal digit into a 4-bit binary


pattern.

61
Exercises

(10 111 011.1101)2 = (?) 8 = (273.64) 8

(6754.32)8 = (?) 2 = (110 111 101 100. 011 010) 2

(1011 1110 0110. 1100 1)2= (?) 16 = (BE6.D8) 16

(A7B8.C9)16 = (?) 2

= (1010 0111 1011 1000. 1100 1001) 2


62
Integer Representation
Integers are the whole numbers, which include
positive integers and negative integers.
The figure below shows different integers.

Integers

Unsigned Signed

Sign-and- One’s Two’s


Magnitude Complement Complement

63
1. Unsigned Integers

 Range of unsigned integers: 0-(2N-1)


 N is the number of bits the computer allocates to store an
unsigned integer.
 For example:
8-bit computer: 0-255
16-bit computer: 0-65,535
 How to store an unsigned integer:
 Step 1: Convert the number to binary.
 Step 2: If the number of bits is less than N, 0s are added to the
left of the binary system so that there is a total of N bits.

64
65
2. Signed Integers

 Sign-and-Magnitude Representation
 The leftmost bit defines the sign of the number: 0 for positive,
and 1 for negative.
 For example: in a 8-bit allocation, the leftmost bit shows the
sign and the other seven bits represent the absolute value.
 Range of signed integers: -(2N-1-1) ~ + (2N-1-1)
 N is the number of bits allocated to represent one sign-and-
magnitude integer.

66
Sign-and-Magnitude Representation

How to store a sign-and-magnitude integer:


Step 1: Convert the number to binary, the
sign is ignored.
Step 2: If the number of bits is less than N-1,
0s are added to the left of the number so that
is a total of N-1 bits.
Step 3: Add sign-bit to the left(to make it N
bits): if it’s positive, add 0; if negative, add 1.

67
Example: Store +7 in an 8-bit memory
location, and store -258 in a 16-bit memory
location using sign-and-magnitude
representation.
+7
1 1 1
0 0 0 0 1 1 1
0 0 0 0 0 1 1 1

-258
1 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0

68
2. Signed Integers
One’s Complement Representation
The one’s complement of a positive binary
number is the same as the original binary
number.
To obtain one’s complement of a negative
binary number, you simply need to flip all the
bits(0s to 1s and 1s to 0s)----except for the
sign-bit.

69
One’s Complement Representation

How to store a one’s complement integer:


Step 1: Convert the number to binary, the
sign is ignored.
Step 2: 0s are added to the left of the number
so that is a total of N bits.
Step 3: If it’s positive, no action is needed;
If negative, every bit is complemented
(0s to 1s and 1s to 0s).

70
Example: Store +7 and -7 in an 8-bit memory
location, and store -258 in a 16-bit memory
location using one’s complement representation.

+7 -7
1 1 1 1 1 1
0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1
1 1 1 1 1 0 0 0

-258
1 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0
1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1

71
2. Signed Integers

Two’s Complement Representation


Two’ s complement is the most common, the
most important, and the most widely used
representation of integers today.

72
Two’s Complement Representation

How to store a two’s complement integer:


Step 1: Convert the number to binary, the
sign is ignored.
Step 2: 0s are added to the left of the number
so that is a total of N bits.
Step 3: If it’s positive, no action is needed;
If negative, every bit is complemented
(0s to 1s and 1s to 0s), then plus 1 to
the last digit.
73
Example: Store +7 in an 8-bit memory location,
and store -40 in a 16-bit memory location using
two’s complement representation.

+7
For negative numbers:
1 1 1
Two’s complement = one’s complement + 1
0 0 0 0 0 1 1 1

-40
1 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0
1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0

74
Floating-point Representation
Integer Part 14 . 234 Fraction Part

To convert a floating-point number to binary


See Decimal to Binary Conversion

Step 1: Convert the integer part to binary.


Step 2: Convert the fraction part to binary.
Step 3: Put decimal point between the two
part.Repetitive Multiplication Method

75
Changing fractions to binary
Multiply by 2 to Stop when
get integer part the result is 0

0.125 0.250 0.500 1.000 0.000

0 0 0 1
Integer
part
Binary Number: 0.0001
76
Excise: transform the number 49.58 to a
binary(3 bits after the decimal point)?

(49.58)10 = (?) 2 = (110001. 100) 2

77
Part 3

Operations on Bits

78
Bit Operations

Arithmetic Logical Shift

79
Arithmetic Operations

Arithmetic operations involve adding( + ),


subtracting( - ), multiplying(×), dividing(÷),
and so on.
You can apply these operations to integers and
floating-point numbers.

80
Arithmetic Operations

Table: adding bits


Operand1 Operand 2 Operation Result Carry
0 0 + 0
0 1 + 1
1 0 + 1
1 1 + 0 1

81
Example: (10111)2+ (00011)2=?

10111
+) 00011
11010

82
Question: How does the
computer subtract two integers?
For example: 5 – 2 = ?
1. Transform subtracting to adding.
2. Use the two’s complement representation.

 Rule of Adding integers in Two’s Complement:


Add 2 bits and propagate the carry to the next
column. If there is a final carry after the leftmost
column addition, discard it.

83
Example: subtract the following numbers in
two’s complement representation:
(5)10- (2)10=?

(5)10 - (2)10 = (5)10 + (-2)10 =3


1 1 Carry
Discard
0 1 0 1

+ 1 1 1 0

0 0 1 1 3

84
Exercises

Subtract the following numbers in


two’s complement representation:
(24)10 - (17)10

85
Logical Operations
It’s a cat, not a tiger!

True
It’s a red rose!
False
Each logical variable can only be True or False.
In the computer system, “1” means “True”, “0”
means “False”.
86
Some essential concepts

According to the number of operands they


take, operators can be categorized as : unary,
binary and Ternary.

87
Some essential concepts

88
NOT operator

The NOT operator has one input.


It inverts bits; that is, it changes 0 to 1
and 1 to 0.

89
Example: Use the NOT operator on bit pattern
10011000

NOT 10011000
01100111

90
AND operator

The AND operator has two input.


If two bits in two inputs are both 1, the
result is 1; and in other cases, the result is
0.

91
Example: Use the AND operator on bit pattern
10011000 and 00110101

10011000
AND 00110101
00010000

92
Inherent rule of AND operator

If a bit in one input is 0, you can quickly


conclude that the result is 0

93
OR operator

The OR operator has two input.


If a bit in one input is 1, the result is 1.

94
Example: Use the OR operator on bit pattern
10011000 and 00110101

10011000
OR 00110101
10111101

95
Inherent rule of OR operator

If a bit in one input is 1, you can quickly


conclude that the result is 1

96
XOR operator

The XOR operator has two input.


The result is 1 only if two bits in two
inputs are not the same.

97
Example: Use the XOR operator on bit pattern
10011000 and 00110101

10011000
XOR 00110101
10101101

98
Inherent rule of XOR operator

If a bit in one input is 1, the result is the


inverse of the corresponding bit in the
other input.

99
Objectives
 Differentiate between data and information.
 Explain how text, image, audio and video are
represented in computers.
 Explain decimal notation, binary notion,
hexadecimal notation, and octal notation.
 Apply conversions from one number system to
another.

100
Objectives

 Explain how integers are stored in computers(sign-


and-magnitude, one’s complement, two’s
complement)
 Apply bit operations such as arithmetic operations,
logic operations, and shift operations.

101
That’s all for this
chapter!

102

You might also like