Portability vs. Endianness
Dave Gillett
dgil at pa.reuter.COM
Tue Mar 19 09:12:35 AEST 1991
In <2628 at ksr.com> jfw at ksr.com (John F. Woods) writes:
> Bytes[0] = (var >> 24) & 0xFF;
> Bytes[1] = (var >> 16) & 0xFF;
> Bytes[2] = (var >> 8) & 0xFF;
> Bytes[3] = var & 0xFF;
>This code is guaranteed.
I don't think that the standard guarantees that chars are eight bits.
I will agree that that's probably even more common than 4-byte longs, but
even this assumption about word sizes cannot, IMHO, be guaranteed to be
portable. (6-bit and 9-bit chars have gone out of style, but I've used
machines with 16-bit word sizes that would *not* have produced the desired
result for this code.)
Dave
More information about the Comp.lang.c
mailing list