Not to brag or anything but I answered almost every single C++ question correctly in the phone screen. Everything from multiple virtual inheritance to big O of the standard algorithms to the nature of sizeof. I was on a roll until I ran into too many endians. The interviewer asked, "How could you tell if your program is reading in big endian numbers or little endian numbers?" I vaguely knew what endian-ness was and tried to muddle through the question; I hate it when interviewee's try to muddle through something they don't know. Yet there I was, muddling with the best of em.
I think my answer had something to do with performing an AND operation on 1. Needless to say the answer I gave was wrong and the interview was over. Didn't matter how good my C++ knowledge was if I didn't know what Endian-ness was all about.
Big Endian vs. Little Endian [1]. Basically it has to do with byte order (like I had tried to muddle). Assume you put the number "1" into an int. How is the int layed out? Where is the byte with the 1 in it located?
If you are little endian:
Address : 0 1 2 3
Bytes : 00000001 00000000 00000000 00000000
Big Endian:
Address : 0 1 2 3
Bytes : 00000000 00000000 00000000 00000001
By default, x86 machines seem to use little-endian encoding. Therefore the simple way to test whether or not you have a big endian or little endian is:
int main(int argc, char** argv)
{
int a = 1;
char* c = reinterpret_cast<char*>(&a);
if (*c == 1)
{
std::cout << "Little Endian" << std::endl;
}
else
{
std::cout << "Big Endian" << std::endl;
}
}
I feel much more ignorant than when I started because this is a simple question. Guess it doesn't matter sometimes how much C++ you know if you don't know the basics. I checked the above example with other code online [2]. For more information about big/little endian see reference [3].
References
No comments:
Post a Comment