Byte order (or you are both wrong)

Gregory Smith greg at utcsri.UUCP
Thu Apr 17 06:34:53 AEST 1986


In article <17162 at rochester.ARPA> crowl at rochester.UUCP (Lawrence Crowl) writes:
>
>When sorted lexicographically, the numbers in little-endian format are
>sorted based on the low order digit.  For example 32 > 41.  CLEARLY WRONG.
>
>When sorted lexicographically, the numbers in big-endian format are sorted
>based on the first high order digit of each number.  This presents a problem
>when numbers are of different sizes.  For example, 32 > 126.  CLEARLY WRONG.
>incompatible with our (natural) definition of numeric sorting.
>
>CHALLENGE:  Come up with a scheme for representing numbers, and a sorting
>scheme in which numbers sort naturally.  Your scheme must deal with variable
>length character strings and variable size numbers.  That is, you cannot
>requires strings to be padded with nulls, or numbers to be padded with zeros.

How about prepending the digit count to big-endian digit strings?
so 32 and 126 become 232 and 3126, and a lexical comparison gives
3126 > 232. Of course, leading zeroes in the significand cannot be used.
If the strings are of different lengths ( and both positive ) then the
longer one is always greater. To support negative numbers, the length
digit could be biased. The following convention supports signed strings
of length 1-5: 

	1  2  3  4  5		< length of number
	5  6  7  8  9		< prefix for +ve numbers
	4  3  2  1  0		< prefix for -ve numbers

The significand digits in a negative number would have to be 9's complemented,
so 32, 126,-37 and -8 are 632, 7126, 362 and 41. There are two 0's, 49 and 50.
( This makes zero an exception to the leading-zero rule ). Note also that
-99 becomes 300,and that 2999 is illegal.
This is starting to resemble a floating-point scheme...
To allow longer strings, just allow a bigger 'length' field - more digits,
or a whole byte.

>Note that many part numbering schemes have intermixed letters and digits.

But what significance do letters have? do you want them to be ignored,
or to be assigned a dictionary order with the numbers?

P.S. this rather obscure scenario hardly seems reason to state that
little- and big-endians are both wrong.
-- 
"If you aren't making any mistakes, you aren't doing anything".
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg



More information about the Comp.lang.c mailing list