Search code examples
c++unsignedsigned

size of type vs its unsigned equivalent?


The standard specifies minimum ranges that can be hold by arithmetic integer type. However, is there a guaranty that for each signed integer, its unsigned equivalent has the same size?

In other words, is it Always true that:

sizeof(short) == sizeof(unsigned short)
sizeof(int) == sizeof(unsigned int)
sizeof(long) == sizeof(unsigned long)
sizeof(long long) == sizeof(unsigned long long)

?


Solution

  • (All ISO Standard references below refer to N4659: March 2017 post-Kona working draft/C++17 DIS)


    However, is there a guaranty that for each signed integer, its unsigned equivalent has the same size?

    Yes.

    From [basic.fundamental]/3 [extract, emphasis mine]:

    [basic.fundamental]/3

    For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: “unsigned char”, “unsigned short int”, “unsigned int”, “unsigned long int”, and “unsigned long long int”, each of which occupies the same amount of storage and has the same alignment requirements as the corresponding signed integer type; [...]