Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/17/84; site think.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!rose From: rose@think.ARPA (John Rose) Newsgroups: net.lang.c,net.arch Subject: Re: Byte order (retitled) Message-ID: <5056@think.ARPA> Date: Fri, 25-Apr-86 15:02:09 EDT Article-I.D.: think.5056 Posted: Fri Apr 25 15:02:09 1986 Date-Received: Sun, 27-Apr-86 05:11:58 EDT References: <7046@cca.UUCP> <7158@cca.UUCP> <1104@psivax.UUCP> <2590@utcsri.UUCP> <1117@psivax.UUCP> Reply-To: rose@think.UUCP (John Rose) Organization: Thinking Machines, Cambridge, MA Lines: 55 Xref: watmath net.lang.c:8739 net.arch:3169 In article <1117@psivax.UUCP> friesen@psivax.UUCP (Stanley Friesen) writes: >In article <2590@utcsri.UUCP> greg@utcsri.UUCP (Gregory Smith) writes: >> >>I strongly disagree. If you have long *x, then (char)*x ( as opposed to >>*(char*)x ) is the low-order byte of the pointed-to long and is >>portable. > > What I was trying to say is that *both* should be portable and >equivalent. >..... > And I am saying that the following *should* be portable, and >that any implementation that it doesn't work on is brain-damaged. > > register int i; > unsigned char bytes[ sizeof( long )]; /* lo-byte first */ > long input; > register char *cvptr; > > for(cvptr = (char *)&input, i = 0; i < sizeof(long); i++) > bytes[i] = cvptr[i]; > > Sarima (Stanley Friesen) Well, this is certainly false for all existing C's on big-endian machines. Even if you make the pointer "(char *)&input" point to the last byte of the datum, when you index on it, say by referencing "cvptr[1]", you get bytes *past* the last byte of the long datum. But the funny thing is, a C compiler *could* be designed to make Sarima's code work on a big-endian machine. Pointer ordering and arithmetic could be defined, in a self-consistent way, *backwards* from the machine-word ordering. Arrays and structs would be allocated backward in memory. (As a bonus, conversion between pointers and ints could involve a negation, but portable C makes no such requirements.) In essence, the abstract addressing structure imposed by this hypothetical compiler would turn our misguided big-endian machine into a virtual little-endian. This hack fails for the waverers, such as PDP-11's. (However, you could complement the 1s bit before and after index arithmetic?) Be wrong, but be consistent! Disclaimers: This was not a serious suggestion to the compiler designers of the world. It certainly has nothing to do with the official policy of Thinking Machines Corporation. :-) :-) :-) [P.S. Sarima, if this was your meaning all along, sorry to steal your thunder; I must have missed an article.] [P.P.S. Local work crunch precludes retro-flame on &array responses. But stay tuned, netters.] -- ---------------------------------------------------------- John R. Rose Thinking Machines Corporation 245 First St., Cambridge, MA 02142 (617) 876-1111 X270 rose@think.arpa ihnp4!think!rose