[comp.misc] "big endian" and "little endian": bytes, bits, aieee!

chris@mimsy.UUCP (Chris Torek) (12/31/88)

In article <204@mic.UUCP> d25001@mic.UUCP (Carrington Dixon) writes:
>    Uh, am I more confused than usual?  Under the only "little endian"
>architectures with which I am familiar, it is the _byte_ order that goes
>least significant to most significant.  The _bit_ order within each byte
>goes most significant to least significant just like it does in the
>"big endian" machines.
>
>     Are you thinking of some machines other than the usual VAX/Intel?
>Or is one of us just confused?

Everyone is confused :-)

You can get all sorts of combinations, big-endian bytes with little-
endian bits; big-endian bits with little-endian bytes; and even little-
endian bits and bytes with inside-out-endian-floats.  Yeck.  But
somehow it all works out in the end.  My own personal favourite is
Little Big Horn Endian, but there is no accounting for taste.

Actually, endianity---What?  Of course it's a word.  I just said it,
diddn'I?---endianity is all a matter of your point of view.  As long as
you never change your bits to bytes, bytes to words, words to longs, or
long johns to boxer shorts, you will never be caught in the cold, so to
speak.  (Say, who's writing this material?)  Of course, you'll never
get anything done either.  At least, not if your long johns were
knitted by my sister.  (Waitaminnit!  I haven't even *got* a sister!
---Well no wonder you had trouble with the long johns.---  Oh brother.
---They were knitted by your *brother*?---  Aargh.  I'm going back to
my old script writer.  ---Yeah, but he's dull.---  Better dull than
stupid!  ---Okay, but he can't knit worth a darn either.---  OUT!)

Now, where was I?  Oh yes, a matter of your point of view.  There is,
however, something to be said for self-consistency.  Take a Vax, for
instance.  No, not that one; try this one over here.  It is
little-endian: the first byte---the one at offset 0---is the least
significant byte of a longword.  In that first byte, the first bit
---the one numbered 0, again---is the least significant bit, too.  In
fact, if you take bit B at offset O, it is the O*8+B'th bit in the
quantity represented, giving the multiplier (0 or 1) for 2^(O*8+B).
See?  Nicely self-consistent.  (Pay no attention to that D-floating
number behind the curtain!)

Now how about this 68010 here?  It is big-endian: the first byte
(yep, offset 0 again) is the *most* significant byte of a 32 bit
value.  Here, fetch it into a register:

	moveq	#0,d0
	movb	0(a4),d0

Now look at the bit numbered 0.  Is it the most significant bit
of that longword?

	btst	#0,d0

Oops.  No, it is the least significant bit of the most significant byte
of the longword.  What happened to our self-consistency?  (What?  Me?
I wouldn't palm it.  Word o' honor.  See, I dropped it down this here
convenient slot in the table.  The fingers never left my hands. ...
Say, I thought we got you out of here!  [Forgot me hat.]  Here, it
fell under the table.  Let me get that self-consistency back out of
it.  There.  [Thanks.]  Now OUT!  [Sheesh.])

As it happens, on our handy graphical bitmapped display over here on
the right, that most-significant byte shows up on the left edge, with
its most-significant bit on the left edge of those bits.  If we run a
little loop:

	| I think I got most of these mnemonics right
		movl	#W-1,d1
		lea	screen,a0
		subw	#0,d0		| clear C
	L:	movl	(a0),d0
		roxr	#1,d0
		movl	d0,(a0)+
		dbra	L

Ta-da!  The line shifts to the right.  Try that on a little-endian!

Anyway, I like them both.  The trick is always knowing where you
stand, and what you want to see.

Please do not take anything in this article too seriously; the author
was on pasta again when he wrote it.  (You may have guessed that
something was up :-) )
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris