Can the smallest unsigned int that can contain the positive values of a given signed integral type be determined at compile time?
So the smallest unsigned integral type that is large enough to hold the max value of a given signed integral type.
A good starting point is that 2 is the base-number and we need to take into account the 2-based logarithm. General rules:
If an unsigned integer has a sizeof
n bytes, then the largest positive number is (2^(8 * n)) - 1
If a signed integer has a sizeof
n bytes, then the largest positive number is (2^(8 * n - 1)) - 1
As a result, you need to determine the sizeof
a variable of the given type and see if it has a sign.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.