Re: byte order
- From: Maciej Katafiasz <mathrick@xxxxxxxxx>
- Date: Tue, 8 Jan 2008 14:26:34 -0800 (PST)
On Jan 8, 9:37 pm, Raymond Wiker <r...@xxxxxxxxxxxx> wrote:
Not in Common Lisp.
Not in standard C either.
You're joking, right?
// returns 1 if little endian, 0 if big
int am_i_little_endian() {
int x = 0x12345678;
char* b = (char*)&x;
return *b == (x & 0xFF);
}
Are you *absolutely* sure that's standard C, and not just
something that happens to work most of the time?
§6.2.6.1.4 of ISO/IEC 9899:1999 provides explicit provisions for
casting objects to arrays of bytes, and guarantees that such an array
will actually correspond to the in-memory representation.
The standard also gives the possibility of integer types containing
padding bits, and it is not specified how such bits can be detected.
It is, however, specified that value bits must be continuous, and that
char is the smallest integer type. Therefore, assuming 8-bit bits, the
above code is bound to be portable and reliably detect little-
endianness[1] of the host machine.
Cheers,
Maciej
[1] Little-endianness understood as "least significant value byte
comes first". An implementation using little-endian value
representation with padding bits coming before the value bits wouldn't
be reliably detected, but then, any such padding-using implementation
would need to be treated as another type of endianness anyway by any
code that cares about endianness, so it's not really an issue.
.
- Follow-Ups:
- Re: byte order
- From: Raymond Toy (RT/EUS)
- Re: byte order
- From: Vassil Nikolov
- Re: byte order
- References:
- byte order
- From: Rainer Joswig
- Re: byte order
- From: Pascal Bourguignon
- Re: byte order
- From: Jeff M.
- Re: byte order
- From: Raymond Wiker
- byte order
- Prev by Date: Re: byte order
- Next by Date: Re: Lisp and Scheme with fewer parentheses / Mathematica??
- Previous by thread: Re: byte order
- Next by thread: Re: byte order
- Index(es):
Relevant Pages
|
|