Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Rob The Bloke

Compile time byte order constant

This topic is 5744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
No. Generally, you determine your target machines ahead of time and then use conditional compilation to compile in the correct code. It is up to the person building the code to specify the correct target machine at compile time.

That said, it''s fairly easy to determine byte ordering at run-time (assign a number to a variable and check the value of the first byte). But, to make use of that information, your application would be full of if-statements and duplicate code and it probably wouldn''t be worth it.

Share this post


Link to post
Share on other sites
Although this may not be useful for you, some unixes define their architecture''s endian style. A simple demonstration:

#include <stdio.h>

#include <sys/param.h>

int main(void) {
#if defined(BYTE_ORDER)
#if BYTE_ORDER == LITTLE_ENDIAN
fputs("little endian\n",stdout);
#else
fputs("big endian\n",stdout);
#endif
#elif defined(__BYTE_ORDER)
#if __BYTE_ORDER == __LITTLE_ENDIAN
fputs("little endian\n",stdout);
#else
fputs("big endian\n",stdout);
#endif
#else
fputs("unknown endian\n",stdout);
#endif

return 0;
}


This should work in most or all Linux distro''s. I think this works in FreeBSD, but I can''t remember (and I can''t check at the moment). This doesn''t work in Solaris 8, but may in other versions. But, since this is normally used for *nix stuff, you would normally back it up with a configure script to check the endian order for you before compiling.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!