# 2's complement

Discussion in 'Java' started by eeh, May 30, 2005.

1. ### eehGuest

Hi,

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

Thanks!

eeh, May 30, 2005

2. ### Andrew ThompsonGuest

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

Andrew Thompson, May 30, 2005

3. ### LucyGuest

"eeh" <> wrote in message
news:...
> 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;

Lucy, May 30, 2005
4. ### Patricia ShanahanGuest

Lucy wrote:

> "eeh" <> wrote in message
> news:...
>
>>> 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

Patricia Shanahan, May 30, 2005
5. ### Guest

"Lucy" <> wrote in message
news:...
>
> "eeh" <> wrote in message
> news:...
>> 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?

, May 31, 2005
6. ### Kevin McMurtrieGuest

In article <>,
"Lucy" <> wrote:

> "eeh" <> wrote in message
> news:...
> > 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.

Kevin McMurtrie, May 31, 2005