[Mono-list] new color.cs for inclusion in CVS

Brian Crowell brian@fluggo.com
Thu, 7 Mar 2002 14:01:46 -0600


One more. Let me clarify...

>	// This code will not compile:
>	// colorHouston.a = ( packed & 0xff000000 ) >> 24;
>	// colorHouston.r = ( packed & 0xff0000 ) >> 16;
>	// colorHouston.g = ( packed & 0xff00 ) >> 8;
>	// colorHouston.b = ( packed & 0xff );

Not dogging on Houston or anything (he wrote the code when a, r, g, and b
were int's), but the first line isn't supposed to compile, even if they are
int's, checked or not. This is because the expression is evaluated like so:

 1. packed is an int
 2. 0xFF000000 is a uint
 3. The closest match among the & operators is long operator &( long, long )
(there is no implicit cast from int to uint). Both operands are up-cast and
the operation is performed. The result is a long.
 4. long operator >>(long x, int count) is evaluated. The result is a long.
 5. There is no implicit cast from a long to an int, so the compilation
fails.

The code is easily repaired:

	unchecked {
		colorHouston.a = (packed & (int)0xff000000) >> 24;
	}

...but it serves as just another reminder that C# is not C. I like the way
C# works, and this is no exception (once I figured it out  :P), but you just
hafta be careful...

--Brian