Searching \ for 'Question re: 2's complement' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=complement
Search entire site for: 'Question re: 2's complement'.

Truncated match.
'Question re: 2's complement'
1998\06\05@151010 by

I keep running across references to 2's complements, does anyone have a
relatively simple explanation?

Thanks, Barry

Barry,
2's complement is basically using the MSB as a sign bit.

Ex. Here's a table that may help:

DECIMAL #       2's complement
-------------------     -------------------------
+7           0000 0111
+6           0000 0110
+5           0000 0101
+4           0000 0100
+3           0000 0011
+2           0000 0010
+1           0000 0001
0          0000 0000
-1          1111 1111
-2          1111 1110
-3          1111 1101
-4          1111 1100
-5          1111 1011
-6          1111 1010
-7          1111 1001

Best regards,
Steven

***************************************************
Steven Kosmerchock
Engineering Technician
CELWAVE
Phoenix    Arizona    USA
email:    steve.kosmerchockcelwave.com
***************************************************
www.geocities.com/researchtriangle/lab/6584
***************************************************
---- PICLIST(a)MITVMA.MIT.EDU's Message ----

I keep running across references to 2's complements, does anyone have a
relatively simple explanation?

Thanks, Barry

I keep running across references to 2's complements, does anyone have a
relatively simple explanation?

Twos complement is one way to represent signed numbers in a digital system
in such a way that the same hardware logic can do arithmetic without having
to understand the sign-ed-ness.

To get the twos complement of a number, invert all the bits and add 1.
(most procesors will have a "negate" instruction to do this.)

Say you have +1 and -1.  If you're a human, and you want to add them
together, you note that one is negative and do a subtract instead.  However,
that's a bit much to ask of combinatorial hardware logic.

So, instead, we let +1 be 00000001, and -1 = (11111110 + 1) = 11111111.
If you add these together, you'd get 100000000, or simply 00000000 if
you neglect the carry bit.  Hey - exactly what you wanted...

An alternative technology was "ones complement", where you simply invert
all the bits.  In this case, to make your math come out right, you add
the carry bit back into the result.  So 00000001 + 11111110 (-1) =
11111111, which is "-0" (oops - that's one reason ones complement died
out.  Having two values of zero is confusing.)

BillW

Barry Cooper <PICLISTMITVMA.MIT.EDU> wrote:

> I keep running across references to 2's complements, does
> anyone have a relatively simple explanation?

Barry:

Two's-complement is a way of representing negative numbers; in
two's-complement representation, a number is converted from
positive to negative by inverting all its bits and adding 1.
Using 8-bit numbers, for example, the numbers look like this:

Decimal    Binary
-------   --------
127  =  01111111
2  =  00000010
1  =  00000001
0  =  00000000
-1  =  11111111
-2  =  11111110
-127  =  10000001
-128  =  10000000

A year or so ago, I wrote a long essay explaining why two's-
complement is used, rather than some other representation; if
I get a minute this weekend, I'll post it on my web page.

-Andy

=== Andrew Warren - fastfwdix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499 (personal)

The 2's complement (2C from here on) of any value is the value that,
if added to another value will have the same result as subtracting the
original value.  This is very important in the PIC work because the
subtract instruction does not work the way you would expect (it
subtracts W from the value), so you can get the same result by adding
the 2C, since the add instruction DOES work the way you'd expect.  In
decimal, the value that can be added to get the same result as
subtracting another value can be found easily by simply prefixing the
number with a -ve sign.  In binary work with a uP or uC, this is not
quite so easy.  If you know the value at compile time you can do this
and the compiler or assembler will do the conversion for you, but if
you only know the value at run time, you must get the 2C of the value
to be subtracted from W, and add it to the W register instead.

The 2C can be found with just 2 simple instructions by complementing
the value to be subtracted and adding 1, or incrementing once.

For example, to subtract 2 from the W register, first complement the
register containing the value 2:

00000010 becomes 11111101

Next add 1 or increment this value:

11111101 becomes 11111110

If you now add this value to W, the result will be the same as having
subtracted 2 in the first place.  Note that if the original value of W
is more than 1, then the value in W will 'roll over' with the most
significant bit of the result being lost (we don't care about it
anyway).  The 2C of a value is always dependent on the width of the
registers being used for the calculation.  The way you calculate it
will be the same, but the 2C value will be different.  For example,
the 8 bit 2C of 2 is 254, while the 16 bit 2C of 2 is 65534.

HTH - Martin R. Green

Subject: Question re: 2's complement
Author:  pic microcontroller discussion list <PICLISTMITVMA.MIT.EDU> at
Internet
Date:    6/5/98 3:10 PM

I keep running across references to 2's complements, does anyone have a
relatively simple explanation?

Thanks, Barry

2's complement and 1's complement are how negative numbers or results are
displayed.  For example: (8 bit numbers)

-5 =
(in 2's complement)
1 0000 0000-0000 0101 = 1111 1011

(in 1's complement)
1111 1111-0000 0101 = 1111 1010

1's complement is found by inverting all bits.  Add 1 to this result for
2's complement.

----------
> From: Barry Cooper <Barry_CooperBC.SYMPATICO.CA>
> To: PICLISTMITVMA.MIT.EDU
> Subject: Question re: 2's complement
> Date: Friday, June 05, 1998 1:10 PM
>
>   I keep running across references to 2's complements, does anyone have a
> relatively simple explanation?
>
>         Thanks, Barry

Barry,
2's complements are simply the "not" of the value + 1

For example:

movlw   0xFF            ; Load -1 into W
xorwf   value,f ; Form  1's complement
incf    value           ; Form 2's complement

-Dan   W6DFWqsl.net

On Fri, Jun 05, 1998 at 03:10:10PM -0400, Barry Cooper wrote:
>   I keep running across references to 2's complements, does anyone have a
> relatively simple explanation?

A 2's complement is a variation on 1's complement; the 1's complement of
a binary number is just the number with all bits inverted, e.g.

01101010

has a 1's complement of

10010101

where each bit is individually toggled.

Now a 2's complement is a 1's complement plus 1, so the 2's complement of the
first number is

10010110

Note that a 1 value has been added to the number (causing overflow into the
second bit).

The value of 2's complement derives from the fact that the 2's complement of zer
o
is also zero (try it out, ignore the overflow from the high order bit).
This makes it a good operation to use for negating a binary number, and is the
representation used by 99.9% of all computer architectures for representing sign
ed
numbers (where negative zero equals zero).

--
Clyde Smith-Stubbs               |            HI-TECH Software
Email: clydehtsoft.com          |          Phone            Fax
WWW:   http://www.htsoft.com/    | USA: (408) 490 2885  (408) 490 2885
PGP:   finger clydehtsoft.com   | AUS: +61 7 3354 2411 +61 7 3354 2422
---------------------------------------------------------------------------
HI-TECH C: compiling the real world.

D. F. Welch wrote:
>
> Barry,
>    2's complements are simply the "not" of the value + 1
>
>         For example:
>
>         movlw   0xFF            ; Load -1 into W
>         xorwf   value,f ; Form  1's complement
>         incf    value           ; Form 2's complement

That is one brute force way to do it. A better way is this:

COMF    value,f
INCF    value

And if you want to complement W:

XORLW   0xff

I personally prefer Payson's solution for complementing W (which is
suitable for the 12-bit cores):

ADDWF   some_register,W         ;new W = old W + x
SUBWF   some_register,W         ;W = x - (old W + x) = -W

Another solution (that I believe Payson also posted) was to do this:

SUBWF   known_zero,W            ;W = 0 - W = -W

Where 'know_zero' is a file register that is equal to zero (this is very
handy for other stuff too).

Scott

At 12:41 05/06/98 -0700, you wrote:
>Barry,
>2's complement is basically using the MSB as a sign bit.
>
>Ex. Here's a table that may help:

Your table is right but it's not solely "using the msb as a sign bit".

If you use the MSB solely as a sign bit you would be using sign-magnitude
representation. :-)

Regards

WWW WW WWW   White Horse Design
WWWWWWWWWW   +44-385-970009 (Mobile/SMS), +44-118-962-8913/4 (voice/fax)
WWWW  WWWW   whdzetnet.co.uk, http://www.users.zetnet.co.uk/whd
---
Developers of GPS satellite-based tracking systems for vehicles/helicopters

At 13:19 05/06/98 -0700, you wrote:
>Barry,
>   2's complements are simply the "not" of the value + 1

A tip:

If you want to convert a binary value to it's 2's complement start at the
right hand end and write down the bits verbatim *until* you get to the
first bit with value "1" then invert it and every following bit as you
proceed left.

Regards

WWW WW WWW   White Horse Design
WWWWWWWWWW   +44-385-970009 (Mobile/SMS), +44-118-962-8913/4 (voice/fax)
WWWW  WWWW   whdzetnet.co.uk, http://www.users.zetnet.co.uk/whd
---
Developers of GPS satellite-based tracking systems for vehicles/helicopters

-----Original Message-----
From: William Chops Westfield <billwCISCO.COM>
To: PICLISTMITVMA.MIT.EDU <PICLISTMITVMA.MIT.EDU>
Date: Friday, June 05, 1998 2:29 PM
Subject: Re: Question re: 2's complement

>    I keep running across references to 2's complements, does anyone have a
>    relatively simple explanation?

------------------snipped to keep it short----------------------

>An alternative technology was "ones complement", where you simply invert
>all the bits.  In this case, to make your math come out right, you add
>the carry bit back into the result.  So 00000001 + 11111110 (-1) =
>11111111, which is "-0" (oops - that's one reason ones complement died
>out.  Having two values of zero is confusing.)

Right on, Bill -

The alternate technology that Bill mentions was the original method of
representing integer numbers in the computer. The REAL problem with ones
complement was the flaw of the representation of the number Zero. You see,
Zero had TWO representations - one Positive - one Negative. Both numbers
being Zero - both numbers being opposite.

Here's where the problem comes in -

In writing a program, many program branches (conditional jumps) looked at
conditions in registers. The choice of conditions included 'Jump if Zero',
'Jump if Positive', Jump if Negative, and 'Jump if NOT Zero' (do you see

Programmers could (and did) make mistakes in coding. Logical errors
concerning the way they EXPECTED the computer to react  to conditional
branch statements - when the register in question, by chance, contained
Negative Zero. Every Programmer  got 'burned' by this problem at one time or
another. This all too common bug caused the computer manufacturers to
re-design the hardware to 'twos complement'. Problem solved.