Author: J. Wesley Cleveland
Date: 09:21:26 05/12/03
Go up one level in this thread
On May 11, 2003 at 19:58:03, Robert Hyatt wrote:
>On May 11, 2003 at 17:24:41, Gareth McCaughan wrote:
>
>>Robert Hyatt wrote:
>>
>>>>> left-to-right means "ascending address order"...
>>>>
>>>> If that's what you meant by it, then that bit was correct.
>>>> But "from will be the MSbyte, piece will be the LSbyte"
>>>> is guaranteed only if you know the architecture the
>>>> thing is being compiled for. Ascending address order
>>>> needn't mean most significant byte first.
>>>>
>>>
>>> In the context given, my answer is correct. He put four chars into a
>>> struct. The first char will definitely be the "MSB" which is the left-most
>>> byte or the byte with the lowest memory address. The second char will follow
>>> sequentially...
>>>
>>>There can be no exception to that.
>>
>>"Most significant" doesn't mean "lowest address". It means "represents
>>the largest power of 256 when viewed as an integer". And, as I said,
>>on ia32 the most significant byte has the *highest* address within
>>a word.
>
>THat's correct. And when you put four bytes into a word, which is a memory
>unit of four consecutive bytes, the first byte _always_ goes in the leftmost
>byte of that word, with _no_ exceptions. Note that I am _specifically_ talking
>about this case and _only_ this case:
>
>struct {
>
> char a;
> char b;
> char c;
> char d;
>
>}
>
>
>Wherever the struct is located in memory, a will always be at address + 0,
>b at address + 1, etc. That is what I would call "MSB to LSB order within
>the structure."
>
>As I said clearly, I am not stepping into endianness issues whatsoever here,
>as it is meaningless in the above struct context.
>
>>
>>$ cat endian.c
>>#include <stdio.h>
>>int main(void) {
>> union { struct { char a,b,c,d; }; int i; } x;
>> x.i = 0x01020304;
>> printf("%d %d\n", x.a, x.b);
>> return 0;
>>}
>>$ gcc endian.c
>>$ ./a.out
>>4 3
>>
>>The first char in the struct gets the lowest address, and
>>the *least* significant byte of the integer.
>
>However, that wasn't the point. And I clearly mentioned that endianness
>didn't figure in since we were not talking about referencing the struct as
>a single word for any 32 bit operations...
>
>If you change to that "gear" then you are correct for little-endian machines
>like the PC.
x.i & 0xff == x.a on a little-endian machine. Is this also true on a big-endian
machine ?
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.