# (-1) or (0)

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

## 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 on other sites
Well, ~0 should always work. But -1 won't work on one's complement machines.
What's wrong with UINT_MAX?

##### 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 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++// orunsigned int max_uint = UINT_MAX; // C

Regards,

##### 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 on other sites
Quote:
 Original post by SneftelTechnically 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 OOPIf 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 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 on other sites
Quote:
 Original post by Emmanuel DelogetUnless 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 on other sites
Quote:
Original post by doynax
Quote:
 Original post by SneftelTechnically 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 OOPIf 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 on other sites
Quote:
 Original post by doynaxWhat's wrong with the OPs other suggestion (~0)?

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

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

Quote:
 Original post by WashuYes, 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.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 15
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633353
• Total Posts
3011486
• ### Who's Online (See full list)

There are no registered users currently online

×