Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Created April 29, 2016 04:55
Show Gist options
  • Select an option

  • Save mlabbe/889781de525139c5ed01dc96c25bf74c to your computer and use it in GitHub Desktop.

Select an option

Save mlabbe/889781de525139c5ed01dc96c25bf74c to your computer and use it in GitHub Desktop.
Endian detection in the preprocessor
// My goal is to perform endian detect in the preprocessor without
// breaking C89 compatibility. YES, I know how rare big endian is in 2016. I don't care.
#if (0xFF<<8) & 65280
# define LITTLE_ENDIAN
#else
# define BIG_ENDIAN
#endif
// The magic 65280 is a 16 bit int with eight ones followed by eight zeros, at least on little endian.
// Comments?
// Can anyone say whether the preprocessor emulates the enddianness of the target architecture in a cross compile?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment