Ansel said:
I was actually thinking about every width *except* 8-bits when I posted.
I chose 8-bits to simplify the examples. But, what's special about 8-bits
that they ought to be allowed a conventional unsigned range; perhaps it's
easier to think of examples where things would stop working..?
That would be the scene with *your* 8-bit int type, yes. And I wasn't
"suggesting", I was wondering.
OK, choosing a 16-bit type, you were wondering whether the unsigned range
ought to be 0 to 32768 instead of 0 to 65535.
I think it would be simpler to restrict it to 0 to 32767, ie just the
positive values of a signed int type. (That means you can't
represent -(-32768), but you can't do that as a signed value anyway.) Then
you just have to mask the top bit in every operation.
But then, that would be little different from not having unsigned at all,
and and only using the top half of each signed int range.
It's quite simple actually.
That depends on the type of integer: saturating or modular or
except-on-overflow. Note that my OP was basically referring to a "ranged",
or "range-restricted", integer.
There is some merit to ranged-integer types, but the range would need to be
enforced (not popular in C) and the type system becomes massively more
complicated (what's the result of adding a 100..200 type to a 150..250
type?).
Also, a range is usually a subset of the allowed int range; yours would be a
superset! Because the top value will be one more than the normal range of a
signed int.
It *might* work as a range of the full unsigned int type, but that requires
the normal, unconstrained unsigned type to be still available.
Otherwise there are just two many problems, if any arbitrary value of 16, 32
or 64 bits you might encounter, can only ever be represented as signed, so
will be negative half the time. You can't even convert -1 to unsigned, and
back again, as C can now; you can't represent -1 as unsigned at all.