This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates 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/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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: 39478
    
  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: 39478
    
  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: 39478
    
  28
Well done
 
jQuery in Action, 2nd edition
 
subject: ~ operator