File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inversion operator

 
Rally Krish
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What gets printed when the following program is compiled and run. Select the one correct answer.


public class test {
public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}


pls expain

Thanks in advance
 
vidya sagar
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer is -4.

3 when represents in 8 bit(Because it is byte).It will be

3 = 0000 0011
~3 = 1111 1100

Binary 1111 1100 when converted to decimal it results in -4
 
Rally Krish
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

Binary 1111 1100 when converted to decimal it results in -4

can u pls expalin the conversion
 
vidya sagar
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Operators
 
Rally Krish
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vidya sagar,

studying, i got struck here "-6 in binary is 11111010"

again i need to know how exactly the conversion is taking place

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rally Krish:
vidya sagar,

studying, i got struck here "-6 in binary is 11111010"

again i need to know how exactly the conversion is taking place

Thanks


Java uses two's complement to represent integers. But to summarize the link -- to negate a number, the two's complement operation is to invert all the bits, and add one.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic