Define Big/Little Endian ( was: Re: RISC Mach...)

Ron Widell ron at motmpl.UUCP
Sat Feb 10 06:48:41 AEST 1990


In article <686.25c92d5f at mdcbbs.com> halloran at 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?



More information about the Comp.lang.c mailing list