This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 44609
    
  34
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: 44609
    
  34
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: 44609
    
  34
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ~ operator
 
It's not a secret anymore!