[comp.lang.c] Preprocessor code to figure out endianness ?

otto@tukki.jyu.fi (Otto J. Makela) (09/10/90)

I seem to remember seeing somewhere a real smart preprocessor construction
to figure out the endianness of the machine under which the code was being
compiled.  I cannot remember where and how, though ?  Any pointers ?
--
* * * Otto J. Makela <otto@jyu.fi> * * * * * * * * * * * * * * * * * * * * *
* Phone: +358 41 613 847, BBS: +358 41 211 562 (CCITT, Bell 2400/1200/300) *
* Mail: Kauppakatu 1 B 18, SF-40100 Jyvaskyla, Finland, EUROPE             *
* * * Computers Rule 01001111 01001011 * * * * * * * * * * * * * * * * * * *

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (09/13/90)

In article <OTTO.90Sep9235213@tukki.jyu.fi> otto@tukki.jyu.fi (Otto J. Makela) writes:
> I seem to remember seeing somewhere a real smart preprocessor construction
> to figure out the endianness of the machine under which the code was being
> compiled.  I cannot remember where and how, though ?  Any pointers ?

I don't know about this. I can never remember whether people mean
``big-endian'' as high-low or low-high; on the occasions when I need to
know byte order, I use something like

int hilo_magic = 1212958796; char *hilo = (char *) &hilo_magic;

hilo[0]hilo[1] is "HL" on machines that place the high byte first and
"LH" on others. It even works on sixteen-bit machines, provided the
compiler reduces 1212958796 to 18508.

---Dan

scjones@thor.UUCP (Larry Jones) (09/13/90)

In article <28803:Sep1221:34:1690@kramden.acf.nyu.edu>, brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
> on the occasions when I need to know byte order, I use something like
> 
> int hilo_magic = 1212958796; char *hilo = (char *) &hilo_magic;
> 
> hilo[0]hilo[1] is "HL" on machines that place the high byte first and
> "LH" on others. It even works on sixteen-bit machines, provided the
> compiler reduces 1212958796 to 18508.

Gee, it doesn't seem to work at all on my IBM 3090....
----
Larry Jones                         UUCP: uunet!sdrc!thor!scjones
SDRC                                      scjones@thor.UUCP
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
I always have to help Dad establish the proper context. -- Calvin

keck@sea.com (John Keck) (09/16/90)

In article <28803:Sep1221:34:1690@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
> [...]
>
>int hilo_magic = 1212958796; char *hilo = (char *) &hilo_magic;
>
>hilo[0]hilo[1] is "HL" on machines that place the high byte first and
>"LH" on others. It even works on sixteen-bit machines, provided the
>compiler reduces 1212958796 to 18508.
>
>---Dan

As long as they use the ASCII character set.


-- 
John Keck INTERNET:keck@sea.com UUCP:...!ucsd!soledad!keck VOICE:619/581-6181
Systems Engineering Associates, 2204 Garnet Ave Suite 203, San Diego CA 92109
-----------------------------------------------------------------------------
 "Much speech leads inevitably to silence. Better to hold fast to the void."