Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!decwrl!glacier!hplabs!sdcrdcf!psivax!friesen From: friesen@psivax.UUCP Newsgroups: net.lang.c Subject: Re: Byte order (retitled) Message-ID: <1117@psivax.UUCP> Date: Mon, 21-Apr-86 16:35:42 EST Article-I.D.: psivax.1117 Posted: Mon Apr 21 16:35:42 1986 Date-Received: Wed, 23-Apr-86 22:45:27 EST References: <7046@cca.UUCP> <7158@cca.UUCP> <1104@psivax.UUCP> <2590@utcsri.UUCP> Reply-To: friesen@psivax.UUCP (Stanley Friesen) Organization: Pacesetter Systems Inc., Sylmar, CA Lines: 60 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. Actually it is when going the other way, from a pointer to a small integer to a pointer to a large integer that you *have* to use the *(long *)x form, otherwise you will not get the whole thing. >Besides, your solution would get really weird on things like the PDP >where lo bytes come first in words but lo words are second in longs ( >admittedly a silly setup ). What solution? I was only talking about the *semantics* of a cast. I was saying that pointer conversions should result in pointers to rational objects, not wierd things like the the high order fragment of a larger entity. This is a statement about desired behavior *not* implementation. > >Pointer conversions between 'pointer-to-x' and 'pointer-to-y' should be >no-ops whenever possible, since things like (struct foo *)malloc(...) >are done so often. If the above scheme were to be used, then promoting, >say, the (char *) from malloc to a (long *) would require masking off >the lower 2 ( or whatever ) bits of the address. I agree, pointer conversions *should* be no-ops. The article I was responding to had stated that big-endian pointers and little-endian pointer conversions could *both* be no-ops and I was asking HOW?. As far as I can see, in order to get rational, intuitive *semantics* on a big-endian machine ponter conversions *cannot* be no-ops. That is all I was saying. So again, on a big-endian machine, how do you get the desired *semantics* and still leave pointer conversions as no-ops? > >The following portably strips a long into bytes: > >int i; >unsigned char bytes[ sizeof( long )]; /* lo-byte first */ >long input; > for(i=0;i bytes[i] = input >> (i<<3) > 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) UUCP: {ttidca|ihnp4|sdcrdcf|quad1|nrcvax|bellcore|logico}!psivax!friesen ARPA: ttidca!psivax!friesen@rand-unix.arpa