One's and Twos

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Ones and Twos

Compliments

Horners Method
Suppose we wish to compute the value of the hexadecimal
number 2CD5. we can multiply each digit by its weight,
and add up all the products.
2 x 16 + 12 x 16 + 13 x 16 + 5
A straightforward, but inefficient, way to evaluate the
above expression is to:
1.

Compute 16 (two multiplications) and multiply by 2

2.

Compute 16 (one multiplication) and multiply by 12.

3.

Multiply 13 by 16

4.

Add the preceding three products along with 5, the least


significant digit.

Horners Method

A more efficient approach is to use Horners method.

1.

We start with the most significant digit, multiply by 16 and then add the
next digit.

2.

We then multiply this result by 16 and add the next digit.

3.

We continue in this fashion until we reach the point where we add the least
significant digit.

Ex. Evaluate 2CD5


2
x
+

16

multiply MSD (2) by 16

32

product

12

Add next digit (C)

44
x

16

multiply result by 16

704
+

13

Add next digit (D)

717
x

16

multiply result by 16

11472
+

5
11477

Add LSD (5)


final result

Our final result is 1477 decimal equivalent to 2CD5 in hex. With


horners method converting 2DC5 hex only requires three
( instead of six) multiplications and three additions.
((2 x 16 + 12) x 16 + 13) x 16 + 5
2 multiplied by 16 three times
In this form, it is easy to see that the digit 2 is effectively
multiplied by 16 three times; that is, it is given the weight 16.
similarly the digits C (12) and D (13) are multiplied by 16 twice
and once respectively. Thus the final result is the sum of the
digits, properly weighted.

Signed Binary Numbers


Signed

binary numbers are binary


numbers that use a representation that
can accommodate both positive and
negative numbers.
Unsigned binary numbers, on the other
hand, are binary numbers in which all
bits are used to hold the magnitudes of
the numbers. Thus, unsigned numbers
can represent positive numbers only.

4 types of signed numbers


Sign-magnitude
2. Twos complements
3. Ones complements
4. Excess-n
1.

Signed Magnitude Representation

In mathematics, we use a minus sign to


designate a negative number. We can do
essentially the same thing within a
computer.
Numbers are stored inside a computer
in binary in fixed-length sizes.
We can use the leftmost bit of a binary
to represent its sign: 0 for + and 1
for -.

Signed Magnitude Representation


Ex.

Numbers represented in a computer using 16 bits,


we can use the leftmost bit for the sign and the
remaining 15 bits for the magnitude of the number. With
this scheme, -5 and + 5 become
sign bit
1000000000000101 = -5
magnitude

sign bit
0000000000000101 = +5
magnitude

The number representation is called sign-magnitude


representation. The leftmost bit is the sign bit; remaining
bits to the right hold the magnitude of the number.

The

principal short coming of signed-magnitude


representation is that it requires the computer
to analyze the signs of the numbers when it
performs a computation.
ex. Suppose +5 and -5 are to be added. The computer must first
examine the sign bits of the two magnitudes in order to add the two
numbers. If, on the other hand, the sign bits were the same, then the
computer must add the magnitudes.

The

particular computation that the computer


has to perform depends on the signs of the
numbers.

Twos Complement Representation


Before

discussing twos complement representation, lets


make an observation that we will use later.

Suppose a binary number contains 16 bits that are all


equal to 1. if we add 1 to this number and discard the carry
out of the leftmost column, we get a zero result;
Carry discarded

1111111111111111 16 bit number containing all 1s


+

1 add 1

0000000000000000 get all zeros

Twos

complement representation is
another representation that can
accommodate both positive and
negative numbers.
The advantage of twos complement
representation is that unlike signmagnitude representation, it does not
require a computer to perform sign
analysis when adding or subtracting.

To

understand this representation,


consider what -5 means mathematically:
-5 is the number that when added to +5
results in zero.
lets write +5 in binary and see which
number produces zero when added to it.
This number the binary number that yields
zero when added to the binary for +5 is
called twos complement of +5.

For

our first guess, lets take +5 in


binary and flip all its bits. We then add
the result to +5 and check if the result is
zero.
0000000000000101 = +5
+ 1111111111111010 = +5 flipped
1111111111111111 =should be zero

Clearly,

the flipped version of +5 is not -5


because it does not yield zero when
added to -5. However, recall our earlier
observation that adding 1 to a binary
number of all 1s results in zero. Because
the flipped version of +5 produces all 1s
when added to +5, the flipped value plus
one should produce zero.

1111111111111010 = +5 flipped
+
1
1111111111111011 = +5 flipped + 1

Now

lets see if this new value, +5


flipped + , yields zero when added to +5

carry

0000000000000101 = +5
+ 1111111111111011 = +5 flipped + 1
0000000000000000

Indeed,

it does yield zero, assuming the carry out of the


leftmost column is not included in the result. But this is
precisely what happens in a computer because numbers
are held in fixed-length areas.

Thus, when +5 flipped +1 is added to +5 in a


computer, the result is zero.
More

important, we have determined the procedure for


computing the twos complement of any binary number:
simply flip its bits and add one.

When

we flip the bits of a binary number and add one,


we say that we are taking the twos complement of the
number, or, more simply, that we are complementing the
number

Beware that we will use the term complement in


three different ways when dealing with binary
numbers:
1.To complement a bit means to flip the bit.
2.To complement a number means to take its
twos complement (flip its bits plus one)
3.To bitwise complement a number means to flip
each of its bits but not add one.
the bitwise complement of a number is sometimes called the ones
complement of the number

Thus

taking the twos complement of a binary number


is, in effect, negating the number.
In twos complement system, the leftmost bit of a
number indicates its sign.
A

1 bit indicates negative number


(1111111111111010 = -5)

0 bit indicates positive number


(0000000000000101 = +5)

In

twos complement system, we refer to positive


numbers as true form and negative numbers in
complement form.

Although

the left most bit is the sign in both


the sign-magnitude and the twos complement
systems, the two systems are quite different.
Ex.

1111111111111111 = -32,767 in sign-magnitude


1111111111111111 = -1 in twos complement

Furthermore,

in the sign-magnitude system,


sign analysis is not necessary when adding (or
Subtracting) in the twos complement system,
but in the twos complement system sign
analysis it is not necessary.

That is regardless of the signs of the two numbers, to


compute their sum (or difference), we simply add (or
subtract) the two numbers. If the value of the result is
positive, it will appear in true form; if the result is
negative, it will appear in complement form.
Lets add +5 and -1
0000000000000101 = +5
+ 1111111111111111 = -1
0000000000000100 = +4
The result is +4 in true form.

Now, Lets try Add +2 and -5


0000000000000010 = +2
+ 1111111111111011 = -5
1111111111111101 = -3
The result is -3 in complement form.

Computer

circuits designed for twos


complement system are cheaper and less
complex than circuits designed for the signmagnitude system because the former does
not require sign analysis.
It is for this reason that most computer
systems today use twos complement
system.

Ones complement system


The

ones complement of a number is its bitwise complement.

That

is the number that is obtained by flipping all the bits in


the original number.

For

example, the ones complement of

0000000011111110 is
1111111100000001
If we add a number and its ones complement, we get a sum in
which all the bits are one.

0000000011111110
+ 1111111100000001
1111111111111111
Adding a number with its ones complement does not
yield zero. Thus, we conclude that the ones
complement of a number is not the negative of that
number.

But suppose we let the value zero be represented by two bit patterns: all zeros
and all ones. Then adding a number wit its ones complement would, indeed
yield zero.

If we interpret both 0000 and 1111 as zero, we can use ones


complement numbers in very much the same way that we use twos complement
numbers, but with one additional twist:

For addition, we must add the end-around carry to the LSB of the sum
ex. Add -1 with -1
carry out

1111111111111110 = -1 in ones complement


+ 1111111111111110 = -1 in ones complement
1111111111111100
+

intermediate sum
end-around carry

1111111111111101 = -2 final sum

When we add the


end-around carry to
the intermediate
sum, we then get
the correct sum, -2

Ones

complement representation has the advantage that the


negation of a number is simple and fast.

Negation

in twos complement representation, on the other hand,


takes considerably longer because it requires addition operation.

Ones

complement representation, however has two disadvantages.

First,

there are two representations of zero.

If

we input these two distinct zeros to a comparison circuit, it would


incorrectly flag them as unequal because they have different bit patterns.
Thus to avoid this erroneous result, a comparison circuit would have to
treat comparisons with zero as special cases.

Second,

ones complement addition involves the added complexity


of processing the end-around carry. Processing this carry is
particularly bothersome, and is the principal reason why ones
complement representation is rarely used in modern computers.

Excess-n representation
In

excess-n representation, n is added to the value


of a number to get its representation.

Thus,

an excess-n number is in excess of (i.e


more than) the number it represents by n.
Example: the excess-4 representation of the value
2 is 2 + 4 = 6 decimal

To obtain

the value, that an excess-n number


represents, we subtract n.
Example: the value of excess-4 number
6 is 6 4 = 2.

An

important property of excess-n


numbers is that they end up in
Value
ascending order if they are sorted
-4
as unsigned integers. Twos
-3
complements numbers do not have -2
-1
this property.
0
Ex. The order of excess-4 numbers 1
2
would be unchabged by an unsigned 3
sort, but twos complement numbers
would end up in 0,123,-4 -3 -2 -1

Excess-4

Twos
complement

000

100

001

101

010

110

011

111

100

000

101

001

110

010

111

011

You might also like