You are on page 1of 27

Unsigned Integers

Computers process binary patterns, i.e. of patterns of 0s and 1s. To represent data
1
within a
computer we need to code it as a binary pattern.
The representation of integers
2
and characters are the most important to consider because more
complicated data representations can be built from them, e.g. comple numbers, dates, times,
addresses, sound, !ideo, typefaces.
Computer architects ha!e employed many different schemes for representing numerical !alues, and
there are ad!antages and disad!antages to each representation. "efore considering these, we need to
remind oursel!es about binary numbers.
Binary numbers
#s you $now, unsigned integer numbers can be represented in bases %radices& other than base ten
%decimal&, for eample the decimal number 1' can be represented as 1( in base ) %octal& as 2' in base
*, as 112 in base + %ternary&, and as 1110 in base 2 %binary&. These follow from the standard
techni,ue of coding each position of the number as a power of the base-
n
d
n1
d
.. .
0
d
.
.or a decimal number we ha!e-
k
d
k =0
n

/
k
10
, while for a binary number
k
d
k =0
n

/
k
2
The second sum %for a binary number& also gi!es us a simple method to con!ert integers from base 2
to base 10.
Example: for the decimal number 2*2 we ha!e-
252 1111 1100
3
Decimal (Base 10) Binary (Base 2)
MS LS MS LS
2 * 2 1 1 1 1 1 1 0 0
100 10 1 12) (' +2 1( ) ' 2 1
10
2
10
1
10
0
2
0
2
(
2
*
2
'
2
+
2
2
2
1
2
0
1ere MS stands for Most Significant and signifies the leftmost digit or bit. LS stands for Least
Significant and signifies the rightmost digit or bit.
2e can ma$e the base of a number eplicit by suffiing it to the number as a subscript-
1'
10
3 1(
)
3 2'
*
3 112
+
3 1110
2

4n some computer languages we suffi the letter " %or b& to binary !alues e.g. 101"
1
2ell also loo$ at how computer programs are represented later in the course.
2
2ell also loo$ at the approimation of real numbers using floating5point numbers later in the course.
+
6paces or underscores in binary numbers are added for readability. 6paces7underscores between groups of ' bits
%grouping right5to5left& is common.
Decimal to Binary Conversion (Division)
There are se!eral ways of con!erting decimal numbers to binary. # simple techni,ue in!ol!es repeated
di!ision by 2.
Example: What is 98
10
in binary?
6teps-
8 9i!ide the number by 2 gi!ing the ,uotient and the remainder.
8 :epeat pre!ious step with the new ,uotient until a ;ero ,uotient is obtained.
8 The answer is obtained by reading the remainder column from the bottom to the top.
Quotient Remainder
98 2 49 0
49 2 24 1
24 2 12 0
12 2 6 0
6 2 3 0
3 2 1 1
1 2 0 1
#nswer- 110 0010
2
reading the remainder column from bottom to the top <65bit to =65bit
Decimal to Binary Conversion (Subtraction)
1ere is a second techni,ue that subtracts descending powers of 2 from the number.
Example- What is 98
10

in binary?
6teps-
8 9etermine C. C can be > or ?. 4f > then " is 1. 4f ? then " is 0.
8 4f C is > then set net @ to @ 5 A. 4f C is ? then continue with current @.
8 :epeat for descending powers of 2 in column A.
8 #nswer is in column " reading from top to bottom %<65bit to =65bit&
N C P B
98 64 1
98 - 64 = 34 32 1
34 - 32 = 2 < 16 0
2 < 8 0
2 < 4 0
2 2 1
2 - 2 = 0 < 1 0
#nswer- 110 0010
2
Binary Octal (Base 8) Conversion
4n the past
'
programmers sometimes used octal as a more con!enient base for representing long binary
!alues. The octal representation is shorter and the con!ersion from binary to octal and !ice5!ersa is
straightforward. The basic rule of thumb for an octal !alue is-
6tarting from the rightmost (least significant) en, each group of + bits represents 1 octal digit
%called an octet&.
000 %0&, 001 %1&, 010 %2&, 011 %+&, 100 %'&, 101 %*&, 110 %(&, 111 %0&
Example- What is 10101
2
in octal?
10 101

2 5
#nswer- 2*
)
Example- What is 357
8
in binary?
3 5 7

011 101 111
#nswer- 11101111
2
'
#lthough octal is less used these days, you should be familiar with the term.
Hexadecimal (Base 16)
1eadecimal %he& has o!erta$en octal as the radi used by programmers to represent long binary
!alues. "inary !alues can be integers, characters, real numbers, memory addresses, instructions, etc
2e will use headecimal etensi!ely, so it is important to master con!ersions. 4t is also important to
understand that headecimal and decimal are con!eniences for humans, the computer represents
e!erything in binary only. 2e will not perform arithmetic in headecimal, only use it as a shorthand for
writing binary !laues.
6tarting from the rightmost (least significant) en, each group of ' bits represents 1 he digit. 2e
use the symbols 0, 1, 2, +, ', *, (, 0, ), B, #, ", C, 9, C, . for these. The letters # to . can be in
uppercase or lowercase.
2e can summarise the correspondence between headecimal, decimal and binary with the following
table-
Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F
Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Binary 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111
Example: What is 252
10
= 1111 1100
2
in hexadecimal?
1111 1100

F C
#nswer. .C
1(

!ote- 2e will sometimes use the suffi the letter 1 %or h& after a headecimal !alue instead of the
subscript 1(, e.g. 2#+1. #nother alternati!e that is used in some programming languages is to
prefi the headecimal !alue with 0, e.g. 02#+
Radix Arithmetic
Aerforming addition, subtraction, multiplication and di!ision in bases other that 10 is identical to
decimal arithmetic ecept that we use the particular base as the point of reference instead of 10.
6ome eamples should ma$e this clear.
"ition "
#
$ B
#
2or$ing from :ight to =eft
%hile digits remain to be added
#dd current digits of # and "
&f 6D< is less than :
then record 6D<
'ther(ise
record difference 6D< 5 :
add %carry& 1 to net column
En (hile
' + 2
0
E * ' +
0
333333333333333333
1 + 0 *
0
S)btraction "
#
* B
#

(hen "+B
2or$ing from :ight to =eft
%hile digits remain to be subtracted
&f digitof %#& F3 digitof %"&
then record # 5 "
'ther(ise
record %: E #& G "
s)btract 1 from digits to #s left %or
add 1 to digits to "s left&
En (hile
1 ' + 2
)
G * 0 (
)
333333333333333333
( + '
)
Binary Addition Example
#ddition of unsigned binary numbers is straightforward.
Example- Perform the binary addition 11 1011 10 1010
Carry 1 1 1 1
A 1 1 1 0 1 1
B + 1 0 1 0 1 0
Sum 1 1 0 0 1 0 1
Step 7 6 5 4 3 2 1
Step k A
k
+B
k
+Carry
k
= Carry
k+1
Sum
k
1 1 + 0 = 1 =>
sum 1
2 1 + 1 = 10 => carry 1 sum 0
3 0 + 0 + 1 = 1 => sum 1
4 1 + 1 = 10 => carry 1 sum 0
5 1 + 0 + 1 = 10 => carry 1 sum 0
6 1 + 1 + 1 = 11 => carry 1 sum 1
7 1 = 1 =>
sum 1
Binary Subtraction Example
"inary subtraction is straightforward too. 2ell assume that the minuend is greater than the
subtrahend, i.e. the result is not negati!e.
Example: Perform the binary s!btraction 101 0101 " 1 1100
.
Step k A
k
B
k
= Dif
k
1 1 0 = 1
2 0 0 = 0
3 1 1 = 0
4 0 1 Borrow by subtracting 1 from A
7..5
=101 to give
A
7..5
=100 and A
4
=10.
Now use A instead of A, e.g. A
4
B
4
10 1 =1
5 0 1 Subtract 1 from A
7..6
=10 to give A
7..6
=01, A
5
=10.
Now use A instead of A, e.g. A
5
B
5
10 1 =1
6 1 0 = 1 i.e. A
6
B
6
7 0 0 = 0
4f youre used to borrowing by adding bac$ to " %the subtrahend&, then that method will wor$ also
and you are welcome to use it.
2arning- many students ha!e forgotten how to subtract numbers and become reliant on calculators.
<a$e sure you can subtract without a calculatorH
A 0 1 10
A 1 0 0 10
A 1 0 1 0 1 0 1
B 1 1 1 0 0
Dif 0 1 1 1 0 0 1
Step 7 6 5 4 3 2 1
Binary Multiplication Example
Example- Perform the binary m!lti#lication 1 1101 x 111
A 1 1 1 0 1
B 1 1 1
1 1 1 0 1 0 0
1 1 1 0 1 0
1 1 1 0 1
Answer 1 1 0 0 1 0 1 1
Carry 1 10 10 1 1
Binary Division Example
:ecall that di!ision is
di$idend
di$isor
= %!otient +
remainder
di$isor

or
di$idend = %!otient di$isor + remainder
The main difficulty with human di!ision is in estimating how many times the di!isor goes into the
partial di!idend. <ost people do this mentally. =uc$ily for binary numbers, this is easier since we only
ha!e two choices. 2e will postpone the consideration of fractional results until later in the course.
Example- Perform the binary di$ision 10 0011 1111 & 1 1001 i'e' decimal 575&25
Quotient-> 1 0 1 1 1
11001 1 0 0 0 1 1 1 1 1 1
1 1 0 0 1
1 0 1 0 1 1
1 1 0 0 1
1 0 0 1 0 1
1 1 0 0 1
1 1 0 0 1
1 1 0 0 1
Remainder-> 0
#lthough the numbers in the eample abo!e are binary, you may find it useful to pretend they are
decimal when performing comparisons, i.e. will 11,001 go into 10,001 %no&, will it go into 100,011
%yes&. 4f youre Iree$, then the di!ision techni,ue taught in Iree$ schools wor$s fine %try itH&.
Signed Integers
Dp until now we ha!e been assuming that integers are unsigned. 4n addition to representing the
magnitude of numbers, computer architects also need to consider the representation of the sign of a
number %E or 5& and for real numbers the representation of fractional !alues and the representation of
eponents. 4n this section well consider the representation of signed integers.
Bit Groups
@umbers within a computer system are not normally allowed to be of arbitrary length %si;e&. 4nstead
numbers are represented and manipulated in finite bit gro)ps. The most common bit groups are the
bit %15bit&, the byte %)5bit&, and the (or. 2ord si;e is architecture dependent but popular eamples
are 1(5bit words, +25bit words, and ('5bit words
*
. #rchitectures often employ their own uni,ue
names for the differing si;es, for eample, on the Aentium 1(5bit groups are called words, +25bit
groups are called doublewords and ('5bit groups are called ,uadwords.
Bit
3 2 1 0
Nibble
6
7 6 5 4 3 2 1 0
Byte
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
16-bit
Word
The numbers abo!e the bits indicate the bit position, with the least significant bit at position 0 %the
rightmost end&.
*
2e shall see later in the course that there is a strong relationship between word si;e, the width of memory locations
and the si;e of CAD registers.
(
@ibble 3 1alf5a5byte ob!iously -5&
Representing Unsigned Integers
4f we are only interested in natural %unsigned& numbers, then we can represent !alues by their binary
!alue within the computer, padding the number to the left with 0s to fill the byte or word. The
following table summarises the ranges of natural numbers
0
for se!eral bit group si;es-
Bit group Bits Hex
digits
No of possible bit
patterns
Representable
unsigned integers
Bit 1 1
2
1
=2
0 and 1
Nibble 4 1
2
4
= 16
0 to 15
Byte 8 2
2
8
= 256
0 to 255
Word(16bit) 16 4
2
16
= 65,536
0 to 65,535
Word(32bit) 32 8
2
32
= 4,294,967,296
0 to 4,294,967,295
N bits N N/4
8
2
N
0 to 2
N
1
Example- (ho) the bit #attern for the n!mber thirteen !sin* *ro!#s of 3+,+ 5 and - bits.
+ bits %we cant do this, since we do not ha!e sufficient bits&
' bits 1101
* bits 01101
( bits 001101
4n computing we use the following definitions for $ilo %J&, mega %<&, giga %I& and tera %T&-
Jilo J32
10
3102'
<ega <32
20
3102'
2
which is Kust o!er 1 million
Iiga I32
+0
3102'
+
which is Kust o!er 1 billion
0
#ny of the representations for signed integers can also be used although the range will be smaller. 2e will see later
in the course that unsigned integers are used to address main memory locations.
8
is the ceiling operator.
Tera T32
'0
3102'
'
which is Kust o!er 1 trillion
# common con!ention is to use " to signify "ytes and b for bits. .or eample, ' <" means '
<egabytes, while ' <b means ' <egabits.
Representing Signed Integers
#lthough most computers pro!ide some support for representing unsigned integers, good support for
the representation of signed integers within a computer is more important. 2e shall outline se!eral
representations- 6ign L <agnitude, Mnes Complement, Twos Complement, "ias5@ %Ccess5@&, and
"inary Coded 9ecimal %"C9&. The most important and most used representation is Twos
Complement.
4n any representation the properties we would ideally li$e to ha!e are-
8 only 1 bit pattern for each !alue
8 e,ual number of positi!e and negati!e !alues
8 maimum range of !alues
8 no gaps in the range
4n addition, and perhaps most important we would li$e to ha!e a fast and economical hardware
implementation
B
of integer arithmetic.
2e will use as an eample the '5bit group, i.e. the following bit5patterns-
Bit Pattern Unsigned
0000 +0
0001 +1
0010 +2
0011 +3
0100 +4
0101 +5
0110 +6
0111 +7
1000 +8
1001 +9
1010 +10
1011 +11
1100 +12
1101 +13
1110 +14
1111 +15
B
1ardware implementation 5F minimal number of transistors #@9 fast arithmetic if possible.
Representing Integers I - Sign & Magnitude
4n the sign L magnitude representation we use 1 bit %in!ariably the leftmost bit& of the bit group to
represent the sign of the integer and the remaining bits to represent the magnit)e.
Example- What inte*ers can )e re#resent in , bits !sin* si*n . ma*nit!de?
Bit Pattern Unsigned Sign &
Magnitude
0000 +0 +0
0001 +1 +1
0010 +2 +2
0011 +3 +3
0100 +4 +4
0101 +5 +5
0110 +6 +6
0111 +7 +7
1000 +8 0
1001 +9 1
1010 +10 2
1011 +11 3
1100 +12 4
1101 +13 5
1110 +14 6
1111 +15 7
Aerhaps the first thing we notice about sign L magnitude is that we ha!e two bit patterns for ;ero
%labelled E0 and G0&. This wastes a !alue and re,uires that the hardware treat both bit patterns as
;ero.
6ign L magnitude is the simplest representation for humans to understand, and a little bit5more costly
%in transistors& than other methods to implement. Why? "ecause signs need to be ta$en into account
eplicitly leading to the need to implement subtractors as well as adders, for eample, for addition we
need to compare signs and absolute !alues of the numbers and then add or subtract accordingly.
Comparing two absolute !alues is also a costly operation. Twos complement representation doesnt
ha!e these disad!antages.
.or an n5bit group, the sign L magnitude numbers will range from G%2
nG1
G1& to E%2
nG1
G1&
Representing Integers II - One's Complement (historical)
4n ones complement we represent %in a finite bit group& we can negate a number by complementing
%or in!erting& each bit. The complement of 1 is 0N the complement of 0 is 1.
Example: What inte*ers can )e re#resent in , bits !sin* one/s com#lement?
Bit Pattern Unsigned Sign &
Magnitude
One's
Complement
0000 +0 +0 +0
0001 +1 +1 +1
0010 +2 +2 +2
0011 +3 +3 +3
0100 +4 +4 +4
0101 +5 +5 +5
0110 +6 +6 +6
0111 +7 +7 +7
1000 +8 0 7
1001 +9 1 6
1010 +10 2 5
1011 +11 3 4
1100 +12 4 3
1101 +13 5 2
1110 +14 6 1
1111 +15 7 0
=i$e sign L magnitude, ones complement yields two representations for ;ero, and a 1 for the leftmost
bit indicates a negati!e !alue. Mnes complement is less intuiti!e %for humans& than sign L magnitude,
but less costly to implement in hardware. "ecause of the two representations for ;ero, with ones
complement the result after an operation is not always correct e.g. we must add 1 to the result of an
addition if the carry out from the most significant bit is 1. 4t is desirable to a!oid such complications in
hardware.
=i$e sign L magnitude, for an n5bit group the oneOs complement numbers will range from
G%2
nG1
G 1& to E%2
nG1
G1&
Mnes complement was used on some old computers and is no longer used.
Representing Integers III - Twos Complement
Twos complement representation is the most popular representation for signed integers. 4n twos
complement representation we obtain the negati,e of an integer by complementing each of its bits
and adding 1 to the result. The 2s complement for an n5bit binary number P is defined as %2
n
GP&
Example: What inte*ers can )e re#resent in , bits !sin* t)o/s com#lement?
Bit Pattern Unsigned Sign &
Magnitude
One's
Complement
Two's
Complement
0000 +0 +0 +0 +0
0001 +1 +1 +1 +1
0010 +2 +2 +2 +2
0011 +3 +3 +3 +3
0100 +4 +4 +4 +4
0101 +5 +5 +5 +5
0110 +6 +6 +6 +6
0111 +7 +7 +7 +7
1000 +8 0 7 8
1001 +9 1 6 7
1010 +10 2 5 6
1011 +11 3 4 5
1100 +12 4 3 4
1101 +13 5 2 3
1110 +14 6 1 2
1111 +15 7 0 1
The first thing that stri$es us with twos complement is that we only ha!e 1 bit pattern for ;ero. The
second thing that stri$es us, is that the representation is asymmetric
10
i.e. there is one etra negati!e
!alue. =uc$ily the asymmetric range is a minor disad!antage compared to the nice properties that
twos complement pro!ides.
Aerhaps the most useful property of twos complement is that subtraction can be performed by
forming the 2Os complement of the subtrahend and using addition instead. i.e.
P G Q 3 P E %GQ&
.orming the 2s complement turns out be a simple operation to implement so there is no need for a
separate subtractor %as in sign L magnitude& or carry5out adKustments %as in ones complement&.
Twos complement is also a true complement in the sense that EPE%GP& 3 0 and G%GP& 3 P.
10
6ign L <agnitude and Mnes Complement are symmetric
.or n5bits, twoOs complement numbers range from G2
nG1
to E%2
nG1
G1&
Twos complement clock layout
The twos complement range can !iewed on a cloc$ %see below&. 2hen we do this can model
addition by counting cloc$wise and subtraction by counting anti5cloc$wise. @ote also that how the
twos complement of a number is hori;ontally opposite the number and that such pairs add upto
2
'
31( as unsigned integers.

Example: 0alc!late 5 1",2 and 5 " 1"22 !sin* the t)o3s com#lement clock'
Conceptually we perform the addition by counting cloc$wise, i.e. E* E G' is 0101 E 1100, i.e.
mo!e 1100 %twel!e& places cloc$wise from 0101 which arri!es at 0001 3 E1.
6imilarly for the subtraction we count anticloc$wise, e.g. E* G %G2& is 0101 G 1110 e.g. mo!e
1110 %fourteen& places anticloc$wise from 0101 which arri!es at 01113E0.
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
0
+1
+2
+3
+4
+5
+6
+7
-1
-3
-2
-4
-5
-6
-7
-8
Tens Complement
4t is interesting to $now that we can also ha!e complements in other bases, e.g. tens complement.
The 10s complement of an n5digit decimal number P is defined as %10
n
GP&, e.g. for the '5digit
decimal number 12+' the 10s complement is )0((. :ecall that the 2s complement for an n5bit binary
number P is defined as %2
n
GP&.
Twos Complement to Decimal
To con!ert a twos complement number to decimal we can use the formula-

n1
b
*
n1
2
+
k
b
k=0
n2

*
k
2
Example: 0on$ert the ,4bit t)o/s com#lement $al!e 1011 to decimal'
3 %G1 / )& E %0 / '& E %1 / 2& E %1 / 1&
3 G) E 2 E 1
3 G*
#lternati!ely for negati!e !alues we can negate the number %ma$e it positi!e&, con!ert to decimal L
negate it. .or eample for 1101 we can
@egate 1101 by in!erting each bit and adding one gi!ing 0101
Con!ert 0101 to decimal i.e. 1/' E 1/1 3 *
@egate * to gi!e G*
Representing Integers IV - Excess-n or Bias-n
4n Ccess5n %also called "ias5n& representation we represent an integer P by PEn. nF0.
Example: What inte*ers can )e re#resent in , bits !sin* an 5xcess48 re#resentation?
Bit Pattern Unsigned Sign &
Magnitude
Ones
Complement
Two's
Complement
Excess 8
0000 0 +0 +0 +0 8
0001 +1 +1 +1 +1 7
0010 +2 +2 +2 +2 6
0011 +3 +3 +3 +3 5
0100 +4 +4 +4 +4 4
0101 +5 +5 +5 +5 3
0110 +6 +6 +6 +6 2
0111 +7 +7 +7 +7 1
1000 +8 0 7 8 0
1001 +9 1 6 7 +1
1010 +10 2 5 6 +2
1011 +11 3 4 5 +3
1100 +12 4 3 4 +4
1101 +13 5 2 3 +5
1110 +14 6 1 2 +6
1111 +15 7 0 1 +7
=i$e twoOs complement, ecess5n is asymmetric. Ccess5n is used where it is important to be able to
compare %and sort& !alues easily. 2e will return to it when we consider the representation of
eponents for real numbers later in the course. 6ome calculators also employ ecess5n numbers.
@ote- if n is chosen to be e,ual to 2
mG1
where m is the number of bits in the representation then
Ccess5n representation is the same as twoOs complement but with the sign5bit in!erted.
Representing Integers V - Binary Coded Decimal (BCD)
6ome architectures support an integer representation called "inary Coded 9ecimal %"C9&. "C9 is
often pro!ided to support commercial applications programmed in the CM"M= language since these
applications tend be 47M intensi!e with little arithmetic processing, and the use of "C9 a!oids costly
binary5to5decimal and decimal5to5binary con!ersions. #lso "C9 implementations tend to support
!ariable5si;ed bit groups, e.g. we can easily represent integers with +00 digits.
The idea behind "C9 is !ery simple. 2e represent each decimal digit by ' bits %a nibble& that encode
each decimal digit i.e. we ha!e
Bit Pattern Unsigned Sign &
Magnitude
One's
Complement
Two's
Complement
Excess-8 BCD
0000 0 +0 +0 +0 8 0
0001 +1 +1 +1 +1 7 +1
0010 +2 +2 +2 +2 6 +2
0011 +3 +3 +3 +3 5 +3
0100 +4 +4 +4 +4 4 +4
0101 +5 +5 +5 +5 3 +5
0110 +6 +6 +6 +6 2 +6
0111 +7 +7 +7 +7 1 +7
1000 +8 0 7 8 0 +8
1001 +9 1 6 7 +1 +9
1010 +10 2 5 6 +2 spare
1011 +11 3 4 5 +3 spare
1100 +12 4 3 4 +4 spare
1101 +13 5 2 3 +5 spare
1110 +14 6 1 2 +6 spare
1111 +15 7 0 1 +7 spare
"C9 is ob!iously !ery easy for humans to comprehend, although a smaller range of numbers is
representable compared with other binary representations, e.g. 1( bits can only represent the natural
numbers 0 to BBBB. 6igns are handled by using two of the unused bit patterns, for eample, on the
R#P #rchitecture, the bit pattern 1100 is used for the E sign and 1101 for the G sign. Confusingly
howe!er, in the R#P architecture the sign nibble occurs at the least significant end.
Example. What is 837
10
and "837
10
in 678 90:?
Dnsigned !alue )+0
10
is 1000 0011 0111 in "C9.
E)+0 3 1000 0011 0111 1100 in R#P "C9 %E at end&
G)+0 3 1000 0011 0111 1101 in R#P "C9 %G at end&
Signed Integer Arithmetic
"ecause of the dominance of twos complement representation we shall only consider arithmetic for
it, in this course. The nice feature with twoOs complement is that addition and subtraction of twoOs
complement numbers wor$s without ha!ing to separate the sign bits %the sign of the operands and
results is effecti!ely subsumed addition7subtraction calculation&. 4f the result of an arithmetic
operation is to too large or too small to fit into the resultant bit5group, then we ha!e an arithmetic
o,erflo(. 4t is normally left to the programmer to decide how to deal with this situation
11
.
Two's Complement Addition
.or twos complement addition we add the !alues and discard any carry5out bit.
Example: 0alc!late 31"82 and "21"52 !sin* an 84bit t)o3s com#lement re#resentation'
(+3) 0000 0011 (2) 1111 1110
+(8) 1111 1000 +(5) 1111 1011
(5) 1111 1011 (7) 1 1111 1001
Discard Carry-Out
',erflo( #)le: 4f 2 twos complement numbers are added, and they are both positi!e or both
negati!e, then o!erflow occurs if and only the result has the opposite sign, i.e.
%E#& E %E"& 3 GC
or
%G#& E %G"& 3 EC
Example: 0alc!late "71"-2 !sin* a ,4bit t)o/s com#lement re#resentation'
(7) 1001
+(6) 1010
(+3) 1 0011 Overfow
11
1ow might we deal with integer o!erflowS
Two's Complement Subtraction
6ubtraction in twoOs complement is accomplished by negating the subtrahend and adding it to the
minuend. #ny carry5out bit is discarded.
Example: 0alc!late 8 " 5 !sin* an 84bit t)o3s com#lement re#resentation
(+8) 0000 1000 0000 1000
(+5) 0000 0101 -> Negate -> + 1111 1011
(+3) 1 0000 0011
Discard
',erflo( #)le:
4f 2 twos complement numbers are subtracted, and their signs are different, then o!erflow occurs if
and only if the result has the same sign as the subtrahend.
%E#& G %G"& 3 GC
or
%G#& G %E"& 3 EC
Example: 0alc!late 7"1"-2 !sin* a ,4bit t)o/s com#lement re#resentation'
(+7) 0111
(6) 0110 (Negated)
(3) 1101 Overfow
Twos Complement Multiplication & Division
<ultiplication in twos complement cannot be accomplished with the standard techni,ue.
.or eample consider P / %GQ&. The twoOs complement of GQ is 2
n
GQ. Therefore P %Q& 3 P%2
n
GQ&
3 2
n
P G PQ. 1owe!er the epected result should be 2
2n
G PQ. 2e can perform multiplication by
con!erting the twoOs complement numbers to their absolute !alues and then negate the result if the
signs of the operands are different. # similar situation eists for twos complement di!ision. To do
di!ision by hand, it is easier con!ert the !alues to absolute !alues, perform the di!ision, and then
negate if the operand signs are different.
<ost architectures implement more sophisticated algorithms. .ortunately for us, it is beyond the
scope of this course to eamine these algorithms. 4f you need to perform multiplication and di!ision,
use the schoolboy method on positi!e !alues and set the signs accordingly.
Characters
Computers map characters to bit patterns %unsigned integers effecti!ely&. The most common
mappings in use are #6C44
12
%pronounced as5$ey& and Dnicode. #n older mapping is 4"<s C"C94C.
"S-&& uses 05bits %12) bit patterns& although most computers etend this to ) bits yielding an etra
12) bit5patterns. #6C44 has 2( lowercase letters, 2( uppercase letters, 10 digits, and +2 punctuation
mar$s. The remaining +' bit patterns represent whitespace characters e.g. space %6A&, tab %1T&, return
%C:&, linefeed %=.& and special control characters that are used in interfacing to 47M de!ices. @ote
that the uppercase letters #5T, lowercase letters a5; and the digits 05B ha!e contiguous !alues.
6trings are represented as se,uences of characters. C.g. The name .re is encoded as follows-
English F r e d
ASCII (Binary) 0100 0110 0111 0010 0110 0101 0110 0100
ASCII (Hex) 46 72 65 64
/he 0*bit "S-&& -haracter Set
Bit positions 654
Bit
positions
000 001 010 011 100 101 110 111 3210
NUL DLE SP 0 @ P p 0000
SOH DC1 ! 1 A Q a q 0001
STX DC2 2 B R b r 0010
ETX DC3 # 3 C S c s 0011
EOT DC4 $ 4 D T d t 0100
ENQ NAK % 5 E U e u 0101
ACK SYN & 6 F V f v 0110
BEL ETB 7 G W g w 0111
BS CAN ( 8 H X h x 1000
HT EM ) 9 I Y i y 1001
LF SUB * : J Z j z 1010
VT ESC + ; K [ k { 1011
FF FS , < L \ l | 1100
CR GS - = M ] m } 1101
SO RS . > N ^ n ~ 1110
SI US / ? O _ o DEL 1111
12
#merican 6tandard Code for 4nformation 4nterchange

You might also like