#include<stdio.h>
#define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1
int endian() {
int i = 1;
char *p = (char *)&i;
if (p[0] == 1)
return LITTLE_ENDIAN;
else
return BIG_ENDIAN;
}
In the above code, you assume that there are only two possible "endian"
values.
int reverseInt (int i) {
unsigned char c1, c2, c3, c4;
if ( endian() == BIG_ENDIAN ) {
return i;
} else {
c1 = i & 255;
c2 = (i >> 8) & 255;
c3 = (i >> 16) & 255;
c4 = (i >> 24) & 255;
return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}
}
In the above code, you assume that an int has a sizeof 4 characters.
int main(void)
{
if(endian())
puts("Big Endian Machine");
else
puts("Small Endian Machine");
printf("%d",reverseInt(5));
return 0;
}
"Endianness" usually refers to how elements are ordered when storing
multi-element entities, and is usually referenced with respect to the order
that "bytes" are used to store "integers". For instance, if it takes
2 "bytes" (A and B) to store an "integer", there are two ways that these
bytes can be ordered in memory as they are stored:
A followed by B
and
B followed by A
If an integer takes four bytes (A, B, C, and D), then there are (4 * 3 * 2 *
1) or 24 ways to order these bytes in memory:
A followed by B followed by C followed by D
A followed by B followed by D followed by C
A followed by D followed by B followed by C
...
D followed by C followed by B followed by A
In the simple 2-element case, when the byte containing the most-significant
portion of the compound value is stored first, the order is called "Big
Endian". When the least-significant portion is stored first, the order is
called "Little Endian".
In the more complex n-element cases, "Little Endian" and "Big Endian" are
two of the many possible orders.
Now, to your code....
in the endian() function, you return one of two values, based on whether the
least-significant portion of the value is stored first or not. This binary
return (LITTLE_ENDIAN or BIG_ENDIAN) can only be valid if an int takes the
same space as two (and only two) char elements. If an int took more space
(say, 4 char elements), then endian() would have to return one of many more
possible ordering names (one of 24 names, for a 4 char int, for instance).
Clearly, your endian() function assumes that sizeof(int) == 2
But, your reverseInt() function clearly assumes that sizeof(int) == 4, at
least for LITTLE_ENDIAN values.
You should know that your compiler may actually use a different value for
sizeof(int).
In summary, your code is flawed.
I tested it on my PC (On Pentium 4) and this is the output:
Small Endian Machine
83886080.
I am baffled as I was expecting 5 to be printed or is it that I am
missing something completely ?
Probably i have completely misunderstood the idea of endianness
Yes.
Any help is appreciated.
First off, as far as the C language is concerned, there is no need to
determine the "endianness" of stored values. Where it is important (i.e.
when transferring binary values through a file), your compiler's
documentation should tell you the exact format. Otherwise, you don't need
to worry about it.
Secondly, you should first determine how "wide" your compiler makes
integers. Look for the sizeof(int) value, as this will give you the number
of "bytes" that make up an integer.
Thirdly, if sizeof(int) is greater than 2, your compiler may choose some
order other than "Little Endian" /or/ "Big Endian" to store the values
in. "Little" and "Big" endian only name two of the possible orders.
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------