Searching \ for '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: '2'S COMPLEMENT'.

Truncated match.
'2's complement'
1998\06\05@172857 by

On Fri, 5 Jun 1998 15:10:10 -0400 Barry Cooper
<Barry_CooperBC.SYMPATICO.CA> writes:
>  I keep running across references to 2's complements, does anyone
>have a
>relatively simple explanation?
>
>        Thanks, Barry
>

2's complement notation is one way of writing an integer
(positive and negative).  It is about the same as driving your car
backwards and seeing what happens to the odometer.  If you drive
represents negative 1.  If we add 00001 to it, we get 0, which is what we
expect.  In a byte, we "drive backwards" from 00 to FF for negative 1, FE
for negative 2, etc.
In decimal, we can calculate a nine's complement of a number by
subtracting the number from 99999 (or however many digits we're dealing
with).  This works out to be 9 minus the corresponding digit.  For
example, the 9's complement of 1234 is

9-1=8
9-2=7
9-3=6
9-4=5

So, the 9's complement of 1234 is 8765.  If we add 1 to this, we
get the TENS COMPLEMENT, in this case 8766.  This corresponds to -1234 in
tens complement notation.  Let's try it out!

We get 1236+8766=(1)0002                We throw out the 1 that
overflows into the carry.

We do the same thing in binary...  We first find the one's
complement, then add 1 to the result to get the two's complement.  In
fact, the instruction on the PDP-8 to get the two's complement was CIA
(for complement and increment accumulator).
The 1's complement is interesting.  We just subtract the number
we're interested in from 11111111 (or however many bits we're using).
Turns out that for each bit, there are only two choices (this is
binary!).  If we subtract 0 from the 1, we get 1.  If we subtract 1 from
the 1, we get zero.  We can do the subtraction (to get the 1's
complement) really easily by just inverting all the bits (changing 1's to
0's and vice versa).  This is the COMPLEMENT instruction.  if we then add
1, we get the two's complement, which is the negative of the number we
started with (in 2's complement notation).
A few other interesting things about 2's complement...

The most significant bit is a 1 if the number is negative.  Thus
it is called the sign bit.

Our normal binary scaling for an 8 bit number, starting with the
least significant bit, is 1, 2, 4, 8, 16, 32, 64, 128.
In two's complement form, the scaling is 1, 2, 4, 8, 16, 32, 64,
-128.  So, if you've got some 2's complement number, it's easy to convert
to decimal by using this scaling.

Finally, a byte is a byte, and it can represent a bunch of
different things.  It's up to the programmer to decide what format the
data is in.  For example, 0x30 could represent the number 0 (in ascii),
or could represent the number 48 (decimal) in unsigned binary or 2's
complement binary, or could represent the number 30 (decimal) in packed
BCD.  It's up to the programmer to keep track of data types!

Harold

_____________________________________________________________________
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

On Sat, 6 Jun 1998 06:57:14 +0100 White Horse Design <whdZETNET.CO.UK>
writes:

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

As I used to explain to my students, "The ORIGINAL meaning of
S&M"...

Harold

_____________________________________________________________________
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

'2'S COMPLEMENT'
1999\05\05@145256 by
Hi,
Can anybody tell me how to do a 2's complement of hex# stored in the w
register.  Thanks
Aidi Moubhij

aidimjr.com

> Hi,
> Can anybody tell me how to do a 2's complement of hex# stored in the w
> register.  Thanks
> Aidi Moubhij
>
> aidimjr.com
>
Now if you mean that you want the 2's complement of w, you can:

sublw   0;

But if you mean that w contains an ASCII hexidecimal encoded nibble,
well, that's another story.

Dave Reinagel
davercisco.com

Aidi Moubhij wrote:
>
> Hi,
> Can anybody tell me how to do a 2's complement of hex# stored in the w
> register.  Thanks
> Aidi Moubhij
>
> aidimjr.com

Usually it looks like(for X true binary of course):

COMF    X,F
INCF    X,F

WBR Dmitry.

Exclusive or it with itself and then add 1

>Hi,
>Can anybody tell me how to do a 2's complement of hex# stored in the w
>register.  Thanks
>Aidi Moubhij
>
>aidimjr.com
>

Subject: Re: 2'S COMPLEMENT

> Exclusive or it with itself and then add 1

Wouldn't that result always be 1?

Yes it would.  Sorry.  I don't know what I was thinking.
I meant to say compliment it and add 1.  Thanks for catching
my flub.

Regards,

Jim

>
>Subject: Re: 2'S COMPLEMENT
>
>
>> Exclusive or it with itself and then add 1
>
>Wouldn't that result always be 1?
>
>

Hi,
I think the idea is bogus: XOR with itself produces always zero. I guess
COMF it, and then INC it.
Imre

On Wed, 5 May 1999, Mr and Mrs James Paul wrote:

{Quote hidden}

Thank you all for your inputs.  What I did is I COMF the w reg. and I added
0x01 to w and that worked for me.  Thanks again.

At 01:37 PM 5/6/99 +0200, you wrote:
{Quote hidden}

Aidi Moubhij

aidimjr.com

|Can anybody tell me how to do a 2's complement of hex# stored in the w
|register.  Thanks

On the 14-bit parts, you may use 'sublw 0'.  This is, obviously, the
fastest way to do it.

On the 12-bit parts, you may use:

subwf   reg,w

If 'reg' were to change between the two instructions, the value in
W would be corrupted.  Given that none of the 12-bit parts support
interrupts, though, you should be okay provided 'reg' is not an I/O
port or similar register.

Hmm... and here I thought the fastest way to increment W without a
spare register was:

subwf   reg,w
incf    reg,f
subwf   reg,w
decf    reg,f

but there's a three-cycle way:

xorlw   255