[SDL] endianity

Matt Johnson mjohnson at shortgrass.net
Tue Jun 19 11:12:17 PDT 2001


On x86 based systems, the low order bytes are stored first (in memory). This
is called little-endian. On PowerPC based systems, I think they are stored
high order bytes first. This is big endian.

For example, a short int (16-bit) number would store the least significant
8-bits first and the most signifcant 8-bits first. So a number $FF00 (in
hex) would be stored $00 for the first byte, and $FF on the second byte on
x86 systems, and $FF on the first byte and $00 on the second byte for
Motorolla (powerPC) based systems.

Everytime you deal with a data type like short or long, and perform
operations on it its always interpreted to be a big endian system. So if you
AND a short int (16-bits) with a byte (8-bits), the byte AND'd will be
performed always on the least significant byte of the short int.

Its only from reading or writing to memory (or harddisk, or whatever) where
you have to worry about the order of the bytes. In fact, the only reason you
would ever have to worry is if your sharing data between one big-endian
system and another little-endian system. Such an incident would happen in
SDL_Network or whatever, since it has cross-platform network code. Also it
would be important when say you save data for one game under the Mac and
load it under the PC.

Hmm, that probably sounds confusing. I recommending reading a good
binary/hex tutorial.

Matt

> i wonder about endianity issue with double/float..
> is it a stupid question ?
>
> i have no idea of how they are laid in memory and write on disk.
> could someone tell me ?
>




More information about the SDL mailing list