Exactly what's the point of 'int32_t', etc.

Started by
21 comments, last by jms bc 10 years ago

Another thing int32_t family of types does is guarantee twos compliment arithmetic.

Really? I highly doubt that, can anyone confirm?

It does.

The typedef name int N _t designates a signed integer type with width N, no padding bits, and a two's-complement representation. Thus, int8_t denotes a signed integer type with a width of exactly 8 bits.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Advertisement
The C99 Standard says in section 6.2.6.2 paragraph 2:

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; there shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M £ N). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways: — the corresponding value with sign bit 0 is negated (sign and magnitude); — the sign bit has the value -(2N) (two’s complement); — the sign bit has the value -(2N - 1) (one’s complement). Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for one’s complement), is a trap representation or a normal value. In the case of sign and magnitude and one’s complement, if this representation is a normal value it is called a negative zero.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

No really, they specified they are in twos complement, but the problem is the few types that applies to are optional:

7.18.1.1 Exact-width integer types
1 The typedef name intN_t designates a signed integer type with width N, no padding
bits, and a two’s complement representation. Thus, int8_t denotes a signed integer
type with a width of exactly 8 bits.
2 The typedef name uintN_t designates an unsigned integer type with width N. Thus,
uint24_t denotes an unsigned integer type with a width of exactly 24 bits.
3 These types are optional. However, if an implementation provides integer types with
widths of 8, 16, 32, or 64 bits, it shall define the corresponding typedef names.

It would be nice if they just specified everything had to be twos complement and maybe even little endian, but they are supporting ones complement, sign-magnitude and big endian and keep a huge number of things undefined. Hopefully someday computers running ones complement, sign-magnitude or big endian need not be considered anymore.

They don't have to be defined, because if twos compliment isn't native, you don't want the program to emulate it -- that would be very slow. But any twos compliment machine should have them defined.

I recently debugged really old PRNG code that assumed 'int' was 16 bits. Problem was only in the seeding, but that code had been used for years...

I use the _t types now, but I still have a lot of bit twiddling that will fail if a type declared 'int' isn't 32 bits. Definitely an improvement to have these fixed size types.

The Four Horsemen of Happiness have left.

This topic is closed to new replies.

Advertisement