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

6/18/2016

2'sComplementTechnicalNotes

Programming
2'sComplementRepresentationforSignedIntegers
Definition
Calculationof2'sComplement
Addition
Subtraction
Multiplication
Division
SignExtension
OtherSignedRepresentations
Notes

Definition
Property
Two'scomplementrepresentationallowstheuseofbinaryarithmeticoperationsonsignedintegers,yielding
thecorrect2'scomplementresults.
PositiveNumbers
Positive2'scomplementnumbersarerepresentedasthesimplebinary.
NegativeNumbers
Negative2'scomplementnumbersarerepresentedasthebinarynumberthatwhenaddedtoapositivenumber
ofthesamemagnitudeequalszero.
Integer
Signed Unsigned

2'sComplement

00000101

00000100

00000011

00000010

00000001

00000000

255

11111111

254

11111110

253

11111101

252

11111100

251

11111011

Note:Themostsignificant(leftmost)bitindicatesthesignoftheintegerthereforeitissometimescalledthesign
bit.
Ifthesignbitiszero,
thenthenumberisgreaterthanorequaltozero,orpositive.
Ifthesignbitisone,
thenthenumberislessthanzero,ornegative.
http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm

1/4

6/18/2016

2'sComplementTechnicalNotes

Calculationof2'sComplement
Tocalculatethe2'scomplementofaninteger,invertthebinaryequivalentofthenumberbychangingalloftheones
tozeroesandallofthezeroestoones(alsocalled1'scomplement),andthenaddone.
Forexample,
00010001(binary17)

11101111(two'scomplement17)

= 11101110(Invertbits)
11101110+00000001 = 11101111(Add1)
NOT(00010001)

2'sComplementAddition
Two'scomplementadditionfollowsthesamerulesasbinaryaddition.
Forexample,
5+(3)=2 00000101 = +5
+11111101

= 3

00000010

= +2

2'sComplementSubtraction
Two'scomplementsubtractionisthebinaryadditionoftheminuendtothe2'scomplementofthesubtrahend
(addinganegativenumberisthesameassubtractingapositiveone).
Forexample,
712=(5) 00000111 = +7
+11110100

= 12

11111011

= 5

2'sComplementMultiplication
Two'scomplementmultiplicationfollowsthesamerulesasbinarymultiplication.
Forexample,
(4)4=(16) 11111100 = 4
00000100 = +4
11110000

= 16

http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm

2/4

6/18/2016

2'sComplementTechnicalNotes

2'sComplementDivision
Two'scomplementdivisionisrepeated2'scomplementsubtraction.The2'scomplementofthedivisoris
calculated,thenaddedtothedividend.Forthenextsubtractioncycle,thequotientreplacesthedividend.This
repeatsuntilthequotientistoosmallforsubtractionoriszero,thenitbecomestheremainder.Thefinalansweris
thetotalofsubtractioncyclesplustheremainder.
Forexample,
73=2remainder1 00000111 = +7 00000100 = +4
+11111101

= 3

+11111101

= 3

00000100

= +4

00000001

= +1(remainder)

SignExtension
Toextendasignedintegerfrom8bitsto16bitsorfrom16bitsto32bits,appendadditionalbitsontheleftsideof
thenumber.Filleachextrabitwiththevalueofthesmallernumber'smostsignificantbit(thesignbit).
Forexample,
SignedInteger 8bitRepresentation 16bitRepresentation
1

11111111

1111111111111111

+1

00000001

0000000000000001

OtherRepresentationsofSignedIntegers
SignMagnitudeRepresentation
Anothermethodofrepresentingnegativenumbersissignmagnitude.Signmagnituderepresentationalsouses
themostsignificantbitofthenumbertoindicatethesign.Anegativenumberisthe7bitbinaryrepresentation
ofthepositivenumberwiththemostsignificantbitsettoone.Thedrawbackstousingthismethodfor
arithmeticcomputationarethatadifferentsetofrulesarerequiredandthatzerocanhavetworepresentations
(+0,00000000and0,10000000).

OffsetBinaryRepresentation
Athirdmethodforrepresentingsignednumbersisoffsetbinary.Begincalculatingaoffsetbinarycodeby
assigninghalfofthelargestpossiblenumberasthezerovalue.Apositiveintegeristheabsolutevalueadded
tothezeronumberandanegativeintegerissubtracted.OffsetbinaryispopularinA/DandD/Aconversions,
butitisstillawkwardforarithmeticcomputation.

Forexample,
Largestvaluefor8bitinteger=28=256
Offsetbinaryzerovalue=2562=128(decimal)=10000000(binary)
10000000(offsetbinary0)+00010110(binary22)=10010110(offsetbinary+22)
10000000(offsetbinary0)00000111(binary7)=01111001(offsetbinary7)

SignedInteger SignMagnitude OffsetBinary

http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm

3/4

6/18/2016

2'sComplementTechnicalNotes

+5

00000101

10000101

+4

00000100

10000100

+3

00000011

10000011

+2

00000010

10000010

+1

00000001

10000001

00000000
10000000

10000000

10000001

01111111

10000010

01111110

10000011

01111101

10000100

01111100

10000101

01111011

Notes
OtherComplements
1'sComplement=NOT(n)=11111111n
9'sComplement=99999999n
10'sComplement=(99999999n)+1

BinaryArithmetic
Addition
Subtraction
Multiplication
Division
[Index|TechnicalNotes]
DISCLAIMER
Pageauthor:DawnRorvik(rorvikd@evergreen.edu)
Lastmodified:05/20/2003

http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm

4/4

Вам также может понравиться