Docsity

Two’s Complement Arithmetic - Computer Architecture - Notes | CPSC 5155G, Study notes of Computer Architecture and Organization

Toggle sidebar
bg1
Two’s Complement Arithmetic
We now address the issue of representing integers as binary strings in a computer.
There are four formats that have been used in the past; only one is of interest to us.
The four formats are
1. Sign–Magnitude
2. One’s–Complement
3. Two’s–Complement
4. Excess–N, where N is some positive integer; say 127 for Excess–127.
Sign–magnitude representation is obsolete.
Excess–127 representation is only used as part of floating point representations.
If discussed at all, it will be in the context of the IEEE–754 standard.
We focus on Two’s–complement, but discuss one’s–complement arithmetic as
a mechanism for generating the two’s complement.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Two’s Complement Arithmetic - Computer Architecture - Notes | CPSC 5155G and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Two’s Complement Arithmetic

We now address the issue of representing integers as binary strings in a computer. There are four formats that have been used in the past; only one is of interest to us. The four formats are

  1. Sign–Magnitude
  2. One’s–Complement
  3. Two’s–Complement
  4. Excess–N, where N is some positive integer; say 127 for Excess–127. Sign–magnitude representation is obsolete. Excess–127 representation is only used as part of floating point representations. If discussed at all, it will be in the context of the IEEE–754 standard. We focus on Two’s–complement, but discuss one’s–complement arithmetic as a mechanism for generating the two’s complement.

Representation of Non–Negative Integers

In each of one’s–complement and two’s–complement arithmetic, no special steps are required to represent a non–negative integer. All conversions to the complement number systems begin with conversion to simple binary representation, often from decimal notation. The only issue with non–negative integers is to insure that the number is not too large for the number of bits used in the representation. There are two ways to define this size limitation in the complement numbers system.

  1. The most significant bit (leftmost) must be a “0” for non–negative numbers.
  2. For an N–bit representation, the number must be less than 2 N– . The biggest signed 8–bit integer is 2 7
  • 1 = 127. The biggest signed 16–bit integer is 2 15
  • 1 = 32767. Three 8–bit examples: 100 = 0110 0100 22 = 0001 0110 0 = 0000 0000

Taking the One’s–Complement

To take the one’s complement of a binary integer, just Change every 0 to a 1 Change every 1 to a 0 One’s–complement arithmetic is that arithmetic that uses the one’s–complement of a binary integer to represent its negative. + 100 in 8–bit one’s complement. 0110 0100.

- 100 in 8–bit one’s complement. Convert 100 to 8–bit binary 0110 0100 Take the one’s complement 1001 1011 - 100 is represented as 1001 1011 + 22 is 8–bit one’s complement 0001 0110 - 22 in 8–bit one’s complement Convert 22 to 8–bit binary 0001 0110 Take the one’s complement 1110 1001 - 22 is represented as 1110 1001 We no longer use one’s–complement arithmetic. It has two serious problems.

Problems with One’s–Complement Arithmetic

There are two serious problems with the use of one’s–complement arithmetic. The first one will be described but not explained. Problem 1: It is a lot trickier to build a binary adder for one’s–complement numbers. Just take my word for this one. Problem 2: Negative Zero Consider 8–bit one’s–complement arithmetic

  • 0 is 0000 0000 Take the one’s complement 1111 1111 So – 0 is represented by 1111 1111 Most of us are uneasy about having a distinct – 0 in our number system. Code such as the following is considered strange. if ( ( x == 0) || (x == –0) ) This course will not cover one’s–complement arithmetic or use it directly for any purpose other than taking the two’s complement, discussed next.

Consider 5 + 7 5 = 0101 7 = 0111 1100 12

Taking the Two’s Complement (Part 2)

The recipe for taking the two’s–complement of a binary number is simple.

  1. Take the one’s–complement of the number
  2. Add 1 to that complement. + 100 in 8–bit two’s–complement binary 0110 0100
  • 100 in 8–bit two’s–complement binary Represent +100 as an 8–bit number 0110 0100 Take the one’s complement 1001 1011 Add 1 1
  • 100 is represented as 1001 1100 + 22 in 8–bit two’s–complement binary 0001 0110
  • 22 in 8–bit two’s–complement binary Represent +22 as an 8–bit number 0001 0110
  • Take the one’s complement
  • Add
  • – 22 is represented as

Half Adders and Full Adders

A half adder is an adder with no “carry in”. A full adder is an adder that allows

a “carry in”. For adding two binary numbers, the “carry in” is either 0 or 1.

In decimal arithmetic on integers,

a half adder can be used for adding the units column

a full adder would be used for adding the tens, hundreds, thousands (etc.)

columns as each of these can have a “carry in” from a column to the right.

The terms “half adder” and “full adder” apply only to binary arithmetic.

A full adder can be converted to a half adder by setting its “carry in” to 0.

Example From Unsigned Integer Arithmetic

Here we add 14 (binary 01110) and 11 (binary 01011) to get

25 (binary 11001).

Double Negations (Part 1)

Remember that the range of 8–bit two’s–complement arithmetic is – 128 to 127. + 100 in 8–bit two’s–complement binary 0110 0100

  • 100 in 8–bit two’s–complement binary Represent +100 as an 8–bit number 0110 0100 Take the one’s complement 1001 1011 Add 1 1
  • 100 is represented as 1001 1100 +100 in 8–bit two’s–complement binary Represent – 100 as an 8–bit number 1001 1100 Take the one’s complement 0110 0011 Add 1 1
  • 100 is represented as 0110 0100 This is the same as + 100. So – ( – 100) is + 100.

Double Negations (Part 2)

Remember that the range of 8–bit two’s–complement arithmetic is – 128 to 127. + 22 in 8–bit two’s–complement binary 0001 0110

  • 22 in 8–bit two’s–complement binary Represent +22 as an 8–bit number 0001 0110 Take the one’s complement 1110 1001 Add 1 1
  • 22 is represented as 1110 1010 +22 in 8–bit two’s–complement binary Represent – 22 as an 8–bit number 1110 1010 Take the one’s complement 0001 0101 Add 1 1
  • 22 is represented as 0001 0110 This is the same as + 22. So – ( – 22) is + 22.

Standard Ranges

Here is the standard Java implementation of Two’s–Complement arithmetic.

In general the range for N–bit two’s–complement arithmetic is

N–

to 2

N–

Java Type Number

of Bits

Lower

Limit

Upper

Limit

byte 8 – 128 127

short 16 – 32768 32767

int 32 – 2 147 483 648 2 147 483 647

long 64 – 2

63

63

Log 10 (2) is about 0.30103. Log 10 (3) is about 0.47712.

Log 10 (

63

) is about 0.30103  63 = 18.9649.

Log 10 (9) = Log 10 (

2

) = 2Log 10 (3) = 20.47712 = 0.95424.

As 0.9649 > Log 10 (9), we conclude that 2

63

is bigger than 9 10

18

Standard Values in N–Bit Two’s Complement Arithmetic

(With Examples for 16–bit integers)

0 is represented by N zeroes 0000 0000 0000 0000

–1 is represented by N ones 1111 1111 1111 1111

N–

is represented by a 1

followed by (N – 1) zeroes 1000 0000 0000 0000

The student should memorize these patterns and not bother with

standard conversion techniques when obtaining them.

Technically speaking, the representation of – 2

N–

cannot be derived by

standard means, as 2

N–

cannot be represented.

Overflow: “Busting the Arithmetic”

The range of 16–bit two’s–complement arithmetic is

– 32,768 to 32,

Consider the following addition problem: 24576 + 24576.

Now + 24,576 (binary 0110 0000 0000 0000) is well within the range.

What happened?

We had a carry into the sign bit. This is “overflow”. The binary representation

being used cannot handle the result.

NOTE: This works as unsigned arithmetic.

When we design a binary adder for N–bit integers (N > 1), we shall address

the standard method for detecting overflow.

Two’s Complement vs. Taking the Two’s Complement

There is a difference between the idea of a complement system and the process of taking the complement. Two’s–complement arithmetic is a system of representing integers in which the two’s- complement is used to compute the negative of an integer. For positive integers, the method of conversion to binary differs from unsigned integers only in the requirement to use a fixed number of bits; this may give leading zeroes. Question: What is the representation of the positive number 123 in 8–bit two’s complement arithmetic? Answer: 0111 1011. Note that I did not take the two’s complement of anything to get this. Question: What is the representation of the negative number –123 in 8–bit two’s complement arithmetic? Answer: I take the two’s complement of 0111 1011 to get 1000 0101. Summary: Speaking of the two’s-complement does not mean that one must take the two’s-complement of anything.