(-1) or (0)
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.
Well, ~0 should always work. But -1 won't work on one's complement machines.
What's wrong with UINT_MAX?
What's wrong with UINT_MAX?
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.
Use either
A compiler vendor is good at telling what is the maximum value of an int when using its compiler :)
Regards,
#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++// orunsigned int max_uint = UINT_MAX; // C
Regards,
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.
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 OOPWhat's wrong with the OPs other suggestion (~0)?
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.
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,
Regards,
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.
Quote:Original post by doynaxQuote: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 OOPWhat's wrong with the OPs other suggestion (~0)?
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement