Write a program to determine whether a computer is big-endian or little-endian.
bool endianness() {
int i = 1;
char *ptr;
ptr = (char*) &i;
return (*ptr);
}
So I have the above function. I don't really get it. ptr = (char*) &i, which I think means a pointer to a character at address of where i is sitting, so if an int is 4 bytes, say ABCD, are we talking about A or D when you call char* on that? and why?
Would some one please explain this in more detail? Thanks.
So specifically, ptr = (char*) &i; when you cast it to char*, what part of &i do I get?
If
ptr
points to byte A or D depends on the endianness of the machine.ptr
points to that byte of the integer that is at the lowest address (the other bytes would be atptr+1
,...).On a big-endian machine the most significant byte of the integer (which is
0x00
) will be stored at this lowest address, so the function will return zero.On a litte-endian machine it is the opposite, the least significant byte of the integer (
0x01
) will be stored at the lowest address, so the function will return one in this case.Sure,
Lets take a look
If the machine is Little endian, then data will be in *ptr will be 0000 0001.
If the machine is Big Endian, then data will be inverted, that is, i will be
So *ptr will hold 0x0
Finally, the return *ptr is equivalent to
This is using type punning to access an integer as an array of characters. If the machine is big endian, this will be the major byte, and will have a value of zero, but if the machine is little endian, it will be the minor byte, which will have a value of one. (Instead of accessing
i
as a single integer, the same memory is accessed as an array of four chars).If you have a little-endian architecture,
i
will look like this in memory (in hex):If you have a big-endian architecture,
i
will look like this in memory (in hex):The cast to
char*
gives you a pointer to the first byte of the int (to which I have pointed with a^
), so the value pointed to by thechar*
will be01
if you are on a little-endian architecture and00
if you are on a big-endian architecture.When you return that value,
0
is converted tofalse
and1
is converted totrue
. So, if you have a little-endian architecture, this function will returntrue
and if you have a big-endian architecture, it will returnfalse
.Whether
*((char*)&i)
is byte A or byte D gets to the heart of endianness. On a little endian system, the integer 0x41424344 will be laid out in memory as: 0x44 43 42 41 (least significant byte first; in ASCII, this is "DCBA"). On a big endian system, it will be laid out as: 0x41 42 43 44. A pointer to this integer will hold the address of the first byte. Considering the pointer as an integer pointer, and you get the whole integer. Consider the pointer as a char pointer, and you get the first byte, since that's the size of a char.Assume int is 4 bytes (in C it may not be). This assumption is just to simplify the example...
You can look at each of these 4 bytes individually.
char
is a byte, so it's looking at the first byte of a 4 byte buffer.If the first byte is non 0 then that tells you if the lowest bit is contained in the first byte.
I randomly chose the number 42 to avoid confusion of any special meaning in the value 1.
Breakdown:
If firstByteOf4Is42 is true you have little-endian. If lastByteOf4Is42 is true then you have big-endian.