• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

narrowing double to byte question.

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This program is from jaworski's quiz.
public class test
{
public static void main(String args[])
{
double d1=1;
double d2=0;
d1=d1/d2;
byte b;
b = (byte) d1;
System.out.println(b);
}
}
The output is -1. How?
Regards
Sanjay
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case d1 is infinite which means a hex equivalent of oxfff. This when converted to decimal will give you -1. Instead if hex equivalent is oxffe then the decimal equivalent is -2 and will be -3 for oxffd...so on.
 
mehrar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typo I missed one f. It should be oxffff, oxfffe, oxfffd...
 
Sanjay Mishra
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain how the decimal conversion will
yield -1.
Regards
Sanjay
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to understand 2's complement and binary to decimal conversion. To get the value of a negative number, you need to get the 2's complement.
example:
To get the 2's complement of a binary number, flip all the bits and add one. So, the 2's complement of 0xFFFF (all one's) is 0x0000 + 1 = 0x0001 which is 1 in decimal. So, 0xFFFF = -1 in decimal.
Hope that was understandable. Maybe someone can explain it better.
 
mehrar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Byte range is from -128 to 127. This means that 0x007f is the maximum number (equivalent to 127). If you add one to it JVM will take the two's compliment because the MSB becomes 1 which means negative. In this case 0x00f0 will be 1111 0000 in binary. Now Two's compliment of this is 0001 0000 and the sign bit is 1 which means negative number. Now the Decimal equivalent of (0001 0000) is 16 but with the sign bit on will be -16. So if you convert 0x00f0 into decimel you'll get -16. Similarly the double INFINITE is 0xffff which is 1111 1111 1111 1111 in binary. Two's compliment of it will be 0000 0000 0000 0001 with the sign bit on. Thus the binary equivalent will be -1. In case of 0x00ff it will be -1, and in case of oxfe it will be -2.
Another explanation to this is like this :-
0x00ff = 0000 0000 1111 1111
-128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1
Hope it helps.
------------------
Raj
 
no wonder he is so sad, he hasn't seen this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic