Velocity Reviews > Java > 2's complement

# 2's complement

eeh
Guest
Posts: n/a

 05-30-2005
Hi,

Could anyone give me sample code to calculate 2's complement of an
integer which is n (n<16) bits length?

Thanks!

Andrew Thompson
Guest
Posts: n/a

 05-30-2005
On 29 May 2005 20:57:02 -0700, eeh wrote:

> Could anyone give me sample code to calculate 2's complement of an
> integer which is n (n<16) bits length?

Can you..
b) Consult your lecturer or tutor?
d) Offer cash?

<http://www.physci.org/codes/javafaq.jsp#homework>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane

Lucy
Guest
Posts: n/a

 05-30-2005

"eeh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> Could anyone give me sample code to calculate 2's complement of an
> integer which is n (n<16) bits length?
>
> Thanks!

int a = 5;
int b = ~a;

Patricia Shanahan
Guest
Posts: n/a

 05-30-2005
Lucy wrote:

> "eeh" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
>>> Hi,
>>>
>>> Could anyone give me sample code to calculate 2's complement of an
>>> integer which is n (n<16) bits length?
>>>
>>> Thanks!

>
>
> int a = 5;
> int b = ~a;
>
>

That will give the 1's complement, the result of flipping
each bit. You can get the 2's complement in several ways but
the simplest is:

int b = -a;

Patricia

gwcherryHatesGreenEggsAndSpam@alum.mit.edu
Guest
Posts: n/a

 05-31-2005

"Lucy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "eeh" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> Hi,
>>
>> Could anyone give me sample code to calculate 2's complement of an
>> integer which is n (n<16) bits length?
>>
>> Thanks!

>
> int a = 5;
> int b = ~a;

I wonder whether the op wants to find
the two's complement form of an integer?

Kevin McMurtrie
Guest
Posts: n/a

 05-31-2005
In article <(E-Mail Removed)>,
"Lucy" <(E-Mail Removed)> wrote:

> "eeh" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > Hi,
> >
> > Could anyone give me sample code to calculate 2's complement of an
> > integer which is n (n<16) bits length?
> >
> > Thanks!

>
> int a = 5;
> int b = ~a;

That's one's complement. Two's is one value higher.