Вы находитесь на странице: 1из 61

# LECTURE TWO

INTRODUCTION:
NUMBER SYSTEM AND BINARY
ARITHMETIC

1 11-Jan-18
Introduction
Computer users are often familiar with
computer operations that are non-arithmetic
such as:
 Computer graphics,
 Digital audio, or even
 The manipulation of the computer mouse.
However, regardless of what kind of information
is being manipulated by the computer, the
information must be represented by patterns of
1’s and 0’s (i.e. “on-off” codes).

2 11-Jan-18
Introduction cont..,
This immediately raises the question of
how information should be described or
represented in the machine.
This is the data representation , or data
encoding.
Therefore, graphical images, digital audio,
or mouse clicks must all be encoded in a
systematic, agreed-upon manner.

3 11-Jan-18
Introduction cont..,
In all these cases, there are multiple
ways of encoding a given kind of data.
Some of these ways are useful in one
context while some are useful in another.
In the process of developing a data
representation for computing, a crucial
issue is deciding how much storage be
devoted to each data value.

4 11-Jan-18
Introduction cont..,
For example: a computer architect may decide
to treat integers as being 32 bits in size, and to
implement an ALU that supports arithmetic
operations on those 32-bit values that return 32
bit results.
Some numbers can be too large to represent
using 32 bits, however, and in other cases, the
operands may fit into 32 bits, but the result of a
computation will not, creating an overflow
condition.

5 11-Jan-18
Introduction cont..,
 Therefore, its important to understand
the limits imposed on the accuracy and
range of numeric calculations by the
finite nature of the data representation.

6 11-Jan-18
Positional Numbering Systems
 Radix (or Base): The general idea behind
positional numbering systems is that a numeric
value is represented through increasing powers
of a radix (or base).

7 11-Jan-18
Positional Numbering Systems cont…

8 11-Jan-18
Data Representation in Binary
A bit is the most basic unit of information in a
computer.
 It is a state of “on” or “off” in a digital circuit.
 Sometimes these states are “high” or “low” voltage
instead of “on” or “off..”
A byte is a group of eight bits.
 A byte is the smallest possible addressable unit of
computer storage.
 The term, “addressable,” means that a particular
byte can be retrieved according to its location in
memory.

9 11-Jan-18
Data Representation in Binary cont…
A word is a contiguous group of bytes.
 Words can be any number of bits or bytes.
 Word sizes of 16, 32, or 64 bits are most common.
 In a word-addressable system, a word is the smallest
addressable unit of storage.
A group of four bits is called a nibble (or nybble).
 Bytes, therefore, consist of two nibbles: a “high-order
nibble,” and a “low-order” nibble.
 The nibble containing the least-valued binary digit is
called the low-order nibble, and the other half the high-
order nibble.

10 11-Jan-18
Data Representation in Binary cont…
Bytes store numbers using the position of each
bit to represent a power of 2.
 The binary system is also called the base-2
system.
 Our decimal system is the base-10 system. It
uses powers of 10 for each position in a
number.
 Any integer quantity can be represented
exactly using any base (or radix).
11 11-Jan-18
Converting between bases
There are two important groups of number base
conversions:
1.Conversion of decimal numbers to base-r
numbers
2.Conversion of base-r numbers to decimal
numbers
There are two methods of converting between
bases:
1. Division-remainder method
2. Subtraction method

12 11-Jan-18
Converting Unsigned Whole Numbers
Example 1: Convert 104 to base 3 using the
division-remainder method.
3|104 2
3| 34 1
3| 11 2
3| 3 0
3|1 1
0
 =10212
13 11-Jan-18
Converting between bases cont…
 Example 2:Convert 147 to binary
2|147 1
2| 73 1
2|36 0
2|18 0
2|9 1
2|4 0
2|2 0
2|1 1
0
=10010011

14 11-Jan-18
Converting Fractions
Example 1:

(0.5625)10  (?) 2
• 0.5625  2 = 1.1250 Most significant
• 0.1250  2 = 0.2500
• 0.2500  2 = 0.5000
• 0.5000  2 = 1.0000 Least significant

(0.5625)10  (0.1001) 2
15 11-Jan-18
Converting Fractions cont…
Example 2: Convert 0.34375 to binary with
4 bits to the right of the binary point.
0.34375 × 2 =0.68750 MSB
0.68750 × 2 = 1.37500
1.37500 × 2 = 0.75000
0.75000× 2 =1.50000 LSB
=0.0101

16 11-Jan-18
Subtraction method
 Suppose we want to convert the decimal
number 190 to base 3.
 We know that 3 5 = 243 so our result will be
less than six digits wide.
 The largest power of 3 that we need is
therefore 3 4 = 81, and 81  2 = 162.
 Write down the 2 and subtract 162 from 190,
giving 28.

17 11-Jan-18
Subtraction method cont…
Converting 190 to base 3...
3 1 = 3 is again too large, so we assign a
zero placeholder.
The last power of 3, 3 0 = 1, is our last
choice, and it gives us a difference of
zero.
Our result, reading from top to bottom is:
19010 = 210013
18 11-Jan-18
Subtraction method cont…

19 11-Jan-18
Conversion from a system with base R to
Decimal
To convert a number from a system with base R to decimal we
replace R with the base of the system in the following expression:
n n-1 1 0
(dndn-1…d1d0)R = (dnXR )+ (dn-1XR ) + …+ (d1XR ) +( d0XR )
2 1 0
(214)5 = (2X5 )+(1X5 ) +(4X5 ) = (2X25)+(1X5)+(4X1)=50+5+4=
(59)10
(2021)3
3 2 1 0
=(2X3 )+(0X3 )+(2X3 )+(1X3 )=(2X27)+(2X3)+(1X1)=54+6+1=
(61)10
2 1 0
(13A)12 = (1X12 )+(3X12 )+(10X12 )=144+36+10=(190)10
1 0
(4C)18 = (4X18 )+(12X18 )=72+12=(84)10
(135)4 = Invalid. Digit 5 is greater than 4 which is the base of the
system.

20 11-Jan-18
Converting between Power-of-Two
Radices
Example 4:Convert 110010011101 to octal and
hexadecimal.
 110 010 011 101 = 6235
 Separate into groups of 3 for octal conversion
1100 1001 1101 = C9D16
 Separate into groups of 4 for hexadecimal
conversion

21 11-Jan-18
Converting fractional numbers from binary
to hex and from hex to binary
 The same principle with integer numbers applies: Group four bits
together, padding with zeros if necessary, but this time from left to
right, and convert its 4-bit group to its corresponding Hexadecimal
number and vice versa.
 Examples:

(0.5A4C)16  (0.0101101001001100) 2

## (0.01001) 2  (0.01001000) 2  (0.48)16

(3B.25)16  (00111011.00100101)2

## (110.011) 2  (0110.0110) 2  (6.6)16

22 11-Jan-18
SIGNED INTEGER REPRESENTATION
 There are three ways in which signed
binary numbers may be expressed:
 Signed magnitude,
 One’s complement and
 Two’s complement.

23 11-Jan-18
SIGNED MAGNITUDE
 A signed-magnitude number has a sign as its
left-most bit (also referred to as the high-
order bit or the most significant bit) while
the remaining bits represent the magnitude
(or absolute value) of the numeric value.
 For example, in 8-bit signed magnitude,
 positive 3 is:00000011
 Negative 3 is:10000011
 N’ bits can represent values from -(2N-1 -1) to +(2N-1 -1).

24 11-Jan-18
Signed magnitude cont…
 Computers perform arithmetic operations
on signed magnitude numbers in much
the same way as humans carry out pencil
and paper arithmetic.
 Humans often ignore the signs of the
operands while performing a
calculation, applying the appropriate
sign after the calculation is complete.

25 11-Jan-18
Signed magnitude cont…
 Binary addition is as easy as it gets
 The rules for binary addition:
0+0= 0 0+1= 1
1+0= 1 1 + 1 = 10
 The simplicity of this system makes it
possible for digital circuits to carry out
arithmetic operations.

26 11-Jan-18
Signed magnitude cont…
 Example 1:
 Using signed magnitude binary arithmetic,
find the sum of 75 and 46.
 First, convert 75 and 46 to binary, and arrange
as a sum, but separate the (positive) sign bits
from the magnitude bits.

27 11-Jan-18
Signed magnitude cont…
Example 2:
 Using signed magnitude binary arithmetic, find the
sum of 107 and 46.

We see that the carry from the seventh bit overflows and
is discarded, giving us the erroneous result: 107 + 46 =
25.

28 11-Jan-18
Signed magnitude cont…
 Example 3: Using signed magnitude binary
arithmetic, find the sum of - 46 and - 25.

##  Because the signs are the same, all we do is add

the numbers and supply the negative sign when
we are done.

29 11-Jan-18
Signed magnitude cont…
 Signed magnitude representation is easy for
people to understand, but it requires
complicated computer hardware.
 Another disadvantage of signed magnitude is
that it allows two different representations for
zero: positive zero and negative zero.
 For these reasons computers systems employ
complement systems for numeric value
representation.

30 11-Jan-18
ONE’S COMPLEMENT
 For example, in 8-bit one’s complement,
Positive 3 is: 00000011
Negative 3 is: 11111100
 In one’s complement, as with signed magnitude,
negative values are indicated by a 1 in the high
order bit.
 Complement systems are useful because they
eliminate the need for subtraction.
N-bits can represent values from; -(2N-1-1) to +(2N-
1 -1)

31 11-Jan-18
One’s complement cont…
 With one’s complement addition, the carry bit is
“carried around” and added to the sum.
Example 1: Using one’s complement binary arithmetic,
find the sum of 48 and – 19
 note that 19 in one’s complement is 00010011, so -19
in one’s complement is: 11101100.

32 11-Jan-18
One’s complement cont…
Although the “end carry around” adds
some complexity, one’s complement is
simpler to implement than signed
magnitude.
But it still has the disadvantage of having
two different representations for zero:
positive zero and negative zero.
Two’s complement solves this problem.

33 11-Jan-18
TWO’S COMPLEMENT
To express a value in two’s complement:
If the number is positive, just convert it
to binary and you’re done.
If the number is negative, find the one’s
complement of the number and then add
1.

34 11-Jan-18
Two’s complement cont…
Example:
 In 8-bit one’s complement, positive 3 is:
00000011
 Negative 3 in one’s complement is:
11111100
 Adding 1 gives us -3 in two’s
complement form: 11111101.
 N-bits number can represent values from: -2n-
1 to +(2n-1 -1)

35 11-Jan-18
Two’s complement cont…
 With two’s complement arithmetic, all we do is
add our two binary numbers. Just discard any
carries emitting from the high order bit.
 Example: Using two’s complement binary
arithmetic, find the sum of 48 and - 19.

36 11-Jan-18
Floating-point Representation
 The signed magnitude, one’s complement,
and two’s complement representation that
we have just presented deal with integer
values only.
 Without modification, these formats are not
useful in scientific or business applications
that deal with real number values.
 Floating-point representation solves this
problem.
37 11-Jan-18
Floating-point Representation cont…
Floating-point numbers allow an
arbitrary number of decimal places to the
right of the decimal point.
For example: 0.5  0.25 = 0.125
They are often expressed in scientific
notation.
For example:
0.125 = 1.25  10-1
5,000,000 = 5.0  106

38 11-Jan-18
Floating-point Representation cont…
 Floating-point numbers consist of three parts:
 a sign bit,
 an exponent part (representing the exponent on a
power of 2),
 fractional part called a significand or it also called
mantissa).

39 11-Jan-18
The IEEE Floating-Point Standard
There are essentially two IEEE standard
floating-point formats.
These are the basic and the extended
formats.
 In each of these, IEEE defines two
formats, that is, the single-precision and
the double-precision formats.

40 11-Jan-18
Single Precision standard
The IEEE-754 single precision floating
point standard uses an 8-bit exponent and
a 23-bit significand.

41 11-Jan-18
Double Precision Standard
 The IEEE-754 double precision standard uses an
11-bit exponent and a 52-bit significand.

42 11-Jan-18
 The significand of a floating-point number is
always preceded by an implied binary point.
 Thus, the significand always contains a
fractional binary value.
 The exponent indicates the power of 2 to which
the significand is raised.

43 11-Jan-18
Normalization

44 11-Jan-18
Representation of Floating Point Numbers
 The exponent is a signed number represented using the
bias method with a bias 127
 In radix 2, normalization corresponds to having a
fraction whose first bit (MSB) is 1.
 The fraction represents a number less than 1, but the
significand of the floating point number is 1 plus the
fraction part
 i.e. if ‘e’ is the biased exponent (value of exponent
field) and ‘f’ is the value of the fraction field
 the number is represented as
1.f  2e-127

45 11-Jan-18
Representation of Floating Point Numbers cont…
Example 1: Represent the following number
0.12510 into a single-precision binary number.
 Soln. Changing into binary
0.12510 = 0.0012
the number is +ve, then 0 for sign bit,
 Normalizing = 1.0  2-3: 1.0 is the significand,
and from 1.f  2e-127; e – 127 = -3
then: e = -3+127 = 124 is the characteristic (i. e.
the biased exponent)

46 11-Jan-18
Representation of Floating Point Numbers cont…

## sign exponent mantissa

0 01111100 00000000000000000000000

47 11-Jan-18
Representation of Floating Point Numbers cont…
Example 2: What single-precision number does the
following 32 bit word represent:
1 10000001 010000000000000000000002
Soln.: msb = 1 indicates –ve number
e = 100000012 = 27 + 20 = 129
hence 2e-127 = 2129-127 = 22
Fraction Part: Convert .012 into decimal = [0  21]
+ [1  22]
= 0.2510 making the significand of 1.25
Hence the number is – 1.25  22 = - 5

48 11-Jan-18
Character Codes
 Calculations aren’t useful until their results can
be displayed in a manner that is meaningful to
people.
 We also need to store the results of calculations,
and provide a means for data input.
 Thus, human-understandable characters must
be converted to computer-understandable bit
patterns using some sort of character encoding
scheme.

49 11-Jan-18
Character Codes cont…
 As computers have evolved, character codes
have evolved.
 Larger computer memories and storage devices
permit richer character codes.
 The earliest computer coding systems uses six
bits.
 Binary-coded decimal (BCD) was one of these
early codes. It was used by IBM mainframes in
the 1950s and 1960s.
50 11-Jan-18
Binary-coded decimal(BCD)
Numbers can be represented in the base
10 number system while still using a
binary encoding.
Each base 10 digit occupies four bit
positions, which is known as binary
coded decimal (BCD).
Each BCD digit can take on any of 10
values.

51 11-Jan-18
Binary-coded decimal(BCD) cont…
Digit BCD
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

52 11-Jan-18
Binary-coded decimal(BCD) cont…
In this system, numbers are represented
in a decimal form, however each decimal
digit is encoded using a four bit binary
number.
For example: The decimal number 136
would be represented in BCD as follows:
136 = 0001 0011 0110
1 3 6

53 11-Jan-18
Binary-coded decimal(BCD) cont…
 Conversion of numbers between decimal and
BCD is quite simple.
 To convert from decimal to BCD, simply write
down the four bit binary pattern for each
decimal digit.
 To convert from BCD to decimal, divide the
number into groups of 4 bits and write down
the corresponding decimal digit for each 4 bit
group.

54 11-Jan-18
Binary-coded decimal(BCD) cont…
Example 1: The following is a 16 bit
number encoded in packed BCD format:
0101011010010011
This is converted to a decimal number as
follows:
0101 0110 1001 0011
5 6 9 3
The value is 5693 decimal

55 11-Jan-18
Binary-coded decimal(BCD) cont…
Example 2. Give the BCD equivalent for the
decimal number 69.27.
Solution. The decimal number
6 9 2 7
BCD code is 0110 1001 0010 0111
Hence,
(69.27) = (01101001.00100111)BCD
10

56 11-Jan-18
Binary-coded decimal(BCD) cont…
 There are a couple of variations on the BCD
representation, namely packed and unpacked.
 An unpacked BCD number has only a single
decimal digit stored in each data byte.
 In this case, the decimal digit will be in the low four
bits and the upper 4 bits of the byte will be 0.
 In the packed BCD representation, two decimal
digits are placed in each byte.
 Generally, the high order bits of the data byte
contain the more significant decimal digit.

57 11-Jan-18
Binary-coded decimal(BCD) cont…
An example: The following is a 16 bit
number encoded in packed BCD format:
01010110 10010011
This is converted to a decimal number as
follows:
0101 0110 1001 0011
5 6 9 3
The value is 5693 decimal

58 11-Jan-18
Binary-coded decimal(BCD) cont…
 Another example: The same number in
unpacked BCD (requires 32 bits)
00000101 00000110 00001001 00000011
05 06 09 03

59 11-Jan-18
BCD Addition
 In BCD representation each base 10 digit occupies
four bit positions.
 Therefore addition is performed on a BCD digit by
BCD digit basis (not bit by bit), from right to left, as
we would normally carry it out by hand using a
decimal representation.
 As an example consider adding (+255)10 and
(+63)10 in BCD representation.

60 11-Jan-18
BCD Addition

61 11-Jan-18