The Artima Developer Community
Sponsored Link

Java Answers Forum
Question of the Day 2

2 replies on 1 page. Most recent reply: Aug 1, 2002 10:31 AM by Dan Chisholm

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Dan Chisholm

Posts: 11
Nickname: dchisholm
Registered: Jul, 2002

Question of the Day 2 Posted: Aug 1, 2002 7:43 AM
Reply to this message Reply
Advertisement
Would anyone like to suggest an answer to the following question? Can you give an explanation for your answer?

class P{
public static void main (String[] s) {
byte b = 5;
System.out.println(b<<33);
}
}


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

a. Prints: -1
b. Prints: 0
c. Prints: 1
d. Prints: 5
e. Prints: 10
f. Runtime error
g. Compiler error
h. None of the above

I'll post the answer later.


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Question of the Day 2 Posted: Aug 1, 2002 9:36 AM
Reply to this message Reply
> public static void main (String[] s) {
> byte b = 5;
> System.out.println(b<<33);

Here b is byte. However, byte is promoted to int before any manipulation. Since, int has only 32 bits, you cannot shift its bits by more than 31. Here, 33 is int and if you read JLS carefully then it says in case of i << n, where n is int, only first 5 lower order bits of n will be used as shifting no. It is same as saying for all positive n , replace n by n % 32. Here b << 33 will be replaced by b << ( 33 % 32 ) i.e. b << 1. So your answer is 10. If n is long then first six lower order bits will be used as shift no.

Thanks
Kishori

Dan Chisholm

Posts: 11
Nickname: dchisholm
Registered: Jul, 2002

Re: Question of the Day 2 Posted: Aug 1, 2002 10:31 AM
Reply to this message Reply
Kishori,

Yes! You are correct.

People that are studying for the Sun Certified Java Programmer exam spend a lot of time reading the Java Language Specification and frequently quote it as you have. Are you certified?

Dan

Flat View: This topic has 2 replies on 1 page
Topic: loopback in Java RMI Previous Topic   Next Topic Topic: Help !!!! Dynamic dependent dropdown list using servlet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use