Transcript 幻灯片 1

Chapter 2
Data and Number Representations
Outline
Data Representation (chapter2)




Data and Information
Data Types
Digital Data Representation
How To Represent Different Types of Data
Number Representation (chapter3)




Number Systems
Conversion Between Number Systems
Integer Representation
Floating-point Representation
Operations on Bits (chapter4)


Arithmetic Operations
Logical Operations
Objectives
Differentiate between data and information.
 Explain how text, images, audio and video
are represented in computers.
 Explain decimal notation, binary notation,
hexadecimal notation, and octal notation.
 Apply conversions from one number
system to another.

Objectives
Explain how integers are stored in
computers (sign-and-magnitude, one’s
complement, two’s complement).
 Explain how the Excess system works.
 Explain how to represent a floating-point
number in computers.
 Apply bit operations such as arithmetic
operations, logical operations, and shift
operations.

Part 1
Data Representation
Overview
 What’s data?
Data refers to the symbols that a computer
uses to represent facts (such as people,
events and things) and ideas.
 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.
图书登记
5000
图书数量
4000
系列1
系列2
系列3
系列4
系列5
3000
2000
1000
0
1
图书登记
2
3
4
5
图书类别
社科类 数学类 物理类 电子类 外语类 机械类 化工类
95年
96年
97年
98年
99年
928
1100
2080
2650
3108
600
980
1280
1900
2600
300
680
920
1020
1866
500
900
1600
2800
3968
600
850
1011
1400
1911
300
650
890
1080
1532
320
560
880
1225
1600
6
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
called the information processing cycle.
Data
information
Overview (con.)
There are several types of data.
Data
Text
Note:
Number
Image
Audio
Video
The computer industry uses the term
“multimedia” to define information
that contains numbers, text, images,
audio, and video.
Question : How do you handle all these
data types (text, number, image, audio
and video)?
Text
1234…
890….
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 called a bit
pattern.
Digital Data Representation
 Digital device works with discrete, distinct data or
digits, such as 1 and 0.
 Analog device works with continuous data.
Digital
Signals
Analog
Signals
Most computers are digital computers because
the digital is a relatively simple, dependable,
and adaptable technology.
 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.
 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.
1000101010111111
Byte is a kind of bit pattern. Its length is 8
bits.
1 byte = 8 bits
Data Representation (con.)
How to represent different types of data:
 Number Representation
 Text Representation
 Image Representation
 Audio Representation
Note:

In a computer, numbers are
represented using the binary
system.
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 characters (0, 1, 2 … 9)
Others (? ; blank, newline, and tab….)
 In a bit pattern, the number of bits to
represent a symbol depends on how
many symbols are in the set.
--- More symbols mean a longer bit pattern.
Text Representation (con.)
Text Representation (con.)
The relationship between the length of the bit
pattern and the number of symbols is logarithmic.
For example:
If you need 4 symbols, the length is 2 bits.
log24 =2
The forms are: 00, 01, 10 and 11

Number of symbols
Bit pattern length
2
1
16
4
128
7
…….
…….
65,536
16
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
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 means 128 (27) different symbols
can be defined.
Popular Code Schemes
Features of ASCII code :
Range of 7-bit pattern is from 0000000 to
1111111.
The first pattern (000 0000) represents the
null character.
The last pattern (111 1111) represents the
delete character.
There are 31 control characters.
…
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.
EBCDIC Code
Stands for Extended Binary Coded Decimal
Interchange Code. It was commonly used in
IBM mainframes.
It uses 8-bits pattern to represent 256
characters.
Popular Code Schemes
Unicode
Although previous codes can handle English
and European languages well, it cannot
handle all the characters of some other
languages, such as Chinese and Japanese.
Unicode, which was developed to deal with
such languages, uses 2 bytes (16 bits) to
handle 65 536 characters.
ISO Code
It is developed by ISO (International
Organization for Standardization), which
uses 4 bytes (32 bits) to handle 4 294 967
296 (232) characters.
Image Representation
Image
Bitmap
Vector
Video
Bitmap:
--- an image is divided into a matrix of pixels.
--- each pixel is assigned a bit pattern.
--- to represent a black and white image, 1
represents white pixel and 0 represents
black one.
--- to represent a color image, a pixel is
decomposed into three colors (RGB).

Image Representation (con.)
Image Representation (con.)
Image Representation (con.)
For a color image, each pixel has three bit patterns:
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.
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.

Image Representation (con.)
Image Representation (con.)
Video:
---is composed of a series of frames. A movie is
a series of frames shown one after another to
create the illusion of motion.
---each frame could be stored as a bitmap.
---a digital video requires tremendous storage
capacity.
---Refer to chapter 15 for details about video
compression.

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.
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 intervals.
2. The samples are quantized. Quantization
means assigning a value to a sample.
3. The quantized values are changed to binary
patterns.
4. The binary patterns are stored.
Audio Representation (con.)
Audio Representation (con.)
Part 2
Number Representation
 Some essential concepts

Number system: any system of
naming or representing numbers,
also called number representation
system or numeration system.

Carry: happens when the sum or
product of two or more digits equals
or exceeds the base of the number
system.
 Some essential concepts

Base: the number of digits in a number
system.

Position value: the value associated
with each digit place, also called radix,
weight or positional value.
--For example: for the decimal number 125,
the position values associated with the
character 1,2,5 are respectively 102,101,100,
therefore, (125)10=1×102+2×101+5×100.
Question: how do we compare these two
numbers, 25.6 and 52.6, in the decimal system?
(25.6)10 = 2 × 101 + 5 × 100 + 6 × 10-1
(52.6)10 = 5 × 101 + 2 × 100 + 6 × 10-1
n
(N)R =  Ki  Ri
i=-m
Ki{0,1,……,R-1}
n = number of digits of the integer - 1
m= number of digits of the decimal fraction
R = base
Ri = Position values
 Different Number Systems
Binary System (Base 2): consists of 2
digits (symbols). 0 1
Octal System (Base 8): consists of 8
digits.
01234567
Decimal System (Base 10): consists of 10
digits.
0123456789
Hexadecimal System (Base 16): consists
of 16 digits.
0123456789ABCDEF
Question: how do we tell which number
system we are using?
 Subscript method:
(101)2
Binary
(101) 8
Octal
(101) 10
Decimal
(101) 16
Hexadecimal
 Prefix method:
 Postfix method:
101B
101O
101D
101H
 Binary System
 Its base is 2. It has only two digits,1 and 0.
 There will be a carry when the result equals 2.
Position Value
Table
243
Binary Position Value Table
Position Power
Decimal Value
Binary Value
216
65536
10000 0000 0000 0000
…
…
…
210
29
28
27
1024
512
256
128
100 0000 0000
10 0000 0000
1 0000 0000
1000 0000
26
25
24
64
32
16
100 0000
10 0000
1 0000
23
22
8
4
1000
100
21
2
10
20
1
1
How to
represent
(16)10 in the 16bit system??
Decimal
Binary
Binary (16-bit system)
0
0
0000 0000 0000 0000
1
1
0000 0000 0000 0001
2
10
0000 0000 0000 0010
3
11
0000 0000 0000 0011
4
100
0000 0000 0000 0100
5
101
0000 0000 0000 0101
6
110
0000 0000 0000 0110
7
111
0000 0000 0000 0111
8
1000
0000 0000 0000 1000
9
1001
0000 0000 0000 1001
10
1010
0000 0000 0000 1010
11
1011
0000 0000 0000 1011
12
1100
0000 0000 0000 1100
13
1101
0000 0000 0000 1101
14
1110
0000 0000 0000 1110
15
1111
0000 0000 0000 1111
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 210bytes, but
the figure is commonly rounded.)
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 databases—a petabyte
represents about 1 million gigabytes!

Octal System



Its base is 8. It has eight digits: 0, 1, 2, 3, 4, 5, 6 and 7.
There will be a carry when the result equals 8.
A 3-bit pattern can be represented by an octal digit,
and vice versa.
Bit Pattern
Oct Digit
Bit Pattern
Oct Digit
000
0
100
4
001
1
101
5
010
2
110
6
011
3
111
7
 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.
 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.
Bit Pattern
0000
Hex Digit
0
Bit Pattern
1000
Hex Digit
8
0001
0010
0011
1
2
3
1001
1010
1011
9
10
11
0100
0101
0110
0111
4
5
6
7
1100
1101
1110
1111
12
13
14
15
Decimal
Binary
Octal
Hexadecimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111
0
1
2
3
4
5
6
7
10
11
12
13
14
15
16
17
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
 Conversion between number systems
Binary to Decimal
Decimal to Binary
Binary to Octal
Octal to Binary
Binary to Hexadecimal
Hexadecimal to Binary
Binary to Decimal Conversion
 Solution:
Step 1: multiply each binary digit by its
corresponding position value.
Start
Step 2: add all multiplication results
from 0
together to get the decimal number.
Binary Number
Position
Position Value
0
1
0
1
1
0
1
6
5
4
3
2
1
0
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
Decimal to Binary Conversion
Solution:
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.

Example: Convert the decimal number 35
to binary.
Stop when
the quotient
is 0
0
Quotient
Decimal
Number
1
2
4
8
17
35
1
0
0
0
1
1
Binary Number : 100011
Remainder
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?
Binary to Octal Conversion
Solution:
Step 1: Organize the pattern into groups of
3 (from right to left).
Step 2: Transform each group into an octal
digit.

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.
Octal to Binary Conversion

Solution:
Transform each octal digit into a 3-bit
binary pattern.
Octal Number
Octal Number
1
5
6
2
001 100 010
101
110
010
Binary Number
Binary Number
4
2
Binary to Hexadecimal
Solution:
Step 1: Organize the bit pattern into groups
of 4 (from right to left).
Step 2: Transform each group into a
hexadecimal digit.

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 3 digits, add extra 0s to the left.
Hexadecimal to Binary

Solution:
Transform each hexadecimal digit into
a 4-bit binary pattern.
Hexadecimal Number
2
4
C
0010 0100 1100
Binary Number
Exercises
(10 111 011.110 1)2 = (?)8
= (273.64)8
(6754.32)8 = (?)2
= (110 111 101 100. 011 010)2
(1011 1110 0110. 1101 1)2 = (?)16
= (BE6.D8)16
(A7B8.C9)16 = (?)2
= (1010 0111 1011 1000. 1100 1001)2
 Integer Representation


Integers are the whole numbers, which include
positive integers and negative integers.
The figure below shows different integers.
Integers
Unsigned
Sign-andMagnitude
Signed
One’s
Two’s
Complement Complement
 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 number so
that there is a total of N bits.
Add zeros to the left when needed
Decimal
7
8-bit Allocation
16-bit Allocation
0000 0111
0000 0000 0000 0111
234
1110 1010
0000 0000 1110 1010
258
overflow
0000 0001 0000 0010
24,760
overflow
0110 0000 1011 1000
1,245,678
overflow
overflow
Unsigned integers in
two different computers
Exceeds the capacity of the storage
 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 an 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.
Note: There are two 0s in sign-and-magnitude
representation: positive zero (+0) and
negative zero (-0).
---For example: In an 8-bit allocation,
+0  00000000
-0  10000000
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 there 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.

Example: Store +7 in an 8-bit memory location,
and store –258 in a 16-bit memory location using
sign-and-magnitude representation .
+7
Question: how
about +7 in a 16-bit
allocation, and -258
in an 8-bit allocation?
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
 Signed Integers
One’s Complement Representation




To represent a positive number, use the
convention adopted for an unsigned integer.
To represent a negative number, complement
the positive number.
In one’s complement, the complement of a
number is obtained by changing all 0s to 1s
and all 1s to 0s.
The leftmost bit defines the sign of the
number : 0 for positive, and 1 denotes negative.
Range of signed integers: -(2N-1-1) ~ +( 2N-1-1)
---N is the number of bits allocated to
represent a one’s complement integer.
Note: There are two 0s in one’s complement
representation, too: positive zero (+0) and
negative zero (-0).
---For example: In an 8-bit allocation,
+0  00000000
-0  11111111
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 to
make a total of N bits.
Step 3: If the sign is positive, no action is needed.
If negative, every bit is complemented.

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
Note:
One’s complementing means reversing
all bits.
 If you one’s complement a positive
number, you get the corresponding
negative number.
 If you one’s complement a negative
number, you get the corresponding
positive number.
 If you one’s complement a number
twice, you get the original number.
 Signed Integers
Two’s Complement :
Range of signed integers: -(2N-1) ~ +( 2N-1-1)
--- N is the number of bits allocated to
represent a two’s complement integer.
 How to store a two’s complement integer:
Step 1: Convert the number to binary, ignore the
sign.
Step 2: 0s are added to the left of the number to
make a total of N bits.
Two’s
complement
is thenomost
common,
Step
3: If the
sign is positive,
action
is needed.
Ifthe
negative,
leave all theand
rightmost
0s and
the
most important,
the most
widely
first 1 unchanged. Complement the rest of the
used representation of integers today.
bits.

Example: Store +7 in an 8-bit memory location,
and store –40 in a 16-bit memory location using
two’s complement representation .
+7
1 1 1
0 0 0 0 0 1 1 1
1 0 1 0 0 0
-40
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 1 0 0 0
Note:
For negative numbers:
Two’s complement =one’s complement + 1
Note:





In two’s complement representation,
the leftmost bit defines the sign of the number.
 If it is 0, the number is positive.
 If it is 1, the number is negative.
There is only one 0 in two’s complement:
For example: in an 8-bit allocation,
0  00000000
If you two’s complement a positive number,
you get the corresponding negative number.
If you two’s complement a negative number,
you get the corresponding positive number.
If you two’s complement a number twice, you
get the original number.
Summary of integer representation
Contents of
Memory
-----------0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Unsigned
-----------0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Sign-andMagnitude
--------+0
+1
+2
+3
+4
+5
+6
+7
-0
-1
-2
-3
-4
-5
-6
-7
One’s
Complement
--------+0
+1
+2
+3
+4
+5
+6
+7
-7
-6
-5
-4
-3
-2
-1
-0
Two’s
Complement
-------+0
+1
+2
+3
+4
+5
+6
+7
-8
-7
-6
-5
-4
-3
-2
-1
Excess system
In an excess conversion, a positive number,
called the magic number, is used in the
conversion process.
 The magic number is normally (2N-1) or ( 2N-1-1),
where N is the bit allocation. For example, if N
is 8, the magic number is either 128
(Excess_128) or 127(Excess_127).
 To represent a number in Excess, use the
following procedure:
Step 1: Add the magic number to the integer.
Step 2: Change the result to binary and add 0s to
make a total of N bits.

Example: Represent –25 in Excess 127 using
an 8-bit allocation.
Step 1: Add 127 to –25 and get 102.
Step 2: Change the result to binary, which is
1100110.
Step 3: Add one 0 to make a total of 8 bits. The
representation is 01100110
 Floating-point Representation
Integer Part
1 4 . 2 3 4
Fraction Part
See Decimal to binary Conversion
 To convert a floating-point number to binary:
Step 1: Convert the integer part to binary.
Step 2: Convert the fraction part to binary.
Step 3: Put decimal point between the two
parts.
Repetitive Multiplication Method
Changing fractions to binary
Multiply
Multiply2by
to 2gain
to
get
integer
integer
part
part
Stop when the
result is 0
0.125
0.250
0.500
1.000
0.
0
0
1
0.000
Transform the fraction 0.4 to a binary of 6 bits?
0.4
0.8
1.6
1.2
0.4
0.8
1.6
0.
0
1
1
0
0
1
Example: transform the number 49.58 to
a binary ( 3 bits after the decimal point)?
( 49 . 58 )10 = ( 110001.100 )2
Integer Part
2
2
2
2
2
2
49
24
12
6
3
1
0
-------------------
1
0
0
0
1
1

1

0

0
0.58
2
.16
2
.32
2
.64
Fraction Part
 Floating-point Representation
 Normalization: the moving of the decimal point so
that there is only one ‘1’ to the left of the decimal
point (1. XXXXXXXXX) .
6 digits
+ 1010001 . 11001
+ 26×1.01000111001
3 digits
- 0 . 001110011
- 2-3×1.110011
± 2e × 1.XXXXXXX
Sign
(1 bit)
Exponent
Mantissa
(Excess representation)
(unsigned integer)
 Floating-point Representation
 IEEE Standard:
Single-precision format
4 bytes = 32 bits
sign
1
8
23
exponent
1
11
mantissa
52
Double-precision format
8 bytes = 64 bits
 Floating-point Representation
 IEEE Standard:
Show the representation of the
normalized number:
+ 26 × 1.01000111001
Example:
The sign is positive. The Excess_127 representation of
the exponent is 133. You add extra 0s on the right
to make it 23 bits. The number in memory is stored
as:
0 10000101 01000111001000000000000
Interpret the following 32-bit
floating-point number
1 01111100 11001100000000000000000
Example:
The sign is negative. The exponent is –3 (124 –
127). The number after normalization is
-2-3 × 1.110011
Quiz



The decimal equivalent of the binary number 11.11
is
.
Show the following number in 32-bit IEEE
format:
.
-2-5×1.01101001
Suppose the following bit pattern represents a value
stored in two’s complement notation. Find the two’s
complement representation of the negative of the
value.
01010101
86
Part 3
Operations on Bits
Bit Operations
Arithmetic
Logical
Shift
Arithmetic operations




Arithmetic operations involve adding, subtracting,
multiplying, dividing, and so on.
You can apply these operations to integers and
floating-point numbers.
The multiplication operation can be implemented
in software using repeated addition or in
hardware using other techniques.
The division operation can also be implemented
in software using repeated subtraction or in
hardware using other techniques.
Arithmetic Operations
Table: adding bits
Number of 1s
0
1
2
3

Result
0
1
0
1
Carry
1
1
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.
Example: add the following numbers in two’s
complement representation:
(+24) + (-17) ; (+127) + (+3) .
(+24) + (-17) = +7
1
+
(+127) + (+3) = +130
Carry
1111
00011000
1111111
01111111
11101111
00000011
+
0 0 0 0 0 1 1 1 Result
10000010
discard
What’s wrong???
Data overflow !!!
(-126)10
Overflow


Overflow will happen
when the value
exceeds the range of
the allocation.
Range of numbers in
two’s complement
representation:
1111,1111
(-1) + 0 = -1
- (2N-1) --- 0 --- +(2N-1 –1)
127+3= -126
1000,0010
127+1= -128
1000,0000
Note:

When you do arithmetic operations on
numbers in a computer, remember that
each number and the result should be in
the range defined by the bit allocation.
To subtract, negate (two’s complement) the second
number and add. For example:
(+101) - (+62)  (+101) + (-62)
Carry
1 1
0 1 1
1 1 0
0
0
0
0
1
0
0
1
1
0
---------------------------------Result
0 0 1 0 0 1 1 1
The leftmost carry is discarded.
+
 39
Addition and subtraction for
floating-point numbers
1.
2.
3.
4.
5.
Check the signs. If different, use the sign
of the number whose absolute value is
larger.
Move the decimal point to make the
exponents the same.
Add or subtract the mantissas (including
the integer part and fraction part).
Normalize the result before storing in
memory.
Check for overflow.
Example
Add two floats:
0 10000100 10110000000000000000000
0 10000010 01100000000000000000000
Solution
The exponents are 5 and 3. The numbers are:
+25 × 1.1011 and +23 × 1.011
Make the exponents the same.
(+25 × 1.1011)+ (+25× 0.01011)  +25 × 10.00001
After normalization +26 × 1.000001, which is stored
as:
0 10000101 000001000000000000000000
 Logical Operations
 Some essential concepts
It’s a cat , not a tiger !
True
It’s a red tulip !
False
Each logical variable can only be
True or False.
 In the computer system, “1” means
“True”, “0” means “False”.

 Some essential concepts

According to the number of operands
they take, operators can be categorized
as: unary, binary and Ternary.
Input
Input
Input
Unary
Binary
Output
Output
 Some essential concepts
Logical Operations
Unary
NOT
Binary
AND
OR
XOR
 Some essential concepts
 A truth table shows all the possible input
combinations of input with the corresponding
output.
x
y
x AND y
x
0
1
NOT x
1
0
x
0
0
y
0
1
x OR y
0
1
1
1
0
1
1
1
0
0
1
1
0
1
0
1
0
0
0
1
x
0
y
0
x XOR y
0
0
1
1
1
0
1
1
1
0
NOT operator
The NOT operator has one input. It inverts
bits; that is, it changes 0 to 1 and 1 to 0.
Example
Use the NOT operator on the bit pattern 10011000
Solution
Target
Result
10011000
-----------------01100111
NOT
AND operator
Example
Use the AND operator on bit patterns 10011000
and 00110101.
Solution
Target
Result
10011000
00110101
-----------------00010000
AND
Inherent rule of the AND operator
If a bit in one input is 0, you can quickly
conclude that the result is 0.
OR operator
Example
Use the OR operator on bit patterns 10011000 and
00110101
Solution
Target
Result
10011000
00110101
-----------------10111101
OR
Inherent rule of the OR operator
If a bit in one input is 1, you can quickly
conclude that the result is 1.
XOR operator
Example
Use the XOR operator on bit patterns 10011000
and 00110101.
Solution
Target
Result
10011000
00110101
-----------------10101101
XOR
Inherent rule of the XOR operator
If a bit in one input is 1, the result is the
inverse of the corresponding bit in the other
input.
Mask
A mask is a special bit pattern used to
modify another bit pattern. We can use
masks to unset, set, or reverse specific bits.
Example of unsetting specific bits
Example
Use a mask to unset (clear) the 5 leftmost bits of a
pattern. Test the mask with the pattern 10100110.
Solution
The mask is 00000111.
Target
Mask
Result
10100110
00000111
-----------------00000110
AND
Example
Imagine a power plant that pumps water to a city
using eight pumps. The state of the pumps (on or
off) can be represented by an 8-bit pattern. For
example, the pattern 11000111 shows that pumps
1 to 3 (from the right), 7 and 8 are on while pumps
4, 5, and 6 are off. Now assume pump 7 shuts
down. How can a mask show this situation?
Solution
Use the mask 10111111 to AND with the target
pattern. The only 0 bit (bit 7) in the mask turns
off the seventh bit in the target.
Target
Mask
Result
11000111
10111111
-----------------10000111
AND
Example of setting specific bits
Example
Use a mask to set the 5 leftmost bits of a pattern.
Test the mask with the pattern 10100110.
Solution
The mask is 11111000.
Target
Mask
Result
10100110
11111000
-----------------11111110
OR
Example
Using the power plant example, how can you use
a mask to to show that pump 6 is now turned on?
Solution
Use the mask 00100000.
Target
Mask
Result
10000111
00100000
-----------------10100111
OR
Example of flipping specific bits
Example
Use a mask to flip the 5 leftmost bits of a pattern.
Test the mask with the pattern 10100110.
Solution
Target
Mask
Result
10100110
11111000
-----------------01011110
XOR
Shift operations
If a bit pattern can be shifted to the right or
to the left.
Example
Show how you can divide or multiply a number by
2 using shift operations.
Solution
If a bit pattern represents an unsigned number,
a right-shift operation divides the number by
two. The pattern 00111011 represents 59. When
you shift the number to the right, you get
00011101, which is 29. If you shift the original
number to the left, you get 01110110, which is
118.
Example
Use a combination of logical and shift
operations to find the value (0 or 1) of the
fourth bit (from the right).
Solution
Use the mask 00001000 to AND with the
target to keep the fourth bit and clear the rest
of the bits.
Solution (continued)
Target
Mask
Result
abcd efgh
00001000
-----------------0000e 000
AND
Shift the new pattern three times to the right
0000e000  00000e00  000000e0  0000000e
Now it is easy to test the value of the new pattern as
an unsigned integer. If the value is 1, the original bit
was 1; otherwise the original bit was 0.
Objectives
Differentiate between data and information.
 Explain how text, images, audio and video
are represented in computers.
 Explain decimal notation, binary notation,
hexadecimal notation, and octal notation.
 Apply conversions from one number
system to another.

Objectives
Explain how integers are stored in
computers (sign-and-magnitude, one’s
complement, two’s complement).
 Explain how the Excess system works.
 Explain how to represent a floating-point
number in computers.
 Apply bit operations such as arithmetic
operations, logical operations, and shift
operations.

That’s all for this chapter!