• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

MAX_VALUE and Casting

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output:

[CODE]
int maxvalue = 2147483647
int maxvalue2147483647
long maxvalue = 9223372036854775807
long maxvalue cast to an int -1
double maxvalue = 1.7976931348623157E308
double maxvalue cast to an int 2147483647
/CODE]

Why when you cast a double to an int you get -1 (because it is too big), but when you cast a double, you get the max value for integer?

Thanks again,

Cory
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't it mean that, it is out of range.

The maximum value of double will defnately be out of range of integer..!
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange!!!


Eager to know the reason.




@cmbhatt
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explicit cast from long to int is different from the explicit cast from a double to int.

For double to int, it actually has to convert the bits. Examine the value as a double and then convert it to the nearest int value.

For long to int, it just assumes that you know what you are doing and truncates the 4 high bytes. And believe it or not, if you take only the 4 low bytes of a Long.MAX_VALUE, you get the bit pattern for -1.

Henry
 
Cory Max
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And believe it or not, if you take only the 4 low bytes of a Long.MAX_VALUE, you get the bit pattern for -1.



I think I must be misunderstanding something. Wouldn't the 4 low bytes of a Long.MAX_VALUE be 32 1's? The first one being a sign bit then the remaining 31 being 2147483647?
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think I must be misunderstanding something. Wouldn't the 4 low bytes of a Long.MAX_VALUE be 32 1's? The first one being a sign bit then the remaining 31 being 2147483647?



The low 4 bytes of Long.MAX_VALUE is indeed 32 1's. And ....

11111111 11111111 11111111 11111111 == -1

Remember that Java uses 2's complement to represent integers.

Henry
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic