Byte order (retitled)

friesen at psivax.UUCP friesen at psivax.UUCP
Tue Apr 22 07:35:42 AEST 1986


In article <2590 at utcsri.UUCP> greg at 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<sizeof(long); ++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 at rand-unix.arpa



More information about the Comp.lang.c mailing list