From: Michael H. <mic...@ju...> - 2002-12-30 18:37:11
|
On Monday, December 30, 2002, at 06:31 AM, Johan Knol wrote: >>> However: e.g. "long = int + int" was always done in 16bits. I wonder >>> why >>> that has never been noticed. >> >> Because it is correct. 'int' is the smallest common type for >> (int,int), > and >> the fact that it is being assigned to a long really doesn't matter! > > Hmm. Than how about "int = char + char" or "int = short + short". Gcc > does > promote the operands and not the result. Out of K&R, page 44: * If either operand is a long double, convert the other to long double. * Otherwise, if either operand is a double, convert the other to double. * Otherwise, if either operand is a float convert the other to a float. * Otherwise, convert char and short to int. * Then, if either operand is long, convert the other to long. So the rules promote operands to int, but do not automatically promote past that. I guess the logic is that an int fits into a machine word, so it's cheap to promote up to it. However, promoting any further is expansive and should not be done implicitly. -- Michael |