Sign in to follow this  
silverphyre673

Finding out if machine is big/little endian

Recommended Posts

Michalson    1657
Test it.

Declare a 16bit integer. Store the value 1 in it. Declare a byte pointer and point it at the 16bit integer. Read the value. If you get 1, you have little endian, if you get 0, you have big endian.

EDIT: Bah, beaten to the punch.

Share this post


Link to post
Share on other sites
Catafriggm    296
I'd like to know a way to do this myself, using purely preprocessor macros. Right now I just check the archetecture. If it's x86 or x86-64, it's little endian, if it's PPC it's big endian. If you need more than that, you'll have to look them up (I don't know what other archetectures use, although I'd certainly like to hear if you find out).

Share this post


Link to post
Share on other sites
Inmate2993    222
If you're using SDL, they have a provision for this, I think its #SDL_BYTE_ORDER or something.

Otherwise, use preprocessor macros and a platform dependant header that you can change with each build of the code.

#if HIGH_ENDIAN
x = in[0] + in[1]<<8;
#else
x = in[1] + in[0]<<8;
#endif

And then, HIGH_ENDIAN is defined in the file where DEBUG and stuff would be defined. Stuff you'd change before running a compile, or stuff the ./configure script could set up.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this