Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

cast between long and int.

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you automataiclly convert a long primitive to a int ,if the value of the long is samll enough to fit the int?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
You can carry out narrowing of conversion if range of the variable is not a problem. If range of long variable for conversion is within range of interger then narrowing of conversion is possible.
Regards
Sandip
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James
If by automatically convert you mean without a cast then no you can't do it (not from long to int anyway. You can do it by casting but may lose information when the leftmost bits of the value being cast get cut off. Going from a long to an int the 32 most significant bits would get truncated. For example the largest long you can have is -9223372036854775807. This is
01111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 in binary. Casting this to an int would result in the left most 32 bits getting chopped so it would be: 11111111 11111111 11111111 11111111 or -1 in decimal.
run this to see it in action:
hope that helps
Dave
[This message has been edited by Dave Vick (edited June 26, 2001).]
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic