I'm trying to obtain the maximum value of a certain unsigned integer type without including any headers like <limits>
. So I thought I'd simply flip the bits of the unsigned integer value 0.
#include <iostream>
#include <limits>
int main()
{
std::cout << (~0U) << '\n'; // #1
std::cout << (std::numeric_limits< unsigned >::max()) << '\n'; // #2
return 0;
}
I'm not very experienced on the subtle differences between these. Which is why I'm asking if some unexpected behavior or some platform/architecture issues could occur by using the first method.
... to obtain the maximum value of a certain unsigned integer type without including any headers
Simply assign the value -1
unsigned_type_of_choice max = -1;
Conversion of the -1
, which is an int
, to any unsigned type results in the value of number that is one greater than the largest value minus 1.
The following does not provide the maximum value of the destination type. It fails when the destination type range exceed the range of unsigned
, which is the type of ~0U
. @Christopher Oicles
// problem
unsigned_type_of_choice max_wannabe = ~0U;