updogliu
updogliu

Reputation: 6255

Conversion from int16_t to uint32_t

int16_t s;
uint32_t ui = s;

Is the result of converting a int16_t value to a uint32_t value compiler dependent? If not, what's the rule?

Upvotes: 4

Views: 3408

Answers (1)

Pete Becker
Pete Becker

Reputation: 76498

The results are well-defined; non-negative values stay the same, and negative values are reduced modulo 2^32. But the situations where exact sized types like int16_t and uint32_t are needed are quite rare. There's really no need for anything other than int and unsigned long here: those types have at least as many bits as int16_t and uint32_t and, unlike int16_t and uint32_t, they're required to exist on any conforming implementation. If you really really want the sexy new sized types, at least go for portability with int_least16_t and uint_least32_t.

Upvotes: 7

Related Questions