Integer to store color in C, endian dependent -


i have question endianness, store colors in html format (rgb #aabbcc).

i use 0xaabbcc in code store color, , extract red, green , blue colors apply masks this:

int color = 0xaabbcc; int r = color & 0xff0000; int g = color & 0x00ff00; int b = color & 0x0000ff; 

this works well, have not tested under big endian machine, result same?

i must see sdl checks endian creating surfaces in example of man sdl_creatergbsurface:

#if sdl_byteorder == sdl_big_endian    rmask = 0xff000000;    gmask = 0x00ff0000;    bmask = 0x0000ff00;    amask = 0x000000ff; #else    rmask = 0x000000ff;    gmask = 0x0000ff00;    bmask = 0x00ff0000;    amask = 0xff000000; #endif     surface = sdl_creatergbsurface(sdl_swsurface, width, height, 32,                                   rmask, gmask, bmask, amask); 

yes. bitmasks independent of endianness. place endianness issue if casting byte array (or, in c, char *), in case endianness matter.

also ensure sizeof(int) > 3 before using code!


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -