• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Conversion question from Dan's exam

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following question is from Dan's exam

The answer is -> Prints: false, false, true
I couldn't understand how Float.NEGATIVE_INFINITY gets converted to short....What are the values(bit representation) of Float.NEGATIVE_INFINITY ,Float.POSITIVE_INFINITY,Float.NaN?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The floating point numbers have the following form:
s = sign
e = exponent
m = mantissa
The bit layout of a Java float is:
s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm(23 m's)
The mantissa of of a float which occupies only 23 bits, has 24 bits of presion. The most significant mantissa bit is predictable and not included. Now on to your question. The special values have these forms:
+ infinity: 0 11111111 00000000000000000000000
- infinity: 1 11111111 00000000000000000000000
NaN 0 11111111 10000000000000000000000
(please note that NaN has a one bit at the most significant possition)
Now that you have the bit representation you will be able to easily understand his question.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bob.Thanks alot.
Veena
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena,
Before positive infinity is converted to a short it is first converted to the maximum positive value of an int which is represented in binary as a zero followed by 31 ones. When an int is converted to a short the lower 16 bits are kept but the 16 high order bits are thrown away. When the most positive int value is converted to a short the result is therefore a 16 bit value where all 16 bits are set to one. That is the binary representation of negative one.
Before negative infinity is converted to a short it is first converted to the most negative int value. The binary representation of the most negative int value is a one followed by 31 zeros. When that value is converted to a short the lower 16 bits are kept and the high order 16 bits are thrown away. In this case, the result of the conversion is 16 zeros.
Narrowing conversions are covered in Section 5.1.3. of the Java Language Specification.
Although the real exam requires an understanding of narrowing primitive conversions, the level of detail necessary for this question is a little beyond the scope of the real exam. I'm thinking about removing it from the next version of the exam. Does anyone want to vote for or against its removal?
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Dan.But why Float is converted to most positive int befor?Why it doesn't convert to short directly?
Veena
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose that James Gosling was just trying to simplify the design of the compiler. Of course, I'm just guessing.
 
Ranch Hand
Posts: 716
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan, you asked for votes. I think you provide an unique resource to help developers improve our Java skills.
My vote is that you split your mock exam into two parts if you wish. One with questions pegged to the difficulty level of the actual exam, the other which keeps the mind-benders. I check your site two or three times a week and do an exam just to improve my Java, even though I've already passed the SCJP 1.4 exam....
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I support Alfred as there are valuable lessons that one can learn from your question. Though they are tough for the exam meaning that the exact question might not be asked but they do still make the fundamentals clear which actually help in the exam. You can make a different section for such questions so people shouldn't get disheartened if they score low in these tough questions. Hoping to find the questions there and Thanks Dan for the questions and the help.
[ April 29, 2003: Message edited by: Anupam Sinha ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the December version of the exam I had questions like this one in sections titled "Optional". In the April version I removed those sections because it appeared that some people were still thinking that the Optional sections contained required material. I'll think about adding the Optional sections back into the next version of the exam with a new title that does a better job of making the point. Maybe a title such as "Just for Fun" would make the point.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
I like every question in your exam.Every question teaches something.No question looks simple till you understand the concept clearly.I mean it helps in understanding the concepts clearly.But as said it is good idea to make separate section for those which are not for real exams.I have some title in my mind "Non exam,but brain teasers".....
veena
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic