Re: Byte ordering and array access
- From: "stathis gotsis" <stathisgotsis@xxxxxxxxxxx>
- Date: Sun, 12 Feb 2006 21:32:19 +0200
"Robin Haigh" <ecl6rsh@xxxxxxxxxxx> wrote in message
news:dsnste$kon$1@xxxxxxxxxxxxxxxxxxxxxx
You need two different orderings before you can discuss how they relate tobase-256,
each other.
When you store a 16-bit unsigned integer value into 2 bytes of
byte-addressable memory (and this didn't arise before byte-addressing), by
common custom and convention (but no absolute rule) you encode it
i.e. the byte values you store will be x/256 and x%256.is
On that assumption, you now have an ordering by significance -- one byte
the "big" byte -- and also an ordering by memory address, so you can talkendianness.
about which byte (by significance) is the low-address byte, i.e.
Yes, i was curious if there are other encodings in real systems, other than
this common one, leading to other possibilities for endianness.
With the bits involved in bitwise operations, you have an ordering byor
significance, but only that. There's no low-address end or left-hand end
any other positional description. You can certainly access the LSB, but
every way of doing so refers to it by significance, essentially. So you
can't talk about relative bit-ordering, because you can't see anything for
it to be relative to.
So, i come to the conclusion that shifting operations hide endianness from
the programmer. Maybe one could reveal endianness this way?
#include <stdio.h>
int main(void)
{
int i=0;
unsigned int a = 0xabcdabcd;
unsigned char *b;
b=(unsigned char *)&a;
while (i<sizeof(a))
{
printf("%d byte: %x\n",i+1,b[i]);
i++;
}
return 0;
}
Of course this changes when you serialise the bits in a byte onto a serialsee
communications line. Then, you do have another ordering, so the hardware
does have to agree on the bit-endianness and reassemble the byte values as
transmitted. But, unlike the cpu vendors, the bus and network vendors (by
some miracle) do have this all sorted out, and we don't actually get to
bit-swapped bytes, so we treat it as a non-issue. The danger that youfear
was potentially real, but has been averted.
The terms "left-shift" and "right-shift" are motivated by the fact that in
America and many other countries, when numbers are written down in
place-value notation, we write the big end on the left. If numbers were
normally written the other way round, e.g. 000,000,1 for a million, the
names would have been reversed. This hasn't got anything to do with cpu
architecture.
Yes, that is clear to me now.
.
- Follow-Ups:
- Re: Byte ordering and array access
- From: Robin Haigh
- Re: Byte ordering and array access
- From: Rod Pemberton
- Re: Byte ordering and array access
- References:
- Byte ordering and array access
- From: Benjamin M. Stocks
- Re: Byte ordering and array access
- From: stathis gotsis
- Re: Byte ordering and array access
- From: Vladimir S. Oka
- Re: Byte ordering and array access
- From: stathis gotsis
- Re: Byte ordering and array access
- From: Chris Torek
- Re: Byte ordering and array access
- From: stathis gotsis
- Re: Byte ordering and array access
- From: Robin Haigh
- Byte ordering and array access
- Prev by Date: Re: Command Line Interface (CLI): your recommendations
- Next by Date: Re: How do I convert an int to binary form?
- Previous by thread: Re: Byte ordering and array access
- Next by thread: Re: Byte ordering and array access
- Index(es):
Relevant Pages
|