This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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: 37865
    
  22
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: 37865
    
  22
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: 37865
    
  22
Well done
 
wood burning stoves
 
subject: ~ operator
 
Similar Threads
convert Postiive number to negative..
Picking variables with a given propability
Interview Question
Iterations - finding the same value for 2 variables
can any one explain this