Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain this "endian-ness" function for me?

Tags:

c++

endianness

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?

like image 827
SIr Codealot Avatar asked Mar 28 '10 00:03

SIr Codealot


2 Answers

If you have a little-endian architecture, i will look like this in memory (in hex):

01 00 00 00
^

If you have a big-endian architecture, i will look like this in memory (in hex):

00 00 00 01
^

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 the char* will be 01 if you are on a little-endian architecture and 00 if you are on a big-endian architecture.

When you return that value, 0 is converted to false and 1 is converted to true. So, if you have a little-endian architecture, this function will return true and if you have a big-endian architecture, it will return false.

like image 176
James McNellis Avatar answered Oct 24 '22 04:10

James McNellis


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 at ptr+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.

like image 43
sth Avatar answered Oct 24 '22 05:10

sth