Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   1's complement and 2's complement (http://www.velocityreviews.com/forums/t455807-1s-complement-and-2s-complement.html)

 sarathy 08-01-2006 04:00 AM

1's complement and 2's complement

Hi all,
I have a few doubts in the 1's and 2's complement
representation. Generally negative numbers can be represented using
either 1's complement or 2's complement representation.

1's complement ---> reverse all the bits
2's complement ---> reverse all the bits + 1

i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
But when a number and its complement are added the result must be a
zero right ??
But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
Should'nt we be getting a zero as result ???

2's complement of 2 ( 0000 0010 ) is -2 ( 1111 1110 )
Adding we get , 0000 0010 + 1111 1110 = 0000 0000 ==> [ OK]

Does this complement representation have anything to do with the C's ~
[1's complement] operator ?
Is this representation architecture dependent or compiler dependent ?

Regards,
Sarathy

 SM Ryan 08-01-2006 04:11 AM

Re: 1's complement and 2's complement

"sarathy" <sps.sarathy@gmail.com> wrote:
# Hi all,
# I have a few doubts in the 1's and 2's complement
# representation. Generally negative numbers can be represented using
# either 1's complement or 2's complement representation.
#
# 1's complement ---> reverse all the bits
# 2's complement ---> reverse all the bits + 1
#
# i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
# But when a number and its complement are added the result must be a
# zero right ??
# But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]

On a ones complement machine, ~0 is 0, called a negative zero.
Some CPUs convert -0 to +0, some don't. -0 = +0, but also
sometimes -0 < +0.

# Does this complement representation have anything to do with the C's ~
# [1's complement] operator ?

On ones complement CPUs, -x = ~x. Whether this was signficant when C
was first created, you would have to ask Ritchie.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So....that would make Bethany part black?

 red floyd 08-01-2006 04:12 AM

Re: 1's complement and 2's complement

sarathy wrote:
> Hi all,
> I have a few doubts in the 1's and 2's complement
> representation. Generally negative numbers can be represented using
> either 1's complement or 2's complement representation.
>
> 1's complement ---> reverse all the bits
> 2's complement ---> reverse all the bits + 1
>
> i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
> But when a number and its complement are added the result must be a
> zero right ??
> But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
> Should'nt we be getting a zero as result ???

In a pure 1's complement notation, you have the concept of "minus zero",
which is the ones complement of 0.

So your result is "minus zero".

 Roy Smith 08-01-2006 04:55 AM

Re: 1's complement and 2's complement

"sarathy" <sps.sarathy@gmail.com> wrote:

> Hi all,
> I have a few doubts in the 1's and 2's complement
> representation. Generally negative numbers can be represented using
> either 1's complement or 2's complement representation.
>
> 1's complement ---> reverse all the bits
> 2's complement ---> reverse all the bits + 1
>
> i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
> But when a number and its complement are added the result must be a
> zero right ??
> But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
> Should'nt we be getting a zero as result ???

You did. In 1's complement, there is no unique representation for zero.
All 0's and all 1's are both equal to zero.

> Does this complement representation have anything to do with the C's ~
> [1's complement] operator ?

Not really

> Is this representation architecture dependent or compiler dependent ?

Whether you are doing 1's complement or 2's complement math depends on the
underlying hardware. That being said, I haven't seen a 1's complement
machine in a couple of eons. It's pretty much an obsolete concept as far
as hardware design goes.

 Bill Pursell 08-01-2006 05:33 AM

Re: 1's complement and 2's complement

Roy Smith wrote:
> "sarathy" <sps.sarathy@gmail.com> wrote:

> > 1's complement ---> reverse all the bits
> > 2's complement ---> reverse all the bits + 1
> >
> > i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
> > But when a number and its complement are added the result must be a
> > zero right ??
> > But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
> > Should'nt we be getting a zero as result ???

>
> You did. In 1's complement, there is no unique representation for zero.
> All 0's and all 1's are both equal to zero.

No, in 8-bit ones complement, zero is represented as either
0x00 or 0x80. 0xff is -127.

The problem is that addition with one's complement is
not the same as addition with 2's complement. To
add two numbers, you have to perform different operations
depending on the signedness of the numbers, and that
is why 2's complement is preferred.

 Michael Mair 08-01-2006 05:47 AM

Re: 1's complement and 2's complement

Bill Pursell schrieb:
> Roy Smith wrote:
>> "sarathy" <sps.sarathy@gmail.com> wrote:

>
>>>1's complement ---> reverse all the bits
>>>2's complement ---> reverse all the bits + 1
>>>
>>>i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
>>>But when a number and its complement are added the result must be a
>>>zero right ??
>>>But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
>>>Should'nt we be getting a zero as result ???

>>
>>You did. In 1's complement, there is no unique representation for zero.
>>All 0's and all 1's are both equal to zero.

>
> No, in 8-bit ones complement, zero is represented as either
> 0x00 or 0x80. 0xff is -127.

8-bit ones complement? You mean sign and magnitude.

There is only one kind of ones complement for C.

C99, 62.6.2#2: "
— the corresponding value with sign bit 0 is negated (sign and magnitude);
— the sign bit has the value -(2N) (two’s complement);
— the sign bit has the value -(2N - 1) (one’s complement).
"

> The problem is that addition with one's complement is
> not the same as addition with 2's complement. To
> add two numbers, you have to perform different operations
> depending on the signedness of the numbers, and that
> is why 2's complement is preferred.

And one's complement and sign-magnitude have the advantage
of symmetric value range and others. There have been enough

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

 sarathy 08-01-2006 05:50 AM

Re: 1's complement and 2's complement

Hi,
I guess -0 ==> 1111 1111 is correct in 1's complement notation.
-0 ==> 1000 0000 is in signed magnitude notation.

Please verify and revert back in case.

Rgrds,
Sarathy

Bill Pursell wrote:
> Roy Smith wrote:
> > In article <1154404859.048278.154030@h48g2000cwc.googlegroups .com>,
> > "sarathy" <sps.sarathy@gmail.com> wrote:

>
> > > 1's complement ---> reverse all the bits
> > > 2's complement ---> reverse all the bits + 1
> > >
> > > i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
> > > But when a number and its complement are added the result must be a
> > > zero right ??
> > > But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
> > > Should'nt we be getting a zero as result ???

> >
> > You did. In 1's complement, there is no unique representation for zero.
> > All 0's and all 1's are both equal to zero.

>
> No, in 8-bit ones complement, zero is represented as either
> 0x00 or 0x80. 0xff is -127.
>
> The problem is that addition with one's complement is
> not the same as addition with 2's complement. To
> add two numbers, you have to perform different operations
> depending on the signedness of the numbers, and that
> is why 2's complement is preferred.

 Bill Pursell 08-01-2006 05:57 AM

Re: 1's complement and 2's complement

Michael Mair wrote:
> Bill Pursell schrieb:
> > Roy Smith wrote:
> >> "sarathy" <sps.sarathy@gmail.com> wrote:

> >
> >>>1's complement ---> reverse all the bits
> >>>2's complement ---> reverse all the bits + 1
> >>>
> >>>i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
> >>>But when a number and its complement are added the result must be a
> >>>zero right ??
> >>>But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
> >>>Should'nt we be getting a zero as result ???
> >>
> >>You did. In 1's complement, there is no unique representation for zero.
> >>All 0's and all 1's are both equal to zero.

> >
> > No, in 8-bit ones complement, zero is represented as either
> > 0x00 or 0x80. 0xff is -127.

>
> 8-bit ones complement? You mean sign and magnitude.

Oops. Of course.

> of symmetric value range and others. There have been enough

Agreed!!

--
Bill

 Frederick Gotham 08-01-2006 02:24 PM

Re: 1's complement and 2's complement

sarathy posted:

> Please verify and revert back in case.

*Cringe*

I'd love to bludgeon to death the next person I hear utter that phrase.

--

Frederick Gotham

 Richard Heathfield 08-01-2006 04:03 PM

Re: 1's complement and 2's complement

Frederick Gotham said:

> sarathy posted:
>
>> Please verify and revert back in case.

>
>
> *Cringe*
>
> I'd love to bludgeon to death the next person I hear utter that phrase.

Are you sure about that? Please verify and revert back in case.

(And now if you'll excuse me, I have a plane to catch. Or a starship. Or
something... TAXI!)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

All times are GMT. The time now is 11:20 AM.