<s>
Two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
is	O
a	O
mathematical	O
operation	O
to	O
reversibly	O
convert	O
a	O
positive	O
binary	O
number	O
into	O
a	O
negative	O
binary	O
number	O
with	O
equivalent	O
negative	O
value	O
,	O
using	O
the	O
binary	O
digit	O
with	O
the	O
greatest	O
place	O
value	O
as	O
the	O
sign	B-Algorithm
to	O
indicate	O
whether	O
the	O
binary	O
number	O
is	O
positive	O
or	O
negative	O
.	O
</s>
<s>
It	O
is	O
used	O
in	O
computer	B-General_Concept
science	I-General_Concept
as	O
the	O
most	O
common	O
method	B-Algorithm
of	I-Algorithm
representing	I-Algorithm
signed	I-Algorithm
(	O
positive	O
,	O
negative	O
,	O
and	O
zero	O
)	O
integers	O
on	O
computers	O
,	O
and	O
more	O
generally	O
,	O
fixed	O
point	O
binary	O
values	O
.	O
</s>
<s>
Two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
is	O
achieved	O
by	O
:	O
</s>
<s>
Step	O
3	O
:	O
adding	O
1	O
to	O
the	O
entire	O
inverted	O
number	O
,	O
ignoring	O
any	O
overflow	B-Algorithm
.	O
</s>
<s>
Failing	O
to	O
ignore	O
overflow	B-Algorithm
will	O
produce	O
the	O
wrong	O
value	O
for	O
the	O
result	O
.	O
</s>
<s>
For	O
example	O
,	O
to	O
calculate	O
the	O
decimal	B-Application
number	O
−6	O
in	O
binary	O
:	O
</s>
<s>
Step	O
1	O
:	O
+6	O
in	O
decimal	B-Application
is	O
0110	O
in	O
binary	O
;	O
the	O
leftmost	O
significant	O
bit	O
(	O
the	O
first	O
0	O
)	O
is	O
the	O
sign	B-Algorithm
.	O
</s>
<s>
+6	O
is	O
not	O
110	O
,	O
because	O
110	O
in	O
binary	O
is	O
−2	O
in	O
decimal	B-Application
.	O
</s>
<s>
To	O
verify	O
that	O
1010	O
indeed	O
has	O
a	O
value	O
of	O
−6	O
,	O
add	O
the	O
place	O
values	O
together	O
,	O
but	O
subtracting	O
the	O
sign	B-Algorithm
from	O
the	O
final	O
calculation	O
.	O
</s>
<s>
Because	O
that	O
the	O
first	O
significant	O
digit	O
is	O
the	O
number	O
sign	B-Algorithm
it	O
must	O
be	O
subtracted	O
to	O
produce	O
the	O
correct	O
result	O
:	O
1010	O
=	O
(	O
1×−23	O
)	O
+	O
(	O
0×22	O
)	O
+	O
(	O
1×21	O
)	O
+	O
(	O
0×20	O
)	O
=	O
1×−8	O
+	O
0	O
+	O
1×2	O
+	O
0	O
=	O
−6	O
.	O
</s>
<s>
Two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
is	O
an	O
example	O
of	O
a	O
radix	B-Algorithm
complement	I-Algorithm
.	O
</s>
<s>
The	O
'	O
two	O
 '	O
in	O
the	O
name	O
refers	O
to	O
the	O
term	O
which	O
,	O
expanded	O
fully	O
in	O
an	O
-bit	O
system	O
,	O
is	O
actually	O
"	O
two	O
to	O
the	O
power	O
of	O
N	O
"	O
-	O
(	O
the	O
only	O
case	O
where	O
exactly	O
'	O
two	O
 '	O
would	O
be	O
produced	O
in	O
this	O
term	O
is	O
,	O
so	O
for	O
a	O
1-bit	O
system	O
,	O
but	O
these	O
do	O
n't	O
have	O
capacity	O
for	O
both	O
a	O
sign	B-Algorithm
and	O
a	O
zero	O
)	O
,	O
and	O
it	O
is	O
only	O
this	O
full	O
term	O
in	O
respect	O
to	O
which	O
the	O
complement	B-Algorithm
is	O
calculated	O
.	O
</s>
<s>
As	O
such	O
,	O
the	O
precise	O
definition	O
of	O
the	O
Two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
an	O
-bit	O
number	O
is	O
the	O
complement	B-Algorithm
of	O
that	O
number	O
with	O
respect	O
to	O
.	O
</s>
<s>
The	O
defining	O
property	O
of	O
being	O
a	O
complement	B-Algorithm
to	O
a	O
number	O
with	O
respect	O
to	O
is	O
simply	O
that	O
the	O
summation	O
of	O
this	O
number	O
with	O
the	O
original	O
produce	O
.	O
</s>
<s>
For	O
example	O
,	O
using	O
binary	O
with	O
numbers	O
up	O
to	O
three-bits	O
(	O
so	O
and	O
,	O
where	O
'	O
2	O
 '	O
indicates	O
a	O
binary	O
representation	O
)	O
,	O
a	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
for	O
the	O
number	O
3	O
(	O
)	O
is	O
5	O
(	O
)	O
,	O
because	O
summed	O
to	O
the	O
original	O
it	O
gives	O
.	O
</s>
<s>
Where	O
this	O
correspondence	O
is	O
employed	O
for	O
representing	O
negative	O
numbers	O
,	O
it	O
effectively	O
means	O
,	O
using	O
an	O
analogy	O
with	O
decimal	B-Application
digits	O
and	O
a	O
number-space	O
only	O
allowing	O
eight	O
non-negative	O
numbers	O
0	O
through	O
7	O
,	O
dividing	O
the	O
number-space	O
in	O
two	O
sets	O
:	O
the	O
first	O
four	O
of	O
the	O
numbers	O
0	O
1	O
2	O
3	O
remain	O
the	O
same	O
,	O
while	O
the	O
remaining	O
four	O
encode	O
negative	O
numbers	O
,	O
maintaining	O
their	O
growing	O
order	O
,	O
so	O
making	O
4	O
encode	O
-4	O
,	O
5	O
encode	O
-3	O
,	O
6	O
encode	O
-2	O
and	O
7	O
encode	O
-1	O
.	O
</s>
<s>
A	O
binary	O
representation	O
has	O
an	O
additional	O
utility	O
however	O
,	O
because	O
the	O
most	O
significant	O
bit	O
also	O
indicates	O
the	O
group	O
(	O
and	O
the	O
sign	B-Algorithm
)	O
:	O
it	O
is	O
0	O
for	O
the	O
first	O
group	O
of	O
non-negatives	O
,	O
and	O
1	O
for	O
the	O
second	O
group	O
of	O
negatives	O
.	O
</s>
<s>
Calculation	O
of	O
the	O
binary	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
a	O
positive	O
number	O
essentially	O
means	O
subtracting	O
the	O
number	O
from	O
the	O
.	O
</s>
<s>
But	O
as	O
can	O
be	O
seen	O
for	O
the	O
three-bit	O
example	O
and	O
the	O
four-bit	O
(	O
)	O
,	O
the	O
number	O
will	O
not	O
itself	O
be	O
representable	O
in	O
a	O
system	O
limited	O
to	O
bits	O
,	O
as	O
it	O
is	O
just	O
outside	O
the	O
bits	O
space	O
(	O
the	O
number	O
is	O
nevertheless	O
the	O
reference	O
point	O
of	O
the	O
"	O
Two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
"	O
in	O
an	O
-bit	O
system	O
)	O
.	O
</s>
<s>
Coincidentally	O
,	O
that	O
intermediate	O
number	O
before	O
adding	O
the	O
one	O
is	O
also	O
used	O
in	O
computer	B-General_Concept
science	I-General_Concept
as	O
another	O
method	O
of	O
signed	B-Algorithm
number	I-Algorithm
representation	I-Algorithm
and	O
is	O
called	O
a	O
Ones	O
 '	O
complement	B-Algorithm
(	O
named	O
that	O
because	O
summing	O
such	O
a	O
number	O
with	O
the	O
original	O
gives	O
the	O
'	O
all	O
1s	O
 '	O
)	O
.	O
</s>
<s>
Compared	O
to	O
other	O
systems	O
for	O
representing	O
signed	O
numbers	O
(	O
e.g.	O
,	O
ones	O
 '	O
complement	B-Algorithm
)	O
,	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
has	O
the	O
advantage	O
that	O
the	O
fundamental	O
arithmetic	O
operations	O
of	O
addition	O
,	O
subtraction	O
,	O
and	O
multiplication	O
are	O
identical	O
to	O
those	O
for	O
unsigned	O
binary	O
numbers	O
(	O
as	O
long	O
as	O
the	O
inputs	O
are	O
represented	O
in	O
the	O
same	O
number	O
of	O
bits	O
as	O
the	O
output	O
,	O
and	O
any	O
overflow	B-Algorithm
beyond	O
those	O
bits	O
is	O
discarded	O
from	O
the	O
result	O
)	O
.	O
</s>
<s>
Additionally	O
,	O
unlike	O
ones	O
 '	O
complement	B-Algorithm
systems	O
,	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
has	O
no	O
representation	O
for	O
negative	B-Algorithm
zero	I-Algorithm
,	O
and	O
thus	O
does	O
not	O
suffer	O
from	O
its	O
associated	O
difficulties	O
.	O
</s>
<s>
Otherwise	O
,	O
both	O
schemes	O
have	O
the	O
desired	O
property	O
that	O
the	O
sign	B-Algorithm
of	O
integers	O
can	O
be	O
reversed	O
by	O
taking	O
the	O
complement	B-Algorithm
of	O
its	O
binary	O
representation	O
,	O
but	O
Two	O
's	O
component	O
has	O
an	O
exception	O
-	O
the	O
lowest	O
negative	O
,	O
as	O
can	O
be	O
seen	O
in	O
the	O
tables	O
.	O
</s>
<s>
The	O
method	B-Algorithm
of	I-Algorithm
complements	I-Algorithm
had	O
long	O
been	O
used	O
to	O
perform	O
subtraction	O
in	O
decimal	B-Application
adding	O
machines	O
and	O
mechanical	O
calculators	O
.	O
</s>
<s>
John	O
von	O
Neumann	O
suggested	O
use	O
of	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
binary	O
representation	O
in	O
his	O
1945	O
First	O
Draft	O
of	O
a	O
Report	O
on	O
the	O
EDVAC	O
proposal	O
for	O
an	O
electronic	O
stored-program	O
digital	O
computer	O
.	O
</s>
<s>
The	O
1949	O
EDSAC	B-Device
,	O
which	O
was	O
inspired	O
by	O
the	O
First	O
Draft	O
,	O
used	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
representation	O
of	O
negative	O
binary	O
integers	O
.	O
</s>
<s>
Many	O
early	O
computers	O
,	O
including	O
the	O
CDC	B-Device
6600	I-Device
,	O
the	O
LINC	B-Device
,	O
the	O
PDP-1	B-Device
,	O
and	O
the	O
UNIVAC	O
1107	O
,	O
use	O
ones	O
 '	O
complement	B-Algorithm
notation	O
;	O
the	O
descendants	O
of	O
the	O
UNIVAC	O
1107	O
,	O
the	O
UNIVAC	B-Device
1100/2200	I-Device
series	I-Device
,	O
continued	O
to	O
do	O
so	O
.	O
</s>
<s>
The	O
IBM	B-Device
700/7000	I-Device
series	I-Device
scientific	O
machines	O
use	O
sign/magnitude	O
notation	O
,	O
except	O
for	O
the	O
index	O
registers	O
which	O
are	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
.	O
</s>
<s>
Early	O
commercial	O
computers	O
storing	O
negative	O
values	O
in	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
form	O
include	O
the	O
English	B-Device
Electric	I-Device
DEUCE	I-Device
(	O
1955	O
)	O
,	O
Digital	O
Equipment	O
Corporation	O
PDP-5	B-Device
and	O
the	O
1963	O
PDP-6	B-Device
.	O
</s>
<s>
The	O
System/360	B-Application
,	O
introduced	O
in	O
1964	O
by	O
IBM	O
,	O
then	O
the	O
dominant	O
player	O
in	O
the	O
computer	O
industry	O
,	O
made	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
the	O
most	O
widely	O
used	O
binary	O
representation	O
in	O
the	O
computer	O
industry	O
.	O
</s>
<s>
The	O
first	O
minicomputer	O
,	O
the	O
PDP-8	B-Device
introduced	O
in	O
1965	O
,	O
uses	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
arithmetic	I-General_Concept
as	O
do	O
the	O
1969	O
Data	B-Device
General	I-Device
Nova	I-Device
,	O
the	O
1970	O
PDP-11	B-Device
,	O
and	O
almost	O
all	O
subsequent	O
minicomputers	O
and	O
microcomputers	O
.	O
</s>
<s>
A	O
two's-complement	B-General_Concept
number	O
system	O
encodes	O
positive	O
and	O
negative	O
numbers	O
in	O
a	O
binary	O
number	O
representation	O
.	O
</s>
<s>
The	O
most	O
significant	O
bit	O
determines	O
the	O
sign	B-Algorithm
of	O
the	O
number	O
and	O
is	O
sometimes	O
called	O
the	O
sign	B-Algorithm
bit	I-Algorithm
.	O
</s>
<s>
Unlike	O
in	O
sign-and-magnitude	O
representation	O
,	O
the	O
sign	B-Algorithm
bit	I-Algorithm
also	O
has	O
the	O
weight	O
shown	O
above	O
.	O
</s>
<s>
In	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
notation	I-General_Concept
,	O
a	O
non-negative	O
number	O
is	O
represented	O
by	O
its	O
ordinary	O
binary	O
representation	O
;	O
in	O
this	O
case	O
,	O
the	O
most	O
significant	O
bit	O
is	O
0	O
.	O
</s>
<s>
However	O
a	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
8-bit	O
number	O
can	O
only	O
represent	O
non-negative	O
integers	O
from	O
0	O
to	O
127	O
(	O
01111111	O
)	O
,	O
because	O
the	O
rest	O
of	O
the	O
bit	O
combinations	O
with	O
the	O
most	O
significant	O
bit	O
as	O
'	O
1	O
 '	O
represent	O
the	O
negative	O
integers	O
−1	O
to	O
−128	O
.	O
</s>
<s>
The	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
operation	O
is	O
the	O
additive	O
inverse	O
operation	O
,	O
so	O
negative	O
numbers	O
are	O
represented	O
by	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
the	O
absolute	O
value	O
.	O
</s>
<s>
To	O
get	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
a	O
negative	O
binary	O
number	O
,	O
all	O
bits	O
are	O
inverted	O
,	O
or	O
"	O
flipped	O
"	O
,	O
by	O
using	O
the	O
bitwise	O
NOT	O
operation	O
;	O
the	O
value	O
of	O
1	O
is	O
then	O
added	O
to	O
the	O
resulting	O
value	O
,	O
ignoring	O
the	O
overflow	B-Algorithm
which	O
occurs	O
when	O
taking	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
0	O
.	O
</s>
<s>
To	O
convert	O
to	O
−5	O
in	O
two's-complement	B-General_Concept
notation	O
,	O
first	O
,	O
all	O
bits	O
are	O
inverted	O
,	O
that	O
is	O
:	O
0	O
becomes	O
1	O
and	O
1	O
becomes	O
0	O
:	O
</s>
<s>
At	O
this	O
point	O
,	O
the	O
representation	O
is	O
the	O
ones	O
 '	O
complement	B-Algorithm
of	O
the	O
decimal	B-Application
value	O
−5	O
.	O
</s>
<s>
To	O
obtain	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
,	O
1	O
is	O
added	O
to	O
the	O
result	O
,	O
giving	O
:	O
</s>
<s>
The	O
result	O
is	O
a	O
signed	O
binary	O
number	O
representing	O
the	O
decimal	B-Application
value	O
−5	O
in	O
two's-complement	B-General_Concept
form	O
.	O
</s>
<s>
The	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
a	O
negative	O
number	O
is	O
the	O
corresponding	O
positive	O
value	O
,	O
except	O
in	O
the	O
special	O
case	O
of	O
the	O
most	O
negative	O
number	O
.	O
</s>
<s>
Likewise	O
,	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
zero	O
is	O
zero	O
:	O
inverting	O
gives	O
all	O
ones	O
,	O
and	O
adding	O
one	O
changes	O
the	O
ones	O
back	O
to	O
zeros	O
(	O
since	O
the	O
overflow	B-Algorithm
is	O
ignored	O
)	O
.	O
</s>
<s>
The	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
the	O
most	O
negative	O
number	O
representable	O
(	O
e.g.	O
</s>
<s>
Hence	O
,	O
there	O
is	O
an	O
'	O
extra	O
 '	O
negative	O
number	O
for	O
which	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
does	O
not	O
give	O
the	O
negation	O
,	O
see	O
below	O
.	O
</s>
<s>
The	O
sum	O
of	O
a	O
number	O
and	O
its	O
ones	O
 '	O
complement	B-Algorithm
is	O
an	O
-bit	O
word	O
with	O
all	O
1	O
bits	O
,	O
which	O
is	O
(	O
reading	O
as	O
an	O
unsigned	O
binary	O
number	O
)	O
.	O
</s>
<s>
Then	O
adding	O
a	O
number	O
to	O
its	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
results	O
in	O
the	O
lowest	O
bits	O
set	O
to	O
0	O
and	O
the	O
carry	B-Algorithm
bit	I-Algorithm
1	O
,	O
where	O
the	O
latter	O
has	O
the	O
weight	O
(	O
reading	O
it	O
as	O
an	O
unsigned	O
binary	O
number	O
)	O
of	O
.	O
</s>
<s>
Hence	O
,	O
in	O
the	O
unsigned	O
binary	O
arithmetic	O
the	O
value	O
of	O
two's-complement	B-General_Concept
negative	O
number	O
of	O
a	O
positive	O
satisfies	O
the	O
equality	O
.	O
</s>
<s>
A	O
shortcut	O
to	O
manually	O
convert	O
a	O
binary	O
number	O
into	O
its	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
is	O
to	O
start	O
at	O
the	O
least	O
significant	O
bit	O
(	O
LSB	O
)	O
,	O
and	O
copy	O
all	O
the	O
zeros	O
,	O
working	O
from	O
LSB	O
toward	O
the	O
most	O
significant	O
bit	O
(	O
MSB	O
)	O
until	O
the	O
first1	O
is	O
reached	O
;	O
then	O
copy	O
that1	O
,	O
and	O
flip	O
all	O
the	O
remaining	O
bits	O
(	O
Leave	O
the	O
MSB	O
as	O
a	O
1	O
if	O
the	O
initial	O
number	O
was	O
in	O
sign-and-magnitude	O
representation	O
)	O
.	O
</s>
<s>
This	O
shortcut	O
allows	O
a	O
person	O
to	O
convert	O
a	O
number	O
to	O
its	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
without	O
first	O
forming	O
its	O
ones	O
 '	O
complement	B-Algorithm
.	O
</s>
<s>
For	O
example	O
:	O
in	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
representation	O
,	O
the	O
negation	O
of	O
"	O
00111100	O
"	O
is	O
"	O
11000100	O
"	O
,	O
where	O
the	O
underlined	O
digits	O
were	O
unchanged	O
by	O
the	O
copying	O
operation	O
(	O
while	O
the	O
rest	O
of	O
the	O
digits	O
were	O
flipped	O
)	O
.	O
</s>
<s>
In	O
computer	O
circuitry	O
,	O
this	O
method	O
is	O
no	O
faster	O
than	O
the	O
"	O
complement	B-Algorithm
and	O
add	O
one	O
"	O
method	O
;	O
both	O
methods	O
require	O
working	O
sequentially	O
from	O
right	O
to	O
left	O
,	O
propagating	O
logic	O
changes	O
.	O
</s>
<s>
The	O
method	O
of	O
complementing	O
and	O
adding	O
one	O
can	O
be	O
sped	O
up	O
by	O
a	O
standard	O
carry	B-Algorithm
look-ahead	O
adder	O
circuit	O
;	O
the	O
LSB	O
towards	O
MSB	O
method	O
can	O
be	O
sped	O
up	O
by	O
a	O
similar	O
logic	O
transformation	O
.	O
</s>
<s>
When	O
turning	O
a	O
two's-complement	B-General_Concept
number	O
with	O
a	O
certain	O
number	O
of	O
bits	O
into	O
one	O
with	O
more	O
bits	O
(	O
e.g.	O
,	O
when	O
copying	O
from	O
a	O
one-byte	O
variable	O
to	O
a	O
two-byte	O
variable	O
)	O
,	O
the	O
most-significant	O
bit	O
must	O
be	O
repeated	O
in	O
all	O
the	O
extra	O
bits	O
.	O
</s>
<s>
Similarly	O
,	O
when	O
a	O
number	O
is	O
shifted	O
to	O
the	O
right	O
,	O
the	O
most-significant	O
bit	O
,	O
which	O
contains	O
the	O
sign	B-Algorithm
information	O
,	O
must	O
be	O
maintained	O
.	O
</s>
<s>
However	O
,	O
if	O
the	O
most-significant	O
bit	O
changes	O
from	O
0	O
to	O
1	O
(	O
and	O
vice	O
versa	O
)	O
,	O
overflow	B-Algorithm
is	O
said	O
to	O
occur	O
in	O
the	O
case	O
that	O
the	O
value	O
represents	O
a	O
signed	O
integer	O
.	O
</s>
<s>
With	O
only	O
one	O
exception	O
,	O
starting	O
with	O
any	O
number	O
in	O
two's-complement	B-General_Concept
representation	O
,	O
if	O
all	O
the	O
bits	O
are	O
flipped	O
and	O
1	O
added	O
,	O
the	O
two's-complement	B-General_Concept
representation	O
of	O
the	O
negative	O
of	O
that	O
number	O
is	O
obtained	O
.	O
</s>
<s>
Positive	O
12	O
becomes	O
negative	O
12	O
,	O
positive	O
5	O
becomes	O
negative	O
5	O
,	O
zero	O
becomes	O
zero( +overflow	O
)	O
,	O
etc	O
.	O
</s>
<s>
+	O
The	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
−128	O
results	O
in	O
the	O
same	O
8-bit	O
binary	O
number	O
.	O
</s>
<s>
Taking	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
the	O
minimum	O
number	O
in	O
the	O
range	O
will	O
not	O
have	O
the	O
desired	O
effect	O
of	O
negating	O
the	O
number	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
−128	O
in	O
an	O
eight-bit	O
system	O
is	O
−128	O
.	O
</s>
<s>
Although	O
the	O
expected	O
result	O
from	O
negating	O
−128	O
is	O
+128	O
,	O
there	O
is	O
no	O
representation	O
of	O
+128	O
with	O
an	O
eight-bit	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
system	I-General_Concept
and	O
thus	O
it	O
is	O
in	O
fact	O
impossible	O
to	O
represent	O
the	O
negation	O
.	O
</s>
<s>
Note	O
that	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
being	O
the	O
same	O
number	O
is	O
detected	O
as	O
an	O
overflow	B-Algorithm
condition	O
since	O
there	O
was	O
a	O
carry	B-Algorithm
into	O
but	O
not	O
out	O
of	O
the	O
most-significant	O
bit	O
.	O
</s>
<s>
The	O
presence	O
of	O
the	O
most	O
negative	O
number	O
can	O
lead	O
to	O
unexpected	O
programming	O
bugs	O
where	O
the	O
result	O
has	O
an	O
unexpected	O
sign	B-Algorithm
,	O
or	O
leads	O
to	O
an	O
unexpected	O
overflow	B-Algorithm
exception	O
,	O
or	O
leads	O
to	O
completely	O
strange	O
behaviors	O
.	O
</s>
<s>
the	O
unary	O
negation	O
operator	O
may	O
not	O
change	O
the	O
sign	B-Algorithm
of	O
a	O
nonzero	O
number	O
.	O
</s>
<s>
In	O
the	O
C	B-Language
and	O
C++	B-Language
programming	I-Language
languages	I-Language
,	O
the	O
above	O
behaviours	O
are	O
undefined	B-Language
and	O
not	O
only	O
may	O
they	O
return	O
strange	O
results	O
,	O
but	O
the	O
compiler	O
is	O
free	O
to	O
assume	O
that	O
the	O
programmer	O
has	O
ensured	O
that	O
undefined	B-Language
computations	O
never	O
happen	O
,	O
and	O
make	O
inferences	O
from	O
that	O
assumption	O
.	O
</s>
<s>
This	O
enables	O
a	O
number	O
of	O
optimizations	O
,	O
but	O
also	O
leads	O
to	O
a	O
number	O
of	O
strange	O
bugs	O
in	O
such	O
undefined	B-Language
programs	O
.	O
</s>
<s>
The	O
most	O
negative	O
number	O
in	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
is	O
sometimes	O
called	O
"	O
the	O
weird	O
number	O
"	O
,	O
because	O
it	O
is	O
the	O
only	O
exception	O
.	O
</s>
<s>
Although	O
the	O
number	O
is	O
an	O
exception	O
,	O
it	O
is	O
a	O
valid	O
number	O
in	O
regular	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
systems	I-General_Concept
.	O
</s>
<s>
All	O
arithmetic	O
operations	O
work	O
with	O
it	O
both	O
as	O
an	O
operand	O
and	O
(	O
unless	O
there	O
was	O
an	O
overflow	B-Algorithm
)	O
a	O
result	O
.	O
</s>
<s>
The	O
relationship	O
to	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
is	O
realised	O
by	O
noting	O
that	O
,	O
and	O
is	O
the	O
ones	O
 '	O
complement	B-Algorithm
of	O
.	O
</s>
<s>
In	O
this	O
subsection	O
,	O
decimal	B-Application
numbers	O
are	O
suffixed	O
with	O
a	O
decimal	B-Application
point	O
"	O
.	O
"	O
</s>
<s>
+	O
Two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
4	O
bit	O
integer	O
valuesTwo	O
's	O
complementDecimal	O
0111	O
7	O
.	O
</s>
<s>
Because	O
of	O
the	O
use	O
of	O
the	O
left-most	O
bit	O
as	O
the	O
sign	B-Algorithm
bit	I-Algorithm
,	O
the	O
absolute	O
value	O
of	O
the	O
most	O
negative	O
number	O
( |	O
8.	O
|	O
=	O
8	O
.	O
)	O
</s>
<s>
Negating	O
a	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
number	O
is	O
simple	O
:	O
Invert	O
all	O
the	O
bits	O
and	O
add	O
one	O
to	O
the	O
result	O
.	O
</s>
<s>
Therefore	O
,	O
1111	O
in	O
binary	O
must	O
represent	O
1	O
in	O
decimal	B-Application
.	O
</s>
<s>
Overflow	B-Algorithm
checks	O
still	O
must	O
exist	O
to	O
catch	O
operations	O
such	O
as	O
summing	O
0100	O
and	O
0100	O
.	O
</s>
<s>
The	O
system	O
therefore	O
allows	O
addition	O
of	O
negative	O
operands	O
without	O
a	O
subtraction	O
circuit	O
or	O
a	O
circuit	O
that	O
detects	O
the	O
sign	B-Algorithm
of	O
a	O
number	O
.	O
</s>
<s>
Moreover	O
,	O
that	O
addition	O
circuit	O
can	O
also	O
perform	O
subtraction	O
by	O
taking	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
a	O
number	O
(	O
see	O
below	O
)	O
,	O
which	O
only	O
requires	O
an	O
additional	O
cycle	O
or	O
its	O
own	O
adder	O
circuit	O
.	O
</s>
<s>
Adding	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
numbers	O
requires	O
no	O
special	O
processing	O
even	O
if	O
the	O
operands	O
have	O
opposite	O
signs	O
;	O
the	O
sign	B-Algorithm
of	O
the	O
result	O
is	O
determined	O
automatically	O
.	O
</s>
<s>
This	O
process	O
depends	O
upon	O
restricting	O
to	O
8	O
bits	O
of	O
precision	O
;	O
a	O
carry	B-Algorithm
to	O
the	O
(	O
nonexistent	O
)	O
9th	O
most	O
significant	O
bit	O
is	O
ignored	O
,	O
resulting	O
in	O
the	O
arithmetically	O
correct	O
result	O
of	O
1010	O
.	O
</s>
<s>
The	O
last	O
two	O
bits	O
of	O
the	O
carry	B-Algorithm
row	O
(	O
reading	O
right-to-left	O
)	O
contain	O
vital	O
information	O
:	O
whether	O
the	O
calculation	O
resulted	O
in	O
an	O
arithmetic	B-Algorithm
overflow	I-Algorithm
,	O
a	O
number	O
too	O
large	O
for	O
the	O
binary	O
system	O
to	O
represent	O
(	O
in	O
this	O
case	O
greater	O
than	O
8	O
bits	O
)	O
.	O
</s>
<s>
An	O
overflow	B-Algorithm
condition	O
exists	O
when	O
these	O
last	O
two	O
bits	O
are	O
different	O
from	O
one	O
another	O
.	O
</s>
<s>
As	O
mentioned	O
above	O
,	O
the	O
sign	B-Algorithm
of	O
the	O
number	O
is	O
encoded	O
in	O
the	O
MSB	O
of	O
the	O
result	O
.	O
</s>
<s>
In	O
other	O
terms	O
,	O
if	O
the	O
left	O
two	O
carry	B-Algorithm
bits	I-Algorithm
(	O
the	O
ones	O
on	O
the	O
far	O
left	O
of	O
the	O
top	O
row	O
in	O
these	O
examples	O
)	O
are	O
both	O
1s	O
or	O
both	O
0s	O
,	O
the	O
result	O
is	O
valid	O
;	O
if	O
the	O
left	O
two	O
carry	B-Algorithm
bits	I-Algorithm
are	O
"	O
1	O
0	O
"	O
or	O
"	O
0	O
1	O
"	O
,	O
a	O
sign	B-Algorithm
overflow	B-Algorithm
has	O
occurred	O
.	O
</s>
<s>
Conveniently	O
,	O
an	O
XOR	B-Application
operation	O
on	O
these	O
two	O
bits	O
can	O
quickly	O
determine	O
if	O
an	O
overflow	B-Algorithm
condition	O
exists	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
far	O
left	O
two	O
(	O
MSB	O
)	O
carry	B-Algorithm
bits	I-Algorithm
are	O
"	O
01	O
"	O
,	O
which	O
means	O
there	O
was	O
a	O
two's-complement	B-General_Concept
addition	O
overflow	B-Algorithm
.	O
</s>
<s>
In	O
general	O
,	O
any	O
two	O
-bit	O
numbers	O
may	O
be	O
added	O
without	O
overflow	B-Algorithm
,	O
by	O
first	O
sign-extending	O
both	O
of	O
them	O
to	O
bits	O
,	O
and	O
then	O
adding	O
as	O
above	O
.	O
</s>
<s>
The	O
bits	O
result	O
is	O
large	O
enough	O
to	O
represent	O
any	O
possible	O
sum	O
(	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
can	O
represent	O
values	O
in	O
the	O
range	O
16	O
to	O
15	O
)	O
so	O
overflow	B-Algorithm
will	O
never	O
occur	O
.	O
</s>
<s>
It	O
is	O
then	O
possible	O
,	O
if	O
desired	O
,	O
to	O
'	O
truncate	O
 '	O
the	O
result	O
back	O
to	O
bits	O
while	O
preserving	O
the	O
value	O
if	O
and	O
only	O
if	O
the	O
discarded	O
bit	O
is	O
a	O
proper	O
sign	B-Algorithm
extension	O
of	O
the	O
retained	O
result	O
bits	O
.	O
</s>
<s>
This	O
provides	O
another	O
method	O
of	O
detecting	O
overflowwhich	O
is	O
equivalent	O
to	O
the	O
method	O
of	O
comparing	O
the	O
carry	B-Algorithm
bitsbut	O
which	O
may	O
be	O
easier	O
to	O
implement	O
in	O
some	O
situations	O
,	O
because	O
it	O
does	O
not	O
require	O
access	O
to	O
the	O
internals	O
of	O
the	O
addition	O
.	O
</s>
<s>
Computers	O
usually	O
use	O
the	O
method	B-Algorithm
of	I-Algorithm
complements	I-Algorithm
to	O
implement	O
subtraction	O
.	O
</s>
<s>
Using	O
complements	O
for	O
subtraction	O
is	O
closely	O
related	O
to	O
using	O
complements	O
for	O
representing	O
negative	O
numbers	O
,	O
since	O
the	O
combination	O
allows	O
all	O
signs	O
of	O
operands	O
and	O
results	O
;	O
direct	O
subtraction	O
works	O
with	O
two's-complement	B-General_Concept
numbers	O
as	O
well	O
.	O
</s>
<s>
Like	O
addition	O
,	O
the	O
advantage	O
of	O
using	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
is	O
the	O
elimination	O
of	O
examining	O
the	O
signs	O
of	O
the	O
operands	O
to	O
determine	O
whether	O
addition	O
or	O
subtraction	O
is	O
needed	O
.	O
</s>
<s>
For	O
example	O
,	O
subtracting	O
−5	O
from	O
15	O
is	O
really	O
adding	O
5	O
to	O
15	O
,	O
but	O
this	O
is	O
hidden	O
by	O
the	O
two's-complement	B-General_Concept
representation	O
:	O
</s>
<s>
Overflow	B-Algorithm
is	O
detected	O
the	O
same	O
way	O
as	O
for	O
addition	O
,	O
by	O
examining	O
the	O
two	O
leftmost	O
(	O
most	O
significant	O
)	O
bits	O
of	O
the	O
borrows	O
;	O
overflow	B-Algorithm
has	O
occurred	O
if	O
they	O
are	O
different	O
.	O
</s>
<s>
As	O
for	O
addition	O
,	O
overflow	B-Algorithm
in	O
subtraction	O
may	O
be	O
avoided	O
(	O
or	O
detected	O
after	O
the	O
operation	O
)	O
by	O
first	O
sign-extending	O
both	O
inputs	O
by	O
an	O
extra	O
bit	O
.	O
</s>
<s>
If	O
the	O
precision	O
of	O
the	O
two	O
operands	O
using	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
is	O
doubled	O
before	O
the	O
multiplication	O
,	O
direct	O
multiplication	O
(	O
discarding	O
any	O
excess	O
bits	O
beyond	O
that	O
precision	O
)	O
will	O
provide	O
the	O
correct	O
result	O
.	O
</s>
<s>
Some	O
multiplication	O
algorithms	O
are	O
designed	O
for	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
,	O
notably	O
Booth	O
's	O
multiplication	O
algorithm	O
.	O
</s>
<s>
Methods	O
for	O
multiplying	O
sign-magnitude	O
numbers	O
do	O
n't	O
work	O
with	O
two's-complement	B-General_Concept
numbers	O
without	O
adaptation	O
.	O
</s>
<s>
Two	O
methods	O
for	O
adapting	O
algorithms	O
to	O
handle	O
two's-complement	B-General_Concept
numbers	O
are	O
common	O
:	O
</s>
<s>
If	O
so	O
,	O
negate	O
(	O
i.e.	O
,	O
take	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
)	O
both	O
operands	O
before	O
multiplying	O
.	O
</s>
<s>
Because	O
both	O
operands	O
are	O
negated	O
,	O
the	O
result	O
will	O
still	O
have	O
the	O
correct	O
sign	B-Algorithm
.	O
</s>
<s>
Subtract	O
the	O
partial	O
product	O
resulting	O
from	O
the	O
MSB	O
(	O
pseudo	O
sign	B-Algorithm
bit	I-Algorithm
)	O
instead	O
of	O
adding	O
it	O
like	O
the	O
other	O
partial	O
products	O
.	O
</s>
<s>
This	O
method	O
requires	O
the	O
multiplicand	O
's	O
sign	B-Algorithm
bit	I-Algorithm
to	O
be	O
extended	O
by	O
one	O
position	O
,	O
being	O
preserved	O
during	O
the	O
shift	O
right	O
actions	O
.	O
</s>
<s>
In	O
the	O
following	O
example	O
,	O
again	O
multiplying	O
6	O
by	O
5	O
,	O
the	O
two	O
registers	O
and	O
the	O
extended	O
sign	B-Algorithm
bit	I-Algorithm
are	O
separated	O
by	O
"	O
|	O
"	O
:	O
</s>
<s>
Comparison	O
is	O
often	O
implemented	O
with	O
a	O
dummy	O
subtraction	O
,	O
where	O
the	O
flags	O
in	O
the	O
computer	O
's	O
status	B-General_Concept
register	I-General_Concept
are	O
checked	O
,	O
but	O
the	O
main	O
result	O
is	O
ignored	O
.	O
</s>
<s>
The	O
zero	B-Algorithm
flag	I-Algorithm
indicates	O
if	O
two	O
values	O
compared	O
equal	O
.	O
</s>
<s>
If	O
the	O
exclusive-or	O
of	O
the	O
sign	B-Algorithm
and	O
overflow	B-Algorithm
flags	I-Algorithm
is	O
1	O
,	O
the	O
subtraction	O
result	O
was	O
less	O
than	O
zero	O
,	O
otherwise	O
the	O
result	O
was	O
zero	O
or	O
greater	O
.	O
</s>
<s>
These	O
checks	O
are	O
often	O
implemented	O
in	O
computers	O
in	O
conditional	B-General_Concept
branch	I-General_Concept
instructions	O
.	O
</s>
<s>
For	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
values	O
,	O
the	O
meaning	O
of	O
the	O
most	O
significant	O
bit	O
is	O
reversed	O
(	O
i.e.	O
</s>
<s>
The	O
following	O
algorithm	O
(	O
for	O
an	O
-bit	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
architecture	O
)	O
sets	O
the	O
result	O
register	O
R	O
to	O
−1	O
if	O
A	O
< B, to +1 if A >	O
B	O
,	O
and	O
to	O
0	O
if	O
A	O
and	O
B	O
are	O
equal	O
:	O
</s>
<s>
In	O
a	O
classic	O
HAKMEM	O
published	O
by	O
the	O
MIT	O
AI	O
Lab	O
in	O
1972	O
,	O
Bill	O
Gosper	O
noted	O
that	O
whether	O
or	O
not	O
a	O
machine	O
's	O
internal	O
representation	O
was	O
two's-complement	B-General_Concept
could	O
be	O
determined	O
by	O
summing	O
the	O
successive	O
powers	O
of	O
two	O
.	O
</s>
<s>
In	O
a	O
flight	O
of	O
fancy	O
,	O
he	O
noted	O
that	O
the	O
result	O
of	O
doing	O
this	O
algebraically	O
indicated	O
that	O
"	O
algebra	O
is	O
run	O
on	O
a	O
machine	O
(	O
the	O
universe	O
)	O
which	O
is	O
two's-complement	B-General_Concept
.	O
"	O
</s>
<s>
It	O
is	O
meaningful	O
either	O
as	O
part	O
of	O
a	O
two's-complement	B-General_Concept
notation	O
for	O
all	O
integers	O
,	O
as	O
a	O
typical	O
2-adic	O
number	O
,	O
or	O
even	O
as	O
one	O
of	O
the	O
generalized	O
sums	O
defined	O
for	O
the	O
divergent	B-Algorithm
series	I-Algorithm
of	O
real	O
numbers	O
1	O
+	O
2	O
+	O
4	O
+	O
8	O
+	O
···	O
.	O
</s>
<s>
Digital	O
arithmetic	O
circuits	O
,	O
idealized	O
to	O
operate	O
with	O
infinite	O
(	O
extending	O
to	O
positive	O
powers	O
of	O
2	O
)	O
bit	O
strings	O
,	O
produce	O
2-adic	O
addition	O
and	O
multiplication	O
compatible	O
with	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
representation	O
.	O
</s>
<s>
To	O
convert	O
a	O
number	O
with	O
a	O
fractional	O
part	O
,	O
such	O
as	O
.0101	O
,	O
one	O
must	O
convert	O
starting	O
from	O
right	O
to	O
left	O
the	O
1s	O
to	O
decimal	B-Application
as	O
in	O
a	O
normal	O
conversion	O
.	O
</s>
<s>
In	O
this	O
example	O
0101	O
is	O
equal	O
to	O
5	O
in	O
decimal	B-Application
.	O
</s>
<s>
Having	O
already	O
calculated	O
the	O
decimal	B-Application
value	O
as	O
mentioned	O
above	O
,	O
only	O
the	O
denominator	O
of	O
the	O
LSB	O
(	O
LSB	O
=	O
starting	O
from	O
right	O
)	O
is	O
used	O
.	O
</s>
<s>
Hence	O
,	O
instead	O
of	O
calculating	O
the	O
decimal	B-Application
value	O
for	O
0110	O
,	O
we	O
calculate	O
the	O
value	O
011	O
,	O
which	O
is	O
3	O
in	O
decimal	B-Application
(	O
by	O
leaving	O
the	O
0	O
in	O
the	O
end	O
,	O
the	O
result	O
would	O
have	O
been	O
6	O
,	O
together	O
with	O
the	O
denominator	O
24	O
=	O
16	O
,	O
which	O
reduces	O
to	O
3/8	O
)	O
.	O
</s>
