aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operators " Watch "operators " New topic
Author

operators

ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
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

Joined: Feb 07, 2005
Posts: 2367
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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operators