[SDL] Get pixel value ...more in details

Rainer Deyke root at rainerdeyke.com
Mon Aug 27 14:36:00 PDT 2001

"Kylotan" <kylotan at kylotan.eidosnet.co.uk> wrote in message
news:mailman.998942646.2098.sdl at libsdl.org...
> > > res = (int)r;
> > > res |= (int)g << 8;
> > > res |= (int)b << 16;
> >
> > Integer promotion rules make these casts redundant.  Replace 'int'
> with
> > 'Uint32', and they'll be correct and no longer redundant.
> Are these rules mandated by the Standard (and if so, is it the same for
> C and C++), or are they just something that 'most' compilers do?

I don't have a copy of the Standard, but I can quote K&R.

The C Programming Language, Second Edition, A6.1:

     A character, a short integer, or an integer bit field, all signed or
not, [...] may be used in an expression wherever an integer may be used.  If
an 'int' can represent all the values of the original type, then the value
is converted to 'int'; otherwise the value is converted to 'unsigned int'.

> Personally I don't see why the left value in a shift expression would be
> promoted to the type of the value on the right.

The type of the value on the right doesn't matter in this case.  Integer
promotion always happens when a type that is narrower than 'int' is used in
an expression.

assert(((char)1 << (char)8) == 0x100);

Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor

More information about the SDL mailing list