2
votes

Some background:

  • Prior to C++20, the C++ Standard allowed any signed integer representation, and the minimum guaranteed range of N-bit signed integers was from -(2^(N-1) - 1) to +2^(N-1) - 1.
  • C++20 standard proposal (as mentioned by @Sneftel) allowed the only representation with the guaranteed range from -2^(N-1) to +2^(N-1) - 1.
  • As far as I understand the range of values min-max must be continuous.

I've tried to find any quotes for the maximum range of N-bit signed integer but found none.

So I wonder (in terms of C++):

  1. Is it right to assume that maximum possible range of N-bit signed integer is limited from -2^(N-1) to +2^(N-1)? Could it be wider?
  2. Could it be affected by underlying platform bit implementation - like "3-state-bit" or something like that?
2
Standard does not limit the maximum possible range. It just guarantees its minimum. - Zefick
@Zefick that's...obvious I think and doesn't answer the question. Let me rephrase it: how could N-bit signed integer be wider then {-2^(N-1); +2^(N-1)}? - Alexander G.
@Zefick it sounds like you're thinking of earlier versions of the language. - Sneftel
@UKMonkey expected by what? Let's have an example: I have INT_MAX = 2 ^ 42 and 32-bit int. Is it conforming standard? If it is - how is it possible? - Alexander G.
"Is it conforming standard?" Pre-C++20 yes. "if it is - how is it possible?" It probably isn't; but the standard didn't care. - UKMonkey

2 Answers

3
votes

There is not currently a final C++20 standard. The proposal you're likely thinking of is P1236, and it says:

The range of representable values for a signed integer type is -2^(N-1) to 2^(N-1)-1 (inclusive), where N is called the range exponent of the type.

That's both a maximum and a minimum. Because the proposal requires two's complement for signed integers, there's no opportunity for variation. Trivially, N bits cannot be used to represent more than 2^N distinct values (by the pigeonhole principle), so that range is as big as things can get, even if it were specified only as a minimum range.

As for three-state bits, those don't work with the language.

3
votes

C++2a, while projected to become the C++20 standard, is not there yet.

Aside from that, while the current draft enforces two's-complement representation for signed integers, the current Standard (C++17) and all precursors do restrict choice:
Specifically, the other options offered are signed magnitude representation and ones' complement. Both feature negative zero.

There is information on the range of N-bit signed integers. Specifically, that not all of those N1 bits of the object-representation need be part of the N2 bits of the value-representation, with the exception of the narrow character types. So, the value-representation's N-bit width might be smaller. Considering the pigeonhole-principle, it is no surprise it cannot be bigger.

The assumption that bits are binary is deeply embedded in the language, just like it pervades many other programming-languages. Changing that would be a major project.