Jump to content
  • Advertisement
Sign in to follow this  
ZedFx

(-1) or (0)

This topic is 4615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have always wondered how people calculate or represent the largest possible number of an unsigned integer, as in C/C++ it can be different sizes on different platforms/chips and hence can have different maximum numerical values. So the question is this: what is the better/best way to calculate it ? I have come to conclusion that it is either ((unsigned int)(-1)) or (~0), however, I am not sure if the (-1) way will work on all chips and things like that.

Share this post


Link to post
Share on other sites
Advertisement
Well, ~0 should always work. But -1 won't work on one's complement machines.
What's wrong with UINT_MAX?

Share this post


Link to post
Share on other sites
Technically it won't always work, since it relies on the architecture using two's-complement for representation of signed integers. Use UINT_MAX, which I believe is defined in limits.h for moderately recent C compilers.

Share this post


Link to post
Share on other sites
Use either


#include <limits> // C++
// or
#include <limits.h> // C


A compiler vendor is good at telling what is the maximum value of an int when using its compiler :)


unsigned int max_uint = std::numeric_limits<unsigned int>::max(); // C++
// or
unsigned int max_uint = UINT_MAX; // C


Regards,

Share this post


Link to post
Share on other sites
Use numeric_limits as was recommended if you need the value at runtime. If you need the value at preprocessing time, use UINT_MAX. If you need the value at compile-time, (0u - 1u) is guaranteed to work, or you can use ::boost::integer_traits if you have boost installed.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Technically it won't always work, since it relies on the architecture using two's-complement for representation of signed integers.
Quote:
Original post by Polymorphic OOP
If you need the value at compile-time, (0u - 1u) is guaranteed to work, or you can use ::boost::integer_traits if you have boost installed.
What's wrong with the OPs other suggestion (~0)?

Share this post


Link to post
Share on other sites
Unless I'm completely clueless, I believe that numeric_limits will work at compile time too - on VC++ implementation numeric_limits::max() is just an alias to the corresponding define, so I guess that a good compiler will probably transform it to a constant. I have no gcc here to verify if it is implemented in the same way.

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
Unless I'm completely clueless, I believe that numeric_limits will work at compile time too - on VC++ implementation numeric_limits::max() is just an alias to the corresponding define, so I guess that a good compiler will probably transform it to a constant. I have no gcc here to verify if it is implemented in the same way.

Regards,


Yes, the values of numeric limits will work at compile time.

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
Quote:
Original post by Sneftel
Technically it won't always work, since it relies on the architecture using two's-complement for representation of signed integers.
Quote:
Original post by Polymorphic OOP
If you need the value at compile-time, (0u - 1u) is guaranteed to work, or you can use ::boost::integer_traits if you have boost installed.
What's wrong with the OPs other suggestion (~0)?


1) it is not really easy to decipher (are you sure you'll remember what you tried to do in 2 years?)

2) there is more standard way to do it

But you are true, it should work. I believe Sneftel considered only the (-1) trick, not the (~0) one.

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
What's wrong with the OPs other suggestion (~0)?

Yeah, that should work too, I should have clarified.

Quote:
Original post by Emmanuel Deloget
Unless I'm completely clueless, I believe that numeric_limits will work at compile time too


Quote:
Original post by Washu
Yes, the values of numeric limits will work at compile time.


Function call results, such as of those in numeric_limits specializations, are never useable at compile-time. In fact, that's precisely why boost has integer_traits.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!