[comp.lang.c] Define Big/Little Endian

halloran@mdcbbs.com (M. J. Halloran II (McDonnell Douglas M&E)) (02/02/90)

In article <6200024@ux1.cso.uiuc.edu>, phil@ux1.cso.uiuc.edu writes:
> Another problem you can encounter when dealing with data structure formats
> imposed by hardware, particularly external hardware, is the byte order.
> An example is handling TCP and IP headers.  The exact data structure is
> defined very specifically, but making code that can deal with it AND be
> portable across big and little endian machines requires a lot of care.
                  ---     ------ ------
		   Let's clerify...
> Such portable code may not be the most optimal depending on how it is
> done.
> 
> --Phil Howard, KA9WGN--
> <phil@ux1.cso.uiuc.edu>

   These define how bytes within words are ordered.

Big endian:    Big end first (MSB)(LSB).
Little endian: Little end first (LSB)(MSB).

	ie: 

	typedef union {
		unsigned char bytes[2];
		unsigned long word;
	    } INDIANS;

	main()	{

	        INDIAN indian;

	    indian.word = 0x1234;

	    if ( indian.bytes[0] == 0x12) /* is MSB at lower mem? */
		puts ( "Big Endian\n");
	    else
		puts ( "Little Endian\n");
	}

aduncan@rhea.trl.oz.au (Allan Duncan) (02/05/90)

> Big endian:    Big end first (MSB)(LSB).
> Little endian: Little end first (LSB)(MSB).

What about the Intel little endian/big endian mish-mash on 32 bits?

Allan Duncan	ACSnet	aduncan@rhea.trl.oz
		ARPA	aduncan%rhea.trl.oz.au@uunet.uu.net
		UUCP	{uunet,hplabs,ukc}!munnari!rhea.trl.oz.au!aduncan
Telecom Research Labs, PO Box 249, Clayton, Victoria, 3168, Australia.

ron@motmpl.UUCP (Ron Widell) (02/10/90)

In article <686.25c92d5f@mdcbbs.com> halloran@mdcbbs.com
         (M. J. Halloran II (McDonnell Douglas M&E)) writes:
<
<   These define how bytes within words are ordered.
<
<Big endian:    Big end first (MSB)(LSB).
<Little endian: Little end first (LSB)(MSB).
<
<	ie: 
<
<	typedef union {
<		unsigned char bytes[2];
<		unsigned long word;
<	    } INDIANS;
<
<	main()	{
<
<	        INDIAN indian;
                ^^^^^^ I assume you meant to put INDIANS here
<
<	    indian.word = 0x1234;
<
<	    if ( indian.bytes[0] == 0x12) /* is MSB at lower mem? */
<		puts ( "Big Endian\n");
<	    else
<		puts ( "Little Endian\n");
<	}

Unless you have a machine with 16-bit (or 2 char) longs, I don't think
you can guarantee this will work either. I just tried it on a 68020 and
a BCS-compliant 88k machine, and they both printed out "Little Endian";
indian.bytes[0] was equal to 0 in both cases, and the else clause was
invoked. When I defined indian.word to be unsigned short (not long), it
seemed to work.
-- 
Ron Widell, Field Applications Eng.	|UUCP: {...}mcdchg!motmpl!ron
Motorola Semiconductor Products, Inc.,	|Voice:(612)941-6800
9600 W. 76th St., Suite G		| I'm from Silicon Tundra,
Eden Prairie, Mn. 55344 -3718		| what could I know?