J
Joe Pfeiffer
BartC said:I was probably thinking of something else where the representation was
not straightforward.
DEC floating point, perhaps?
BartC said:I was probably thinking of something else where the representation was
not straightforward.
Scott Fluhrer said:That is incorrect. The 68K processor was consistently bigendian, and so
0xDDCCBBAA would be stored as the bytes (DD, CC, BB, AA).
Joe Pfeiffer said:If I wanted to check whether a machine were big-endian or little-endian,
my first choice would be to see whether the htonl macro (which converts
a host-order long to a network-order long, which we know in turn is
defined to be big-endian) changes anything. I expect that macro is
outside the scope of this newsgroup, since I'd be surprised if it were
part of the C standard.
[...]
You could also (on a machine with 8-bit bytes) declare an array of 4
chars, cast the array to a long and assign a value to it, then print the
values of the eight chars.
[...]
You could also (on a machine with 8-bit bytes) declare an array of 4
chars, cast the array to a long and assign a value to it, then print the
values of the eight chars.
This particular suggestion has cropped up a couple times already
in this thread, so maybe it's time to point out the error: Since an
array of char has no particular alignment requirement, it might not
be aligned strictly enough for a `long' (or `int' or anything else).
unsigned char c[sizeof(unsigned long)] = { 1 }; // others 0
unsigned long *lp = (unsigned long*)&c[0]; // undefined
printf("%lX\n", *lp); // undefined
If you truly want to engage in this sort of thing, do it the
other way around:
unsigned long l = 1;
unsigned char *cp = (unsigned char*)&l;
for (int i = 0; i < sizeof l; ++i)
printf("%X ", cp);
printf("\n");
That is, begin with the multi-byte value properly aligned, then
inspect its bytes with a character pointer that needs no alignment.
BartC said:I'd been looking at this diagram (top of page 461):
http://tinyurl.com/7vjemjc
But perhaps I'd misunderstood what they meant by byte 0, 1, 2 and 3. I
assumed byte 0 was least significant, the same way bit 0 is. I could
be wrong.
Eric Sosman said:[...]
You could also (on a machine with 8-bit bytes) declare an array of 4
chars, cast the array to a long and assign a value to it, then print the
values of the eight chars.
This particular suggestion has cropped up a couple times already
in this thread, so maybe it's time to point out the error: Since an
array of char has no particular alignment requirement, it might not
be aligned strictly enough for a `long' (or `int' or anything else).
unsigned char c[sizeof(unsigned long)] = { 1 }; // others 0
unsigned long *lp = (unsigned long*)&c[0]; // undefined
printf("%lX\n", *lp); // undefined
If you truly want to engage in this sort of thing, do it the
other way around:
unsigned long l = 1;
unsigned char *cp = (unsigned char*)&l;
for (int i = 0; i < sizeof l; ++i)
printf("%X ", cp);
printf("\n");
That is, begin with the multi-byte value properly aligned, then
inspect its bytes with a character pointer that needs no alignment.
Ben Bacarisse said:In all such diagrams that I've seen, bytes are numbered by address.
Byte 0 will be the byte with the lowest address. What varies from
machine to machine is the significance of byte 0 vs. that of byte 1.
However you read the numbers, I still can't see how you get BB,AA,DD,CC
from that diagram!
BartC said:If the byte numbers are simply offsets from the start address, then
the diagram doesn't show whether the high or low half is stored
first.
So it could be BB,AA, DD,CC or DD,CC, BB,AA. But it looks like
it's the latter, and it must be some other system which is mixed up.
Your best bet is to write code that works with values, regardless
of how those values are represented. Ideally, you would not even
know how the machine represents eight hundred seventeen; you should
concern yourself with finding its factors or comparing it to nine
hundred twenty-six or whatever. It is occasionally necessary to
pierce the veil, but not very often.
Kenneth said:Does it matter if I write it "six", "6", "VI", "seks",
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.