This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "~operator" Watch "~operator" New topic
Author

~operator

jyothi godavarthy
Ranch Hand

Joined: Jan 28, 2004
Posts: 135
please someone explain me how to do this inversion. The output is -9. thanks
class test {
public static void main(String args[]) {
byte x = 8;
x = (byte)~x;
System.out.println(x);
}
}
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
That's the way the Bitwise complement operator ~ works.
Read what JLS Section 15.15.5 Bitwise Complement Operator ~ says-

The type of the operand expression of the unary ~ operator must be a primitive integral type, or a compile-time error occurs. Unary numeric promotion (�5.6.1) is performed on the operand. The type of the unary bitwise complement expression is the promoted type of the operand.
At run time, the value of the unary bitwise complement expression is the bitwise complement of the promoted value of the operand; note that, in all cases, ~x equals (-x)-1.

Hence ~(8) = -8 - 1 = -9.
You can safely use this rule in the exam.

Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
jyothi godavarthy
Ranch Hand

Joined: Jan 28, 2004
Posts: 135
got it Ajit. thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ~operator