Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range of integers imposed by the C++ standard

Tags:

c++

This question is about what the C++ standard imposes to the range of the fundamental integer types. In the C++17 standard, the point 6.9.1 on fundamental types has a point 4 where it says that:

Unsigned integers shall obey to the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of that particular size of integer.

In the C standard, it only says that if [0, max] is the range that can be represented by an unsigned integer, all operations that goes out of this range is reduced modulo range + 1. It never says that range + 1 should be a power of 2.

Does this quote of the C++ standard means that all unsigned integers have a range of the kind [0, 2^n - 1] ? Can we infer from that point that all signed integers have a range of the form [-2^(n/2), 2^(n/2) - 1]?

I see nothing in the standard that say that, but the previous quote of the standard seems to imply that kind of things.


PS: This question is different from the one which is given here as a duplicate. The question linked is about why two's complement is not enforced in the standard. My question is about what is in the actual standard.

like image 819
InsideLoop Avatar asked Feb 04 '18 19:02

InsideLoop


1 Answers

I'll try an answer here, open for corrections:

Unsigned integers shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.

This implies that the ranges of unsigned integers are [0, 2^n - 1], I don't see how you could enforce this otherwise.

[basic.fundamental]#3 — Emphasis is mine

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; that is, each signed integer type has the same object representation as its corresponding unsigned integer type. [...] The range of non-negative values of a signed integer type is a subrange of the corresponding unsigned integer type, the representation of the same value in each of the two types is the same, and the value representation of each corresponding signed/unsigned type shall be the same. [...]

As far as I know, these are the only constraints linking signed and unsigned integers:

  • first point is that related unsigned and signed integers are represented using the same number of bits n;
  • second point is that any positive value of a signed integer has the same representation in the related unsigned type.

The three following methods of representing signed integers fullfil these constraints: Sign & Amplitude, Ones' complement, Two's complement.

As I read it, there are not enough constraints in the standard to enforce two's complement representation for signed integers.

like image 99
Holt Avatar answered Oct 30 '22 21:10

Holt