Author: Robert Hyatt
Date: 11:05:04 05/13/03
Go up one level in this thread
On May 12, 2003 at 12:21:26, J. Wesley Cleveland wrote:
>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 ?
No.
That is the endian issue exactly...
Same thing happens with network functions that require a 4-byte IP address.
Endianness screws that up and there are specific functions to do the conversion
(htonl, htons, etc.)
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.