Ransika deSilva

Ranch Hand

Posts: 524

posted 12 years ago

Hi all,

I have to small problem which I want clear off. I know for any integer the inversion value is (-X)-1, but need some more clarification on this.

Say if you want to invert 18 first thing is to write it in the binary format that's;

1001 0010

then you change the 0s into 1s, and vice versa.

which produces

0110 1101 but the answer for ~18 is -19, (-18)-1. So could you please explain the situation here, I know I am wrong in the the 2s complement,

Thank you very much.

I have to small problem which I want clear off. I know for any integer the inversion value is (-X)-1, but need some more clarification on this.

Say if you want to invert 18 first thing is to write it in the binary format that's;

1001 0010

then you change the 0s into 1s, and vice versa.

which produces

0110 1101 but the answer for ~18 is -19, (-18)-1. So could you please explain the situation here, I know I am wrong in the the 2s complement,

Thank you very much.

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)

Nick George

Ranch Hand

Posts: 815

posted 12 years ago

Well, 2's complement, you recall, is formed by taking the complement and then add one. Therefore, simply inverting and NOT adding one will yeild one less than if you had inverted and then added one. This response assumes you know how 2's complement works... if not, let me know.

[ November 14, 2004: Message edited by: Nick George ]

[ November 14, 2004: Message edited by: Nick George ]

I've heard it takes forever to grow a woman from the ground

posted 12 years ago

The 32-bit int value of 18 is:

00000000 00000000 00000000 00010010

So ~18 is:

11111111 11111111 11111111 11101101

...which is -19.

(Or am I missing something here?)

00000000 00000000 00000000 00010010

So ~18 is:

11111111 11111111 11111111 11101101

...which is -19.

(Or am I missing something here?)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

It is sorta covered in the JavaRanch Style Guide. |