6.2.5.6
For each of the signed integer types, there is a corresponding (but >different) unsigned integer type that uses the same amount of storage >(including sign information) and has the same alignment requirements.
6.2.6.2
For unsigned integer types, the bits of the object representation shall be >divided into two groups: value bits and padding bits.
[...]
For signed integer types, the bits of an object representation shall be >divided into three groups: value bits, padding bits and the sign bit. [...] >(if there are M value bits in the signed type and N in the unsigned type, >then M <= N).
Although "The range of nonnegative values of a signed integer type is a >subrange of its corresponding unsigned integer type" [6.2.5.9] the use of >'subrange' implies that M < N?
Can all the values of a signed int be represented in an unsigned
int (or signed long in an unsigned long, etc.)?
If INT_MIN is now 0 and
all values are arithmetically increased by -INT_MIN, is it
guaranteed that every signed value can be represented?
In other words: is UINT_MAX >= INT_MAX - INT_MIN?
It seems obvious, but the standard appears to imply that this is
not guaranteed. It states:
6.2.5.6
For each of the signed integer types, there is a corresponding (but
different) unsigned integer type that uses the same amount of storage
(including sign information) and has the same alignment requirements.
So, same amount of storage including sign bit implies
UINT_MAX == INT_MAX*2
But:
6.2.6.2
For unsigned integer types, the bits of the object representation
shall be divided into two groups: value bits and padding bits.
[...]
For signed integer types, the bits of an object representation
shall be divided into three groups: value bits, padding bits and
the sign bit. [...] (if there are M value bits in the signed type
and N in the unsigned type, then M <= N).
But shouldn't it be M < N?
Otherwise, if M == N, there are the same
amount of value bits in both signed and unsigned (implying the
unsigned has more padding bits), and UINT_MAX == INT_MAX.
I doubt that this is ever done,
but the standard seems to permit it.
Although "The range of nonnegative values of a signed integer type
is a subrange of its corresponding unsigned integer type" [6.2.5.9]
the use of 'subrange' implies that M < N?
So, is it actually portable to represent signed integers as
unsigned?
NS <ns@2b.here> writes:
Can all the values of a signed int be represented in an unsigned
int (or signed long in an unsigned long, etc.)?
It doesn't, because integer types (other than unsigned char) are
allowed to have padding bits, and unsigned types may have more
padding bits than signed types have.
So, is it actually portable to represent signed integers as
unsigned?
Not fully portable, no. If it's important that this be true then
somewhere there should be a test that INT_MAX < UINT_MAX, etc.
(Note that as of C23 there are different rules, and indeed the newer
standard requires, IIUC, that unsigned types have the same number of
padding bits as their corresponding signed types, and consequently
that INT_MAX < UINT_MAX, etc.)
Can all the values of a signed int be represented in an unsigned int (or signed long in an unsigned long, etc.)?
6.2.5.6
For each of the signed integer types, there is a corresponding (but
different) unsigned integer type that uses the same amount of storage
(including sign information) and has the same alignment requirements.
So, same amount of storage including sign bit implies UINT_MAX == INT_MAX*2
6.2.6.2
For unsigned integer types, the bits of the object representation shall be >> divided into two groups: value bits and padding bits.
[...]
For signed integer types, the bits of an object representation shall be
divided into three groups: value bits, padding bits and the sign bit. [...] >> (if there are M value bits in the signed type and N in the unsigned type,
then M <= N).
But shouldn't it be M < N? Otherwise, if M == N, there are the same amount of value bits in both signed and unsigned (implying the unsigned has more padding bits), and UINT_MAX == INT_MAX. I doubt that this is ever done,
but the standard seems to permit it.
Although "The range of nonnegative values of a signed integer type is a subrange of its corresponding unsigned integer type" [6.2.5.9] the use of 'subrange' implies that M < N?You're thinking of a "proper subrange", by analogy with the term "proper subset", but the standard does not require that the subrange be proper.
On 2026-06-28 06:11, NS wrote:[...]
But shouldn't it be M < N? Otherwise, if M == N, there are the same amount of
value bits in both signed and unsigned (implying the unsigned has more
padding bits), and UINT_MAX == INT_MAX. I doubt that this is ever done,
but the standard seems to permit it.
It does.
I've heard of a system which did not have an arithmetic unit, but did
have a floating point unit, which used the mantissa of floating point
values to represent a large integer type. I think they would have had
INT_MAX == UINT_MAX, but I can't vouch for that personally.
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
NS <ns@2b.here> writes:
Can all the values of a signed int be represented in an unsigned
int (or signed long in an unsigned long, etc.)?
Obviously not. Negative values cannot be represented in unsigned types.
But I don't think that's what NS meant.
| Sysop: | DaiTengu |
|---|---|
| Location: | Appleton, WI |
| Users: | 1,126 |
| Nodes: | 10 (0 / 10) |
| Uptime: | 49:09:37 |
| Calls: | 14,414 |
| Calls today: | 2 |
| Files: | 186,400 |
| D/L today: |
10,328 files (2,796M bytes) |
| Messages: | 2,548,842 |
| Posted today: | 1 |