data representation

22
Department of Civil Engineering Chittagong University of Engineering & Technology Sanjoy Das Lecturer CE 218 Computer Programming Sessional Data Representation

Upload: ashraf-miraz

Post on 17-Aug-2015

25 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Data representation

Department of Civil EngineeringChittagong University of Engineering & Technology

Sanjoy DasLecturer

CE 218Computer Programming Sessional

Data Representation

Page 2: Data representation

Data Representation

INTRODUCTION

In our familiar number system we have symbols ( to ) to represent a number.

We call it decimal system or base system.

To process data in a computer, we need to represent them in the registers of the processor.

A register consists of several circuits.

Page 3: Data representation

Data Representation

INTRODUCTION

Can we represent 10 symbols using an electric circuit? Obviously no.

We can symbolize an electric circuit into two states such as-

(a) contains current (1)(b) no current(0)

Similarly for a magnetic media such as a disk we may consider two states-

(a) magnetized clockwise (b) magnetized counterclockwise

Page 4: Data representation

Data Representation

INTRODUCTION

We may think these two states as two symbols (say one is and the other is ) to represent a number.

Thus we have only two digitsto represent a number in the computer processor, in the memory or in the data storage devices.

Each individual circuit represents a digit and we term it as a bit (it is an abbreviation from binary digit).

Thus, at circuit level, we can represent a number in the computer in binary form.

Page 5: Data representation

Data Representation

THE BINARY SYSTEM

Computers work with the binary or base-two system of numbers that uses the two digits and instead of the ten digits of the more familiar decimal or base-ten system.

In the binary system, a number is denoted as-

(1)

Where and are two integer indices.

The binary digits or bits, take the value of or , and the period (.) is the binary point.

Page 6: Data representation

Data Representation

THE BINARY SYSTEM

The implied value is equal to-

(2)

Where and are two integer indices.

The decimal digits, take values in the range , and the period (.) is the decimal point.

In the decimal system, the same number is expressed as-

(3)

Page 7: Data representation

Data Representation

THE BINARY SYSTEM

The implied value is equal to-

(4)

Which is identical to that computed from the base-two expansion.

Since bits can be represented by the on-o ff positions of electrical switches that are built in the computer’s electrical circuitry, and since bits can be transmitted by positive or negative voltage as a Morse code, the binary system is ideal for developing a computer architecture.

Page 8: Data representation

Data Representation

THE BINARY SYSTEMConversion from Decimal to Binary The conversion from a decimal number to a binary

number can be explained by the following example-

(πŸ“πŸπŸŽπŸ– )𝟏𝟎=(𝟏𝟎𝟏𝟎𝟎𝟎𝟏𝟎𝟏𝟏𝟎𝟎𝟎 )𝟐

πŸπŸ“πŸπŸŽπŸ–πŸπŸπŸ”πŸŽπŸ’βˆ’πŸŽπŸπŸπŸ‘πŸŽπŸβˆ’πŸŽπŸπŸ”πŸ“πŸβˆ’πŸŽπŸπŸ‘πŸπŸ“βˆ’πŸπŸπŸπŸ”πŸβˆ’πŸπŸπŸ–πŸβˆ’πŸŽπŸπŸ’πŸŽβˆ’πŸπŸπŸπŸŽβˆ’πŸŽπŸπŸπŸŽβˆ’πŸŽπŸπŸ“βˆ’πŸŽπŸπŸβˆ’πŸπŸβˆ’πŸŽ

Page 9: Data representation

Data Representation

THE BINARY SYSTEMConversion from Decimal to Binary The conversion from a decimal number (non - integer) to

a binary number can be explained by the following example-

(πŸ‘πŸ’πŸ“ .28125   )𝟏𝟎=(𝟏𝟎𝟏𝟎𝟏𝟏𝟎𝟎𝟏 .𝟎𝟏𝟎𝟎𝟏 )𝟐

πŸπŸ‘πŸ’πŸ“πŸπŸπŸ•πŸβˆ’πŸπŸπŸ–πŸ”βˆ’πŸŽπŸπŸ’πŸ‘βˆ’πŸŽπŸπŸπŸβˆ’πŸπŸπŸπŸŽβˆ’πŸπŸπŸ“βˆ’πŸŽπŸπŸβˆ’πŸ

πŸβˆ’πŸŽ

𝟎 .πŸπŸ–πŸπŸπŸ“Γ—πŸ=𝟎 .πŸ“πŸ”πŸπŸ“πŸŽ .πŸ“πŸ”πŸπŸ“πŸŽΓ—πŸ=𝟏 .πŸπŸπŸ“πŸŽπŸŽ .πŸπŸπŸ“πŸŽπŸŽΓ—πŸ=𝟎 .πŸπŸ“πŸŽπŸŽπŸŽ .πŸπŸ“πŸŽπŸŽπŸŽΓ—πŸ=𝟎 .πŸ“πŸŽπŸŽπŸŽπŸŽ .πŸ“πŸŽπŸŽπŸŽπŸŽΓ—πŸ=𝟏 .𝟎𝟎𝟎𝟎

Page 10: Data representation

Data Representation

THE BINARY SYSTEMConversion from Binary to Decimal The conversion from a binary number to a decimal

number can be explained by the following example-

(𝟏𝟎𝟏𝟎𝟎𝟎𝟏𝟎𝟏𝟏𝟎𝟎𝟎 )𝟐=(πŸ“πŸπŸŽπŸ– )𝟏𝟎

πŸŽΓ—πŸπŸŽ=πŸŽπŸŽΓ—πŸπŸ=πŸŽπŸŽΓ—πŸπŸ=πŸŽπŸΓ—πŸπŸ‘=πŸ–πŸΓ—πŸπŸ’=πŸπŸ”πŸŽΓ—πŸπŸ“=πŸŽπŸΓ—πŸπŸ”=πŸ”πŸ’πŸŽΓ—πŸπŸ•=πŸŽπŸŽΓ—πŸπŸ–=πŸŽπŸŽΓ—πŸπŸ—=πŸŽπŸΓ—πŸπŸπŸŽ=πŸπŸŽπŸπŸ’πŸŽΓ—πŸπŸπŸ=πŸŽπŸΓ—πŸπŸπŸ=πŸ’πŸŽπŸ—πŸ”

πŸ“πŸπŸŽπŸ–

Page 11: Data representation

Data Representation

THE BINARY SYSTEMConversion from Binary to Decimal The conversion from a binary number (non – integer) to a

decimal number can be explained by the following example-

(𝟏𝟎𝟏𝟎𝟏𝟏𝟎𝟎𝟏 .𝟎𝟏𝟎𝟎𝟏 )𝟐= (πŸ‘πŸ’πŸ“ .28125   )𝟏𝟎

πŸΓ—πŸπŸŽ=πŸπŸŽΓ—πŸπŸ=πŸŽπŸŽΓ—πŸπŸ=πŸŽπŸΓ—πŸπŸ‘=πŸ–πŸΓ—πŸπŸ’=πŸπŸ”πŸŽΓ—πŸπŸ“=πŸŽπŸΓ—πŸπŸ”=πŸ”πŸ’πŸŽΓ—πŸπŸ•=πŸŽπŸΓ—πŸπŸ–=πŸπŸ“πŸ”

πŸŽΓ—πŸβˆ’πŸ=πŸŽπŸΓ—πŸβˆ’πŸ=𝟎 .πŸπŸ“πŸŽΓ—πŸβˆ’πŸ‘=πŸŽπŸŽΓ—πŸβˆ’πŸ’=𝟎

πŸΓ—πŸβˆ’πŸ“=𝟎 .πŸŽπŸ‘πŸπŸπŸ“

πŸ‘πŸ’πŸ“

𝟎 .πŸπŸ–πŸπŸπŸ“

Page 12: Data representation

Data Representation

THE BINARY SYSTEMThe Largest Integer Encoded by Bits

Each individual circuit represents a digit and we term it as a bit (it is an abbreviation from binary digit).

As you all know, the largest number for a given number of digits can be obtained by filling out each position with the largest symbol.

Such as : in decimal, largest number with 3 digits = 999

similarly, in binary, largest number with 3 digits = 111

Page 13: Data representation

Data Representation

THE BINARY SYSTEMThe Largest Integer Encoded by Bits

So, the largest integer that can be represented with bits is-

(5)

Where the ones are repeated times. The decimal - number equivalent is-

(6)

Page 14: Data representation

Data Representation

THE BINARY SYSTEMThe Largest Integer Encoded by Bits

To demonstrate this equivalence, we recall from our college years-

Where and are two variables and set and .

(7)

Page 15: Data representation

Data Representation

THE BINARY SYSTEMThe Largest Integer Encoded by Bits When one bit is available, we can describe only the

integers and , and the largest integer is .

With two bits the maximum is . With three bits the maximum is . With eight bits the maximum is . With thirty-onebits the maximum is

.

Page 16: Data representation

Data Representation

THE BINARY SYSTEMSigned Integers To encode a signed integer, we allocate the first bit to

the sign. If the leading bit is , the integer is positive; if the leading

bit is , the integer is negative. The largest signed integer that can be represented with

bits is then-

According to this convention, the integer is stored as the binary string .

Page 17: Data representation

Data Representation

THE BINARY SYSTEMSigned IntegersData Representation Scheme for Integers

………………………………………………………..πŸŽπŸπŸπŸ‘ πŸ‘πŸ

Sign Bit

………………………………………………………..

1 1 0 1

πŸŽπŸπŸπŸ‘ πŸ‘πŸ

Sign Bitβˆ’πŸ“=βˆ’ (𝟏𝟎𝟏 )𝟐

is stored as the binary string

Page 18: Data representation

Data Representation

THE BINARY SYSTEMSigned IntegersData Representation Scheme for Non-Integers (Floating Point Numbers) The floating-point representation allows us to store real

numbers (non-integers) with a broad range of magnitudes, and carry out mathematical operations between numbers with disparate magnitudes.

Page 19: Data representation

Data Representation

THE BINARY SYSTEMSigned IntegersData Representation Scheme for Non-Integers (Floating Point Numbers) Consider the binary number-

To develop the floating-point representation, we recast this number into the product-

Note that the binary point has been shifted to the left by nine places, and the resulting number has been multiplied by the binary equivalent of .

The binary string is the mantissa or significand, and is the exponent.

Page 20: Data representation

Data Representation

THE BINARY SYSTEMSigned IntegersData Representation Scheme for Non-Integers (Floating Point Numbers) To develop the floating-point representation of an

arbitrary number, we express it in the form-

Where is a real number called the mantissa or significand, and is the integer exponent.

This representation requires one bit for the sign, a set of bytes for the exponent, and another set of bytes for the mantissa.

(9)

Page 21: Data representation

Data Representation

THE BINARY SYSTEMSigned IntegersData Representation Scheme for Non-Integers (Floating Point Numbers) In memory, the bits are arranged sequentially in the

following order-

The exponent determines the shift of the binary point in the binary representation of the mantissa.

………………………………………………………..πŸŽπŸπŸπŸ‘ πŸ‘πŸ

Sign Bit

Exponent Mantissa

Page 22: Data representation

β€’ For a 32-bit float type, the mantissa is stored in a 23-bit segment and the exponent in an 8-bit segment, leaving 1 bit for the sign of the number. For a 64-bit double type, the mantissa is stored in a 52-bit segment and the exponent in an 11-bit segment.

Data Representation

THE BINARY SYSTEM