1-line Endianness Detection in the C Preprocessor

Yeah, it's evil (or at least chaotic), but...

Go see 1-line Endianness Detection in the C Preprocessor.

(As someone who had a write a C preprocessor (we needed a consistent preprocessor across several architectures), I appreciate this trick.)


The detection doesn't run in the preprocessor, though. See the comments on the original post.

Since this is runtime and this is a perl blog, maybe it is worth to also refer to http://perldoc.perl.org/perlport.html#Numbers-endianness-and-Width is worth - this is using a similar trick in fact

Using pack/unpack is a bit verbose for my tastes, so I would rather use the byteorder information from the Config module.

use Config;
my $is_little_endian = $Config{byteorder} eq '12345678';

Leave a comment

About Mark Leighton Fisher

user-pic Perl/CPAN user since 1992.