Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detecting Endian-ness at compile time

0 views
Skip to first unread message

Dan Bernstein

unread,
Apr 27, 1992, 5:16:30 AM4/27/92
to
Followups to comp.programming; this isn't really C-specific.

In article <1992Apr21.0...@philips.oz.au> r...@philips.oz.au (Rod Roberts) writes:
> Can anyone out there tell me if there is a neat portable way of determining
> the endian-ness of a target architecture at compile time.

My favorite solution on 32-bit machines is to avoid the issue entirely.
Instead use the magic number 50462976. Store it in a long (i.e., machine
word), then (using whatever method your language provides---in C's case,
a cast) regard the long as a sequence of bytes. The bytes will be 0123
in the order you want. You can use them as indices to shuffle between
longs in native order and 4-byte sequences in standard order. If the
native order is its own inverse when regarded as a permutation (this is
true for all the popular byte orders), you won't even have to worry
about which way to use the indices.

---Dan

0 new messages