[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cross-platform programming question...



Jalapeno wrote:

>In article <bnesff$b90$1@acme.gcfn.org>,
> dalloff@gcfn.org (Dave Althoff Jr) wrote:
>
>> Just a simple one...
>> 
>> When we refer to a hexadecimal number, we designate it like this:
>> 
>> $FB6F
>> 
>> When anybody else does it, they do it like this:
>> 
>> 0xFB6F
>> 
>> I don't know about you, but I find that really confusing.  Someone sent me
>> a look-up table for an I/O routine and it took me the longest time to
>> figure out what it meant because of all that 0x crap in there.
>> 
>> (I have no formal training on any other system.  I was taught some
>Applesoft
>> and some 6502, everything else I know is self-taught)
>> 
>> Now we've been around a lot longer than most of those other programming
>> systems...what in the world possessed them not to use the $ standard that
>> we established decades ago??  Am I missing something important here?  8-)
>> 
>> --Dave Althoff, ][.
>
>Yes, you are. The $ is a convention used by Apple programmers. A much 
>more common notation for hex digits in assembly language on other 
>platforms is an 'h' (or 'H') suffix, as in FB6Fh or FB6FH, which is also 
>by convention.
>
>The 0x notation is in fact part of ANSI X3.159-1989 (and the equivalent 
>ISO) standard for the C programming language. To quote from X3.159-1989:
>
>3.1.3.2 Integer Constants
>
>Syntax
><snip>
>
>Description
>
><snip>
>A decimal constant begins with a nonzero digit and consists of a 
>sequence of decimal digits. An octal constant consists of the prefix 0 
>optionally followed by a sequence of the digits 0 through 7 only. A 
>hexadecimal constant consists of the prefix 0x or 0X followed by a 
>sequence of the decimal digits and the letters a (or A) through f (or F) 
>with values 10 through 15 respectively.

Standards are merely conventions that have "hardened".  ;-)

Most number representation conventions are just scanner hacks, like
the "$" and the C convention.  A prefix is clearly easier to deal with
lexically, since the modulus of conversion is known before the significant
digits are scanned.

The "h" suffix convention is more difficult, since, in general, it is detected
after part of the number has been converted as a decimal number, and
the scanner must backtrack and re-convert.  A scanner writer would never
choose such a notation--but a microprocessor designer might.  ;-)

The convention chosen for C is a bit of frozen history.  The PDP-11
on which it was written used octal as its normal M/L convention, so
octal and decimal were the first things to be supported, and adding a
leading zero to octal constants seemed easy enough--everything else
was decimal.  Then it became clear that a prefix extension for binary
and hex would be needed, and the code for it was grafted into the
octal path, as a non-octal digit following the leading zero:  b for binary
and x for hexadecimal.  Pure hack--no clear generalization to other
radices--but sufficient for all practical purposes, and now frozen into
the standard forever.  It's a shame that hex (and binary, for that matter)
didn't get the first-class, single-prefix-character treatment that octal
gets, but that's history for you.  ;-)

(BTW, hex digits were not always 0-9 + A-F.  Early in the history
of computing, the Librascope LGP-30 used hex notation (because
its words were a multiple of 4 bits) and chose 0-9 + p-u!  It didn't
catch on.  ;-)

-michael

Check out amazing quality sound for 8-bit Apples on my
Home page:  http://members.aol.com/MJMahon/