• 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

please, explain how the answer was gotten

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain how the answer was gotten in details,
Thanks
Q5)
byte b;
double d =417.35;
b=(byte)d;
System.out.println(b);
output is 95
but how to convert a double value into byte type?
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be -95, not 95. Here is how I think is gotten.
1) double 417.35 fist is truncated to int 417
2) 417 = 256 + 128 + 32 + 1 and have a binary representation of
0000 0001 1010 0001
3) it then get truncated again, because byte has only 8 bits, to
1010 0001
4) The sign bit (first bit) at step 3) is 1, therefore it is a negative number. To find the number I use the following relationship.
~x + 1 = -x
~x is 1's complement of the 8 bit in step 3) and it is
0101 1110
This number = 64 + 16 + 8 + 4 + 2 = 94
so x = - (~x + 1 ) = -95.
5) to double check it

C:\JavaRanch>java TestByte
d = 417.35 b = -95

 
kaffo lekan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please how did you do the binary representation
please.
 
huiying li
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is more detail on step 2)
1) It is good to remember
2^10 = 1024, 2^9 = 512, 2^8 = 256, 2^7 = 128, 2^6 = 64 and so on.
2) 417 > 256, and 417 <512, so 9bit or 2^8 is it is highest non-zero bit. 2^0 is the first bit.
3) 417 - 256 = 161, 161 is < 256 , > 128, so the next non-zero bit is the 8th bit you do this recursively, until you get all the bits

4) 417 = 256 + 128 + 32 + 1
= 1 * 2^8 +
1 * 2^7 + 0 * 2^6 + 1 * 2^5 + 0 * 2^4 +
0 * 2^3 + 0 * 2^2 + 0 * 2^1 + 1* 2^0
5) therefore 417 is represented by
0000 0001 1010 0001


 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
huiying li,
very good explanation, found it very useful
Yogesh
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic