G
G Patel
Code in question (assuming CHAR_BIT = 8 system):
union
{
unsigned int whole;
unsigned char bytes[sizeof(int)];
} var;
var.whole = 0xFF;
if( var.bytes[0] == 0xFF )
printf("\nLITTLE ENDIAN\n");
else if( var.bytes[sizeof(int)-1] == 0xFF )
printf("\nBIG ENDIAN\n");
else
printf("\nHUH???\n");
I'm wondering about the portibility of the above ENDIAN tester code
(don't worry about CHAR_BIT != 8 as an issue).
I've read some really knowledgeable posts on clc before that kept
emphasizing the fact that C pastes a small abstraction layer over
memory/hardware. And that 2 contiguous bytes in C's layer is not
necessarily 2 contiguous in RAM (or process memory space) -or- the
order of the bytes in C's layer is not necessarily the same as
hardware. So with this in mind, can the above code be made more
portable?
Thanks
Gaya
union
{
unsigned int whole;
unsigned char bytes[sizeof(int)];
} var;
var.whole = 0xFF;
if( var.bytes[0] == 0xFF )
printf("\nLITTLE ENDIAN\n");
else if( var.bytes[sizeof(int)-1] == 0xFF )
printf("\nBIG ENDIAN\n");
else
printf("\nHUH???\n");
I'm wondering about the portibility of the above ENDIAN tester code
(don't worry about CHAR_BIT != 8 as an issue).
I've read some really knowledgeable posts on clc before that kept
emphasizing the fact that C pastes a small abstraction layer over
memory/hardware. And that 2 contiguous bytes in C's layer is not
necessarily 2 contiguous in RAM (or process memory space) -or- the
order of the bytes in C's layer is not necessarily the same as
hardware. So with this in mind, can the above code be made more
portable?
Thanks
Gaya