chapter 2 data and number representations. outline data representation (chapter2) data and...

127
Chapter 2 Data and Number Representations

Upload: rosamund-kennedy

Post on 29-Dec-2015

230 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Chapter 2

Data and Number Representations

Page 2: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

OutlineOutline

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

Page 3: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 4: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 5: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Part 1

Data Representation

Page 6: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

OverviewOverview

Page 7: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

图书登记

0

1000

2000

3000

4000

5000

1 2 3 4 5 6 7图书类别

图书

数量 1系列

2系列3系列4系列5系列

社科类 数学类 物理类 电子类 外语类 机械类 化工类95年 928 600 300 500 600 300 32096年 1100 980 680 900 850 650 56097年 2080 1280 920 1600 1011 890 88098年 2650 1900 1020 2800 1400 1080 122599年 3108 2600 1866 3968 1911 1532 1600

图 书 登 记

Page 8: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Overview (con.)Overview (con.)

Data information

Page 9: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

The computer industry uses the term

“multimediamultimedia” to define information that contains

numbers, text, images, audio, and video.

There are several types of data.Overview (con.)Overview (con.)

DataData

TextText NumberNumber ImageImage AudioAudio VideoVideo

Note:

Page 10: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

12341234……

890890….….

TextText

Page 11: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Solution

The most efficient solution is to use a uniform representationuniform representation of data.

All data types from outside a computer are transformed intotransformed into this uniform representation when stored in a computer and then transformed transformed backback when leaving the computer.

This universal format is called a bit bit patternpattern.

Page 12: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

DigitalSignals

AnalogSignals

Page 13: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Some important concepts

Bit– short for binary digit, is the smallest smallest unit of dataunit 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 bit patternpattern) to represent all kinds of data.

Page 14: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Some important concepts (con.)

11 0 0 0 0 0 0 11 0 0 11 0 0 11 0 0 1 1 1 1 1 11 1 1 1 1 1

1 byte = 8 bits

Page 15: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Data Representation (con.)Data Representation (con.)

How to represent different types of data: Number Representation Text Representation Image Representation Audio Representation

In a computer, numbers are represented using the binary system.

In a computer, numbers are represented using the binary system.

Note:

Page 16: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Page 17: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Text Representation (con.)

Page 18: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Page 19: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent
Page 20: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Text Representation (con.)

Page 21: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 22: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Page 23: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent
Page 24: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Page 25: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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 CodeIt is developed by ISO (International Organization for Standardization), which uses 4 bytes (32 bits) to handle 4 294 967 296 (232) characters.

Popular Code Schemes

Page 26: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Image

Bitmap Vector Video

Page 27: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Image Representation (con.)

Page 28: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Image Representation (con.)

Page 29: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 30: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 31: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Image Representation (con.)

Page 32: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 33: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 34: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.)

Page 35: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Audio Representation (con.)

Page 36: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Audio Representation (con.)

Page 37: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Part 2

Number Representation

Page 38: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 39: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Some essential concepts

Page 40: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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)R = Ki Ri Ki{0,1,……,R-1} i=-m

n

n = number of digits of the integer - 1

m= number of digits of the decimal fraction

R = base

Ri = Position values

Page 41: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Different Number Systems

Binary System (Base 2): consists of 2 digits (symbols).

Octal System (Base 8): consists of 8 digits.

Decimal System (Base 10): consists of 10 digits.

Hexadecimal System (Base 16): consists of 16 digits.

0 10 1

0 1 2 3 4 5 6 70 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7 8 90 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9 A B C D E F0 1 2 3 4 5 6 7 8 9 A B C D E F

Page 42: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Question: how do we tell which number system we are using?

Subscript method:

Prefix method:

Postfix method:

(101)2

(101) 8

(101) 10

(101) 16

Binary

Octal

Decimal

Hexadecimal

101B

101O

101D

101H

Page 43: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Binary System Its base is 2. It has only two digits,1 and 0. There will be a carry when the result equals 2.

243

Position Value Table

Page 44: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Position Power Decimal Value Binary Value

216 65536 10000 0000 0000 0000… … …

210 1024 100 0000 0000

29 512 10 0000 0000

28 256 1 0000 0000

27 128 1000 0000

26 64 100 0000

25 32 10 0000

24 16 1 0000

23 8 1000

22 4 100

21 2 10

20 1 1

Binary Position Value Table

Page 45: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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 to represent

(16)10 in the 16-bit system??

Page 46: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.)

Page 47: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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!

Page 48: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Octal System

Bit Pattern Oct Digit Bit Pattern Oct Digit

000 0 100 4

001 1 101 5

010 2 110 6

011 3 111 7

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.

Page 49: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 50: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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 Hex Digit Bit Pattern Hex Digit

0000 0 1000 8

0001 1 1001 9

0010 2 1010 10

0011 3 1011 11

0100 4 1100 12

0101 5 1101 13

0110 6 1110 14

0111 7 1111 15

Page 51: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Decimal Binary Octal Hexadecimal

0 0 0 01 1 1 12 10 2 23 11 3 34 100 4 45 101 5 56 110 6 67 111 7 78 1000 10 89 1001 11 9

10 1010 12 A11 1011 13 B12 1100 14 C13 1101 15 D14 1110 16 E15 1111 17 F

Page 52: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Conversion between number systems

Binary to Decimal

Decimal to Binary

Binary to Octal

Octal to Binary

Binary to Hexadecimal

Hexadecimal to Binary

Page 53: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Binary to Decimal Conversion Solution:Step 1: multiply each binary digit by its

corresponding position value.Step 2: add all multiplication results

together to get the decimal number.

6 5 4 3 2 1 026

0 1 0 1 1 0 1

25 24 23 22 21 20

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

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

Binary Number

Result

Start from 0

Position ValuePosition

Page 54: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 55: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Example: Convert the decimal number 35 to binary.

351784210

QuotientQuotientDecimal Number

Stop when the quotient

is 0

Stop when the quotient

is 0

Binary Number : 100011

1001 10

RemainderRemainder

Page 56: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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?

Questions:

Page 57: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Binary to Octal ConversionSolution: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

If the leftmost bit pattern does not contain 3 digits, add extra 0s to the

left.

1 7 6 3 4 4

Page 58: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Octal to Binary Conversion

Solution:Transform each octal digit into a 3-bit binary pattern.

1 4 2

001 100 010

5 6 2

101 110 010

Octal Number Octal Number

Binary Number Binary Number

Page 59: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

If the leftmost bit pattern does not contain 3 digits, add extra 0s to the left.

F C E 4

Page 60: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Hexadecimal to Binary

Solution:Transform each hexadecimal digit into a 4-bit binary pattern.

2 4 C

Hexadecimal Number

Binary Number

0010 0100 1100

Page 61: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Page 62: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Integer Representation

Integers are the whole numbers, which include positive integers and negative integers.

The figure below shows different integers.

IntegersIntegers

UnsignedUnsignedUnsignedUnsigned SignedSignedSignedSigned

Sign-and-Magnitude

Sign-and-Magnitude

One’sComplement

One’sComplement

Two’sComplement

Two’sComplement

Page 63: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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 ~ 25516-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.

Page 64: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Unsigned integers in two different computers

Decimal 8-bit Allocation 16-bit Allocation

7

234

258

24,760

1,245,678

0000 0111

1110 1010

Add zeros to the left when needed

0000 0000 0000 0111

0000 0000 1110 1010

overflowoverflow

overflowoverflow

overflowoverflow overflowoverflow

0000 0001 0000 0010

0110 0000 1011 1000

Exceeds the capacity of the storage

Page 65: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 66: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Note: Note:

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.

Page 67: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

-258

1 1 1

0 0 0 0

0

1 0 0 0 0 0 0 1 0

0 0 0 0 0 0 1 0 0 0 0 0 0 1 01

0 0 0 0 0 0

1 0 0 0 0 0 0 1 0

Question: how about +7 in a 16-bit allocation, and -258

in an 8-bit allocation?

Page 68: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 69: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Note: Note:

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.

Page 70: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

1 1 1

1 1 1

0 0 0 00

-7

1 1 1

1 1 1 1 0 0 0

1 1 1

0 0 0 0

1

0

-258

1 0 0 0 0 0 0 1 0

1 1 1 1 1 1 0 1 1 1 1 1 1 0 11

0 0 0 0 0 0

1 0 0 0 0 0 0 1 0

0

Page 71: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Note: Note:

Page 72: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.Step 3: If the sign is positive, no action is

needed. If negative, leave all the rightmost 0s and the first 1 unchanged. Complement the rest of the bits.

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

Page 73: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

1 1 1

0 0 0 00

-40

0 0 0 1 0 1 0 0 0

1 1 1 1 1 1 1 1 1 0 1 1 0 0 01

0 0 0 0 0 0

1 0 1 0 0 0

0

For negative numbers:

Two’s complement =one’s complement + 1

Note: Note:

Page 74: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

In two’s complement representation, the leftmost bitthe 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: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.

Note: Note:

Page 75: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Summary of integer representationContents of Contents of

MemoryMemory------------

0000000100100011010001010110011110001001101010111100110111101111

UnsignedUnsigned

------------------------00112233445566778899

101011111212131314141515

Sign-and-Sign-and-MagnitudeMagnitude

One’sOne’sComplementComplement

Two’sTwo’sComplementComplement

Page 76: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 77: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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

Page 78: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Floating-point Representation

1 4 . 2 3 4Integer PartFraction Part

1 4 2 3 4

See Decimal to binary Conversion

Repetitive Multiplication Method

Page 79: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Changing fractions to binary

0.125 0.250 0.500 1.000 0.000

0 0 1

Stop when the result is 0

0 .

Multiply 2 to gain integer part

Multiply by 2 to get integer part

Transform the fraction 0.4 to a binary of 6 bits?

0.4

0 .

0.8

0

1.6

1

1.2

1

0.4

0

0.8

0

1.6

1

Page 80: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Example: transform the number 49.58 to a binary ( 3 bits after the decimal point)?

2 49 2 24 ---- 1 2 12 ---- 0 2 6 ---- 0 2 3 ---- 0 2 1 ---- 1 0 ---- 1

0.58 2 1 .16 2 0 .32 2 0 .64

Integer Part

Fraction Part

( 49 . 58 )10 = ( . )2110001 100

Page 81: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Normalization: the moving of the decimal point so that there is only one ‘1’ to the left of the decimal point (1. XXXXXXXXX) .

Floating-point Representation

+ 1010001 . 11001

- 0 . 001110011

6 digits+ 26×1.01000111001

- 2-3×1.110011

± 2e × 1.XXXXXXX

Sign(1 bit)

Exponent(Excess

representation)

3 digits

Mantissa(unsigned integer)

Page 82: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Floating-point Representation

IEEE Standard:

1 8 23

1 11 52

Single-precision format4 bytes = 32 bits

Double-precision format 8 bytes = 64 bits

exponentexponent mantissamantissa

sign

Page 83: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Floating-point Representation

IEEE Standard:

Page 84: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Example: Show the representation of the normalized number: + 26 × 1.01000111001

The sign is positive. The Excess_127 representation of The sign is positive. The Excess_127 representation of the exponent is 133. You add extra 0s on the right the exponent is 133. You add extra 0s on the right to make it 23 bits. The number in memory is stored to make it 23 bits. The number in memory is stored as:as:

00 10000101 01000111001000000000000 10000101 01000111001000000000000

Page 85: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Example: Interpret the following 32-bit floating-point number 1 01111100 11001100000000000000000

The sign is negative. The exponent is –3 (124 – The sign is negative. The exponent is –3 (124 –

127). The number after normalization is127). The number after normalization is -2-2-3-3 × 1.110011 1.110011

Page 86: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

86

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

Page 87: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Part 3

Operations on Bits

Page 88: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Bit Operations

Arithmetic Logical Shift

Page 89: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 90: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Arithmetic Operations

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.

Number of 1s Result Carry

0 0

1 1

2 0 1

3 1 1

Table: adding bits

Page 91: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Example: add the following numbers in two’s complement representation:

(+24) + (-17) ; (+127) + (+3) .

0 0 0 1 1 0 0 0

1 1 1 0 1 1 1 1

(+24) + (-17) =

Carry

0 0 0 0 0 1 1 1

1 1 1 1 1

discard

+7

0 1 1 1 1 1 1 1

0 0 0 0 0 0 1 1

(+127) + (+3) =

1 0 0 0 0 0 1 0

1 1 1 1 1 1 1

Result

+130

(-126)10What’s wrong???

Data overflow !!!

Page 92: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Overflow

Overflow will happen when the value exceeds the range of the allocation.

Range of numbers in two’s complementrepresentation:

- (2N-1) --- 0 --- +(2N-1 –1)

127+3= -126 127+1= -128

1 0 0 0 , 0 0 0 0 1 0 0 0 , 0 0 1 0

(-1) + 0 = -1

1 1 1 1 , 1 1 1 1

Page 93: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

To subtract, negate (two’s complement) the second number and add. For example:

(+101) - (+62) (+101) + (-62)

When you do arithmetic operations on When you do arithmetic operations on numbers in a computer, remember that numbers in a computer, remember that each number and the result should be each number and the result should be in in the rangethe range defined by the bit allocation. defined by the bit allocation.

Note: Note:

Carry 1 1

0 1 1 0 0 1 0 1 ++1 1 0 0 0 0 1 0

----------------------------------Result 0 0 1 0 0 1 1 1 39The leftmost carry is discarded.

Page 94: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Addition and subtraction for floating-point numbers

1. Check the signs. If different, use the sign of the number whose absolute value is larger.

2. Move the decimal point to make the exponents the same.

3. Add or subtract the mantissas (including the integer part and fraction part).

4. Normalize the result before storing in memory.

5. Check for overflow.

Page 95: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Add two floats:0 10000100 101100000000000000000000 10000010 01100000000000000000000

SolutionSolution

The exponents are 5 and 3. The numbers are:+25 × 1.1011 and +23 × 1.011Make the exponents the same.(+25 × 1.1011)+ (+25× 0.01011) +25 × 10.00001After normalization +2+266 × 1.000001 × 1.000001, which is stored as:0 10000101 0000010000000000000000000 10000101 000001000000000000000000

Page 96: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Logical Operations Some essential concepts

Each logical variable can only be True or False.

In the computer system, “1” means “True”, “0” means “False”.

It’s a red tulip !

It’s a cat , not a tiger !

TrueTrue

FalseFalse

Page 97: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

According to the number of operands they take, operators can be categorized as: unary, binary and Ternary.

Some essential concepts

UnaryUnaryInput Output

BinaryBinaryInput

OutputInput

Page 98: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Some essential concepts

Logical Operations

Unary

NOT

Binary

AND OR XOR

Page 99: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Some essential concepts A truth table shows all the possible input

combinations of input with the corresponding output.

x y x OR y

0 0 0

0 1 1

1 0 1

1 1 1

x y x XOR y

0 0 0

0 1 1

1 0 1

1 1 0

x y x AND y

0 0 0

0 1 0

1 0 0

1 1 1

x NOT x

0 1

1 0

Page 100: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

NOT operator

The NOT operator has one input. It inverts bits; that is, it changes 0 to 1 and 1 to 0.

Page 101: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Use the NOT operator on the bit pattern 10011000

SolutionSolution

Target Target 1 0 0 1 1 0 0 01 0 0 1 1 0 0 0 NOTNOT ------------------------------------Result Result 0 1 1 0 0 1 1 10 1 1 0 0 1 1 1

Page 102: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

AND operator

Page 103: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Use the AND operator on bit patterns 10011000 and 00110101.

SolutionSolution

Target Target 1 0 0 1 1 0 0 01 0 0 1 1 0 0 0 ANDAND 0 0 1 1 0 1 0 10 0 1 1 0 1 0 1 ------------------------------------Result Result 0 0 0 1 0 0 0 00 0 0 1 0 0 0 0

Page 104: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Inherent rule of the AND operator

If a bit in one input is 0, you can quickly conclude that the result is 0.

Page 105: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

OR operator

Page 106: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Use the OR operator on bit patterns 10011000 and 00110101

SolutionSolution

Target Target 1 0 0 1 1 0 0 01 0 0 1 1 0 0 0 OROR 0 0 1 1 0 1 0 10 0 1 1 0 1 0 1 ------------------------------------Result Result 1 0 1 1 1 1 0 11 0 1 1 1 1 0 1

Page 107: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Inherent rule of the OR operator

If a bit in one input is 1, you can quickly conclude that the result is 1.

Page 108: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

XOR operator

Page 109: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Use the XOR operator on bit patterns 10011000 and 00110101.

SolutionSolution

Target Target 1 0 0 1 1 0 0 01 0 0 1 1 0 0 0 XORXOR 0 0 1 1 0 1 0 10 0 1 1 0 1 0 1 ------------------------------------Result Result 1 0 1 0 1 1 0 11 0 1 0 1 1 0 1

Page 110: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 111: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 112: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Example of unsetting specific bits

Page 113: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Use a mask to unset (clear) the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110.

SolutionSolution

The mask is The mask is 0000011100000111..

Target Target 1 0 1 0 0 1 1 01 0 1 0 0 1 1 0 ANDANDMaskMask 0 0 0 0 0 1 1 10 0 0 0 0 1 1 1 ------------------------------------Result Result 0 0 0 0 0 1 1 00 0 0 0 0 1 1 0

Page 114: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

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?

Page 115: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Use the mask 1Use the mask 100111111 to AND with the target 111111 to AND with the target pattern. The only 0 bit (bit 7) in the mask turns pattern. The only 0 bit (bit 7) in the mask turns off the seventh bit in the target.off the seventh bit in the target.

Target Target 1 1 0 0 0 1 1 11 1 0 0 0 1 1 1 ANDANDMaskMask 1 1 00 1 1 1 1 1 1 1 1 1 1 1 1 ------------------------------------Result Result 1 1 00 0 0 0 1 1 1 0 0 0 1 1 1

SolutionSolution

Page 116: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Example of setting specific bits

Page 117: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Use a mask to set the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110.

SolutionSolution

The mask is The mask is 1111111111000.000.

Target Target 1 0 1 0 0 1 1 0 1 0 1 0 0 1 1 0 ORORMaskMask 1 1 1 1 11 1 1 1 1 0 0 0 0 0 0 ------------------------------------Result Result 1 1 1 1 1 1 1 1 1 1 1 1 01 1 0

Page 118: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Using the power plant example, how can you use a mask to to show that pump 6 is now turned on?

SolutionSolution

Use the mask Use the mask 0000110000000000..

Target Target 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 ORORMaskMask 0 00 0 11 0 0 0 0 0 0 0 0 0 0 ------------------------------------Result Result 1 0 1 0 11 0 0 1 1 1 0 0 1 1 1

Page 119: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Example of flipping specific bits

Page 120: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Use a mask to flip the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110.

SolutionSolution

Target Target 1 0 1 0 0 1 1 01 0 1 0 0 1 1 0 XORXOR MaskMask 1 1 1 1 11 1 1 1 1 0 0 00 0 0 ------------------------------------Result Result 0 1 0 1 10 1 0 1 1 1 1 0 1 1 0

Page 121: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Shift operations

If a bit pattern can be shifted to the right or to the left.

Page 122: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

SolutionSolution

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.

ExampleExample

Show how you can divide or multiply a number by2 using shift operations.

Page 123: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

ExampleExample

Use the mask 00001000 to AND with the target to keep the fourth bit and clear the rest of the bits.

SolutionSolution

Use a combination of logical and shift operations to find the value (0 or 1) of the fourth bit (from the right).

Page 124: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

Solution (continued)Solution (continued)

Target Target a b c d e f g ha b c d e f g h ANDAND MaskMask 0 0 0 0 10 0 0 0 1 0 0 00 0 0 ------------------------------------Result Result 0 0 0 00 0 0 0 e e 0 0 0 0 0 0

Shift the new pattern three times to the right

0000 0000ee000 000 00000 00000ee00 00 000000 000000ee0 0 0000000 0000000ee

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.

Page 125: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 126: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

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.

Page 127: Chapter 2 Data and Number Representations. Outline Data Representation (chapter2) Data and Information Data Types Digital Data Representation How To Represent

That’s all for this chapter!