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

operators

 
ramya ray
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer of below question is 'g'.Can somebody please explain me how,as i am unable to figure out .

class EBH021 {
public static void main(String[] args) {
System.out.print((-1 & 0x1f) + "," + (8 << -1));
}}

What is the result of attempting to compile and run the program?

a. Prints: 0,0
b. Prints: -1,4
c. Prints: 0x1f,8
d. Prints: 31,16
e. Run-time error
f. Compile-time error
g. None of the above

Thanks
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the shift operator, 8 << -1 is the same as 8 << 31, so the 1 bit is shifted out of the int, so you have 0.

Also if you bitwise and -1 with another number, it doesn't change that number since the bit representation of -1 is all 1s.

0x1f = 31.

So none of the options match the output.
[ September 18, 2006: Message edited by: Keith Lynn ]
 
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 Keith Lynn:
In the shift operator, 8 << -1 is the same as 8 << 31, so the 1 bit is shifted out of the int, so you have 0.


In the AND operator, (-1 & 0x1f) is the same as (0xffffffff & 0x1f), so the result is 0x1f... or when printed in decimal, 31.

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