aspose file tools*
The moose likes Beginning Java and the fly likes ~ operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "~ operator " Watch "~ operator " New topic
Author

~ operator

SanjayR Jain
Ranch Hand

Joined: Mar 05, 2009
Posts: 43
How the ~operator works?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Does this Java™ Tutorials page help? Remember ~ is a unary operator and has a higher precedence than */%.
SanjayR Jain
Ranch Hand

Joined: Mar 05, 2009
Posts: 43
As I go through the tutorials, It is a bitwise complement operator.

I have made java application in which I do this.

public static void main(String[] args) {
System.out.println(~4);
}

Result of this is -5.
I am not getting why the output is -5?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You will have to write down the bit values for 4, then invert them, then convert them back to decimal remembering you are using two's complement arithmetic.

Then you will get -5.
SanjayR Jain
Ranch Hand

Joined: Mar 05, 2009
Posts: 43
Ok Now I got it.
Thanks a lot
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ~ operator