wood burning stoves*
The moose likes Beginning Java and the fly likes Bitwise inversion operator (~) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bitwise inversion operator (~)" Watch "Bitwise inversion operator (~)" New topic
Author

Bitwise inversion operator (~)

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
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.


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
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 ]

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

Joined: Aug 31, 2004
Posts: 11343

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?)


"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
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
OK, thanks a lot, the answers are superb, thank you once again....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bitwise inversion operator (~)