In <RWk1c.8412$(E-Mail Removed)> "Massimiliano Alberti" <(E-Mail Removed)> writes:

>Are there rules on how should the C behave with the arithmetic operations?
Signed arithmetic overflow causes undefined behaviour. Unsigned

arithmetic overflow causes the result to be reduced modulo the largest

representable value plus one. However, due to the integral promotions,

arithmetic between unsigned operands shorter than unsigned int may be

performed using signed int arithmetic.

>Are the overflows always ignored? And are they simply truncated? (if I have

>a 16-bits unsigned short ints

>unsigned short int a = 65535, b = 10; is a + b defined? And a * b? and b -

>a?)
All of them are (normally) defined, but not *well* defined, because the

result depends on the type of a and b *after* the integral promotions.

If unsigned short is a 16-bit type, there are two *likely* scenarios:

1. int is a 32-bit type, and then the results have type int and the values

65545, 655350 and -65525, respectively.

2. int is a 16-bit type, in which case the operands are promoted to

unsigned int, which is a 16-bit type, too. In this case, the results

have type unsigned int and the values 9, 65526 and 11, respectively.

One can also imagine "exotic" scenarios where these operations generate

signed integer overflow or yield unsigned results other than those

mentioned above, but these are highly unlikely to occur in practice

(that's why I called them "exotic").

So, if you want consistent results, avoid using unsigned short operands.

Dan

--

Dan Pop

DESY Zeuthen, RZ group

Email:

(E-Mail Removed)