binary representation and computer arithmetic

44
Binary Binary Representation and Representation and Computer Computer Arithmetic Arithmetic

Upload: jonah-bentley

Post on 04-Jan-2016

66 views

Category:

Documents


5 download

DESCRIPTION

Binary Representation and Computer Arithmetic. DECIMAL and BINARY system. The Decimal System This uses ten digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) to represent numbers The Binary System uses just two digits, 0 and 1 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Binary Representation and Computer Arithmetic

Binary Binary Representation Representation and Computer and Computer

ArithmeticArithmetic

Page 2: Binary Representation and Computer Arithmetic

DECIMAL and BINARY DECIMAL and BINARY systemsystem

The Decimal SystemThe Decimal System This uses ten digits (0, 1, 2, 3, 4, 5, 6, This uses ten digits (0, 1, 2, 3, 4, 5, 6,

7, 8, 9) to represent numbers 7, 8, 9) to represent numbers

The Binary SystemThe Binary System uses just two digits, 0 and 1 uses just two digits, 0 and 1 easier for a computer to distinguish easier for a computer to distinguish

between two different voltage levels between two different voltage levels (than ten) (than ten)

Page 3: Binary Representation and Computer Arithmetic

POSITIONAL NOTATION (in decimal)

PositionPositional al NameName

thousanthousandsds

hundrehundredsds tenstens unitsunits

PositionPositional al WeigWeightht

101033 101022 101011 101000

ValueValue 1 0001 000 100100 1010 11

   55 22 33 77

5 2375 237 5 x 1 000 + 2 x 100 + 3 x 10 + 7 x 15 x 1 000 + 2 x 100 + 3 x 10 + 7 x 1

Consider the decimal number 5237.

Page 4: Binary Representation and Computer Arithmetic

POSITIONAL NOTATION (in binary)

Positional Positional WeightWeight 2277 2266 2255 2244 2233 2222 2211 2200

ValueValue 121288

6464 3232 1616 88 44 22 11

Number Number  11 11 00 00 00 11 00 11

Positional Positional NotatioNotationn

1100 0101 = 1x128 + 1x64 + 0x32 + 1100 0101 = 1x128 + 1x64 + 0x32 + 0x16 + 0x8 + 1x4 + 0x2 + 1x1 = 0x16 + 0x8 + 1x4 + 0x2 + 1x1 = 197 197 

Consider the byte 1100 0101

Note the LSD in 1100 0101 is the right most digit which is 1 and the MSD is the left most digit which is 1.

Page 5: Binary Representation and Computer Arithmetic

Representing Integers in Representing Integers in BinaryBinary

ASCII codes are pure or true ASCII codes are pure or true binary numbers.  By that we binary numbers.  By that we mean there is no positive or mean there is no positive or negative sign attached to these negative sign attached to these numbers.numbers.

We are now going to look at We are now going to look at representing integers in binary.representing integers in binary.

Page 6: Binary Representation and Computer Arithmetic

Representing Integers in Representing Integers in BinaryBinary

Here are some of the challenges we face when we Here are some of the challenges we face when we move to representing integers in binary:move to representing integers in binary:

1.  Integers can be positive or negative.  1.  Integers can be positive or negative. 

2. Must have a unique representation.2. Must have a unique representation.

   3. Should be able to add and subtract 3. Should be able to add and subtract themthem

  4.  Operations must be efficient ( using 4.  Operations must be efficient ( using the smallest number of circuits possible) the smallest number of circuits possible) 

Page 7: Binary Representation and Computer Arithmetic

INTEGER INTEGER REPRESENTATIONSREPRESENTATIONS

There are 4 commonly known (1 not common) There are 4 commonly known (1 not common) integer representations. All have been used at integer representations. All have been used at

various times for various reasons.various times for various reasons.

1. unsigned 1. unsigned

2. sign magnitude 2. sign magnitude

3. one's complement 3. one's complement

4. two's complement 4. two's complement

5. biased (not commonly known) 5. biased (not commonly known)

Page 8: Binary Representation and Computer Arithmetic

UNSIGNED BINARY UNSIGNED BINARY NUMBER SYSTEMNUMBER SYSTEM

The standard binary encoding The standard binary encoding already given already given

only positive values only positive values range: 0 to 2**n - 1, for n bits range: 0 to 2**n - 1, for n bits

example: example:

4 bits, values 0 to 15 4 bits, values 0 to 15

n=4, 2**4 -1 is 15 n=4, 2**4 -1 is 15

Page 9: Binary Representation and Computer Arithmetic

Sign-Magnitude Binary Sign-Magnitude Binary Number SystemNumber System

The sign and magnitude number The sign and magnitude number system is a simple binary code system is a simple binary code system used to represent positive system used to represent positive and negative integers.and negative integers.

  In this system, the first bit (the In this system, the first bit (the MSB) in a binary representation is MSB) in a binary representation is a sign bit (0 for positive and 1 for a sign bit (0 for positive and 1 for negative)  followed by the  negative)  followed by the  magnitude bits.  magnitude bits.  

Page 10: Binary Representation and Computer Arithmetic

Sign-Magnitude Binary Sign-Magnitude Binary Number SystemNumber System

Example Example 0101 represents the number +5.0101 represents the number +5.

  The most significant bit (MSB) =0.  The 0 The most significant bit (MSB) =0.  The 0 indicates the number is positive.indicates the number is positive.

The remaining 3 bits represent the The remaining 3 bits represent the magnitude of the number.  The decimal magnitude of the number.  The decimal value of binary 101 is 4 + 0 + 1 = 5.value of binary 101 is 4 + 0 + 1 = 5.

Therefore, 0101, using a 4-bit sign-Therefore, 0101, using a 4-bit sign-magnitude convention is the integer +5magnitude convention is the integer +5

Page 11: Binary Representation and Computer Arithmetic

PROBLEMPROBLEM

How many positive binary numbers How many positive binary numbers can be represented in a can be represented in a nibble nibble using the sign-magnitude using the sign-magnitude notation?notation?

How many positive binary numbers How many positive binary numbers can be represented in a can be represented in a bytebyte using the sign-magnitude using the sign-magnitude notation?notation?

Page 12: Binary Representation and Computer Arithmetic

Representation of Negative Representation of Negative Integers using Sign-Magnitude Integers using Sign-Magnitude

NotationNotationExampleExample When an 8-bit sign-magnitude convention When an 8-bit sign-magnitude convention

for representing numbers is used, the for representing numbers is used, the numbernumber1011 0111 represents the negative 1011 0111 represents the negative integer -55.integer -55.

   The first bit, the MSB, is 1 indicating the The first bit, the MSB, is 1 indicating the

integer is negative.integer is negative.   The next 7 bits 011 0111 represent the The next 7 bits 011 0111 represent the

magnitude of the integer.magnitude of the integer.Binary 011 0111 has decimal value  32 + Binary 011 0111 has decimal value  32 + 16 + 4 + 2 + 1 = 55.16 + 4 + 2 + 1 = 55.

Page 13: Binary Representation and Computer Arithmetic

The 1's Complement formThe 1's Complement form

The 1's complement representation in The 1's complement representation in binary of a positive integer is same as binary of a positive integer is same as the sign-magnitude representation of the sign-magnitude representation of that integer.that integer.

ExampleExample

Using a byte, the 1's complement Using a byte, the 1's complement in binary of  +84 isin binary of  +84 is 0101 0100.0101 0100.

Page 14: Binary Representation and Computer Arithmetic

The 1's Complement in Binary The 1's Complement in Binary of a Negative Integerof a Negative Integer

Rule:  Rule: 

The 1's complement in binary of a The 1's complement in binary of a negative integer is obtained by negative integer is obtained by subtractingsubtracting its magnitude  from its magnitude  from 22nn -1 -1 where n is the number of where n is the number of bits used to store the integer in bits used to store the integer in binary.binary.

Page 15: Binary Representation and Computer Arithmetic

BINARY ARITHMETICBINARY ARITHMETIC

Page 16: Binary Representation and Computer Arithmetic

BINARY ADDITIONBINARY ADDITION

Rule Rule 11

Rule Rule 22

Rule Rule 33

Rule Rule 44

Rule Rule 55

0 0

+0+0

0 0

0 0

+1+1

1 1

1 1

+0+0

1 1

1 1

+ 1+ 1

1 0 1 0

1 1

1 1

+1+1

11 11

Page 17: Binary Representation and Computer Arithmetic

BINARY ADDITIONBINARY ADDITION

View online lessonView online lesson

Page 18: Binary Representation and Computer Arithmetic

BINARY ADDITIONBINARY ADDITION

ExampleExample Add these binary numbers:Add these binary numbers:                           1100 (1100 (1212)+)+             +    1101  +    1101  ((1313))                   1 1001    1 1001     ((2525)   )   

Page 19: Binary Representation and Computer Arithmetic

BINARY SUBTRACTIONBINARY SUBTRACTION

Rule 1Rule 1 Rule 2Rule 2 Rule 3Rule 3 Rule 4Rule 4

0 0

- 0- 0

0 0

1 1

- 1- 1

0 0

1 1

- 0- 0

1 1

0 0

- 1- 1

11

Page 20: Binary Representation and Computer Arithmetic

BINARY SUBTRACTIONBINARY SUBTRACTION

View online lessonView online lesson

Page 21: Binary Representation and Computer Arithmetic

BINARY SUBTRACTIONBINARY SUBTRACTION

ExampleExample

Subtract these binary numbers:Subtract these binary numbers:                           1101 1101 ((1313)-)-             +   0110 +   0110  ( ( 66))                   0111    (0111    (77)   )   

Page 22: Binary Representation and Computer Arithmetic

The 1's Complement in Binary The 1's Complement in Binary of a Negative Integerof a Negative Integer

ExampleExampleStore the integer -36 in a byte in 1's Store the integer -36 in a byte in 1's

complement form.complement form.Step 1:    +3610 = 0010 0100Step 1:    +3610 = 0010 010022       [convert the magnitude of the integer to binary][convert the magnitude of the integer to binary] Step 2:     Step 2:     1111 1111     1111 1111                                       - - 0010 01000010 0100                                    1101 10111101 1011

[A byte contains 8 bits. Therefore, subtract 0010 [A byte contains 8 bits. Therefore, subtract 0010 0100 from 20100 from 288-1= 255.  In binary 255 is 1111 -1= 255.  In binary 255 is 1111 1111]1111]

Page 23: Binary Representation and Computer Arithmetic

EASY WAYEASY WAY

Determine the 1's complement of a negative Determine the 1's complement of a negative integerinteger

Determine the 1's complement of Determine the 1's complement of the corresponding positive integer the corresponding positive integer

invert all bits.invert all bits.

Page 24: Binary Representation and Computer Arithmetic

EXAMPLEEXAMPLE

Using a byte, find the 1's complement of -57.Using a byte, find the 1's complement of -57.

+57 stored in a byte in 1's +57 stored in a byte in 1's complement form is  0011 1001.complement form is  0011 1001.

   Invert all bits to determine the Invert all bits to determine the

1's complement of -57 to be 1100 1's complement of -57 to be 1100 0110.0110.

Page 25: Binary Representation and Computer Arithmetic

Two's Complement Two's Complement NotationNotation

The 2's Complement in Binary of a The 2's Complement in Binary of a Positive IntegerPositive Integer

same as sign-magnitude notation same as sign-magnitude notation

Page 26: Binary Representation and Computer Arithmetic

Two's Complement Two's Complement NotationNotation

The 2's Complement of a Negative The 2's Complement of a Negative IntegerInteger

1.1. First, we write the positive value of First, we write the positive value of the number in binary. the number in binary.

2.2. Next, we reverse each bit to get the Next, we reverse each bit to get the 1's complement. 1's complement.

3.3. Last, we add 1 to the number. Last, we add 1 to the number.

Page 27: Binary Representation and Computer Arithmetic

ExampleExample Store -27 in a byte using 2's complement notation.Store -27 in a byte using 2's complement notation.

Step 1:  +27 in binary is 0001 1011.Step 1:  +27 in binary is 0001 1011.   Step 2:  Invert bits to yield 1110 0100.  Then the 1's Step 2:  Invert bits to yield 1110 0100.  Then the 1's

complement of -27 is 1110 0100.complement of -27 is 1110 0100.

Step 3:  Add 1 to the 1's complement:Step 3:  Add 1 to the 1's complement:                                        1110 01001110 0100                                      +           1+           1                                      1110 01011110 0101  The 2's complement of -27 is 1110 0101.The 2's complement of -27 is 1110 0101.

Page 28: Binary Representation and Computer Arithmetic

Binary ArithmeticBinary Arithmetic

Subtraction with one’s complementSubtraction with one’s complement Subtraction with two’s complementSubtraction with two’s complement

Page 29: Binary Representation and Computer Arithmetic

FractionsFractions

Converting a fractional decimal value to binary Converting a fractional decimal value to binary

Page 30: Binary Representation and Computer Arithmetic

Floating Point Arithmetic Floating Point Arithmetic

Floating point arithmetic derives its name Floating point arithmetic derives its name from something that happens when you from something that happens when you use exponential notation. use exponential notation.

Consider the number 123: it can be written Consider the number 123: it can be written using exponential notation as: using exponential notation as:

1.23 * 10 1.23 * 10 22 12.3 * 10 12.3 * 10 11 123 * 10 123 * 10 00 .123 * 10 .123 * 10 33 1230 * 10 1230 * 10 -1-1

Page 31: Binary Representation and Computer Arithmetic

Floating Point ArithmeticFloating Point Arithmetic

All of these representations of the All of these representations of the number 123 are numerically number 123 are numerically equivalent. equivalent.

They differ only in where the They differ only in where the decimal point appears in the decimal point appears in the number.number.

The decimal point "floats" within the The decimal point "floats" within the number as the exponent is changed. number as the exponent is changed. This phenomenon gives floating This phenomenon gives floating point numbers their name. point numbers their name.

Page 32: Binary Representation and Computer Arithmetic

Floating Point ArithmeticFloating Point Arithmetic

The first representation, 1.23 * 10 The first representation, 1.23 * 10 22, , is in a form called "scientific is in a form called "scientific notation“notation“

In scientific notation, the significand In scientific notation, the significand

is always a number greater than or is always a number greater than or equal to 1 and less than 10. equal to 1 and less than 10. (normalization)(normalization)

Page 33: Binary Representation and Computer Arithmetic

Floating Point NumberFloating Point Number

A floating-point number is often stored as A floating-point number is often stored as three parts:three parts:

A significand or mantissa (indicating the A significand or mantissa (indicating the digits that define the number's digits that define the number's magnitude) magnitude)

An exponent or scale (indicating the An exponent or scale (indicating the position of the radix point) position of the radix point)

A sign (indicating whether the number is A sign (indicating whether the number is positive or negative) positive or negative)

Page 34: Binary Representation and Computer Arithmetic

Floating Point ArithmeticFloating Point Arithmetic

The exponent does not always indicate a power of The exponent does not always indicate a power of ten; sometimes powers of sixteen are used ten; sometimes powers of sixteen are used instead, or, most commonly of all, powers of twoinstead, or, most commonly of all, powers of two

The real number The real number -0.125-0.125 is equal to is equal to -1.25 x 10^-1 if powers of ten are used,-1.25 x 10^-1 if powers of ten are used, -2 x 16^-1 if powers of sixteen are used, -2 x 16^-1 if powers of sixteen are used, -1 x 2^-3 if powers of two are used. -1 x 2^-3 if powers of two are used.

The absolute value of the mantissa is, The absolute value of the mantissa is, however, always greater than or equal to 1 however, always greater than or equal to 1 and less than the base of numeration. and less than the base of numeration.

Page 35: Binary Representation and Computer Arithmetic

Floating Point FormatsFloating Point Formats

Over the years, floating point Over the years, floating point formats in computers have not formats in computers have not exactly been standardized. While the exactly been standardized. While the IEEEIEEE (Institute of Electrical and (Institute of Electrical and Electronics Engineers) has Electronics Engineers) has developed standards in this area, developed standards in this area, they have not been universally they have not been universally adopted. adopted.

Page 36: Binary Representation and Computer Arithmetic

In the IEEE single-precision In the IEEE single-precision representation of a real representation of a real

numbernumber One bit is reserved for the sign, and One bit is reserved for the sign, and

it is set to 0 for a positive number it is set to 0 for a positive number and to 1 for a negative one.and to 1 for a negative one.

A representation of the exponent is A representation of the exponent is stored in the next eight bits. stored in the next eight bits.

The remaining twenty-three bits are The remaining twenty-three bits are occupied by a representation of the occupied by a representation of the mantissa of the number. mantissa of the number.

Page 37: Binary Representation and Computer Arithmetic

IEEE 754 Standard (32 IEEE 754 Standard (32 bits)bits)

3131 3030 2929 2288

2727 2626 2255

2424 2233

2222 2121 22 11 00

Sign bit

(1 bit)

Exponent

(8 bits)

Mantissa

(23 bits)

Page 38: Binary Representation and Computer Arithmetic

IEEE 754 Standard (32 IEEE 754 Standard (32 bits)bits)

Sign bit = 0 or 1Sign bit = 0 or 1 The exponent, which is a signed integer in The exponent, which is a signed integer in

the range from -126 to 127, is represented the range from -126 to 127, is represented as a as a biased valuebiased value. (127 is the bias used). (127 is the bias used)

Only the part of the mantissa that comes Only the part of the mantissa that comes after the binary point is actually stored. after the binary point is actually stored. This suppressed digit at the beginning of This suppressed digit at the beginning of the mantissa is called the ``hidden bit.'' the mantissa is called the ``hidden bit.''

Page 39: Binary Representation and Computer Arithmetic

Floating Point ArithmeticFloating Point Arithmetic

In a binary computer, all numbers In a binary computer, all numbers are stored in base 2 instead of base are stored in base 2 instead of base 1010

The absolute value of the mantissa The absolute value of the mantissa is, however, always greater than or is, however, always greater than or equal to 1 and less than 2.equal to 1 and less than 2.

Page 40: Binary Representation and Computer Arithmetic

EXAMPLEEXAMPLE

Find the IEEE single-precision Find the IEEE single-precision representation of representation of 5.755.75. .

The sign bit is 0, since the number is The sign bit is 0, since the number is positive positive

(5)(5)10 10 = (101)= (101)22

To convert 0.75 do repeated To convert 0.75 do repeated multiplicationmultiplication

Page 41: Binary Representation and Computer Arithmetic

EXAMPLEEXAMPLE

.75 X 2 = 1.5 --------.75 X 2 = 1.5 -------- 1 1

.5X2 = 1.0 ----------.5X2 = 1.0 ----------11

0 ------0 ------ (stop) (stop)

(.75)(.75)1010 = (.11) = (.11)22

Page 42: Binary Representation and Computer Arithmetic

EXAMPLEEXAMPLE

(5.75)(5.75)1010 = (101.11)= (101.11)22

Normalizing 101.11 ---Normalizing 101.11 --- 1 1 . . 01110111 X 2 X 222

Hidden bitHidden bit mantissamantissa ExponentExponent

Adding bias to the exponentAdding bias to the exponent

127+2 = 129127+2 = 129 (129)(129)1010 = ( = ( 1000 00011000 0001 ) )22

Page 43: Binary Representation and Computer Arithmetic

And the result….And the result….

Lining up, we getLining up, we get

0 10000001 0 10000001 0111000000000000000000001110000000000000000000

Page 44: Binary Representation and Computer Arithmetic

More ExamplesMore Examples

Convert from Decimal to Convert from Decimal to BinaryBinary