Big endian and Little endian

Thread Starter

asilvester635

Joined Jan 26, 2017
73
I wrote a program to find out whether my computer is big or little endian. Below is my code and its output in decimal are 87 d6 12 0. I know that big endian stores the most significant byte in the smallest address (that number will be printed first), and little endian stores the least significant byte in the smallest address (that byte will be printed first). My problem is determining which end of the output is the most significant bit and which is least significant bit. How do I determine that?

main.cc
Code:
int main() {

    int n = 1234567;
    unsigned char* byte = (unsigned char*) (&n);
    printf("%x %x %x %x\n", (int)byte[0],
                            (int)byte[1],
                            (int)byte[2],
                            (int)byte[3]);

} // end of main
 

WBahn

Joined Mar 31, 2012
30,062
Bits are atomic within a byte, so they don't have an ordering in the sense you are thinking. You have a value stored in memory. You access it as a value. You can perform operations to interact with that value in ways that allow you to access specific information or make specific changes to that value. Those interactions are self-consistent. For instance, if you AND the value with 1 then you will get zero if the least significant bit is set and one if it is not because the value 1 has a 1 in the least significant bit and zeroes elsewhere.
 

MrSoftware

Joined Oct 29, 2013
2,200
Your PC is little endian. The most significant values are stored in the higher addresses. It printed the most significant byte last. It would have been more obvious if you assigned your int like this:

int n = 0x01234567; // Notice the 0x to specify that this is a hex value

Also depending on your compiler (and your optimization settings), your code might not work as you expect. This "(int)byte[3]" tells the compiler, take the byte located at byte+3 and treat it as an int, which by the way is 4 bytes in size (in this case, it could easily be another size). There is no guarantee that those other 3 bytes will be zeros, and your new 4-byte int can easily contain your one byte plus 3 random non-zero bytes that you weren't expecting to be non-zero. I've fixed several bugs caused by similar code. A safer way is this:

int fourthByte = ((int)(byte[3]) & 0xFF;

Here you have explicitly masked out all but the single byte that you're after and ensured that the only non-zero byte in your new int will be your one byte copied from location byte[3].

Try this:

int n = 0x01234567;
unsigned char* byte = (unsigned char*) (&n);
printf("%x %x %x %x\n", ((int)(byte[0]) & 0xFF),
((int)(byte[1]) & 0xFF),
((int)(byte[2]) & 0xFF),
((int)(byte[3]) & 0xFF));
 

Thread Starter

asilvester635

Joined Jan 26, 2017
73
Your PC is little endian. The most significant values are stored in the higher addresses. It printed the most significant byte last. It would have been more obvious if you assigned your int like this:

int n = 0x01234567; // Notice the 0x to specify that this is a hex value

Also depending on your compiler (and your optimization settings), your code might not work as you expect. This "(int)byte[3]" tells the compiler, take the byte located at byte+3 and treat it as an int, which by the way is 4 bytes in size (in this case, it could easily be another size). There is no guarantee that those other 3 bytes will be zeros, and your new 4-byte int can easily contain your one byte plus 3 random non-zero bytes that you weren't expecting to be non-zero. I've fixed several bugs caused by similar code. A safer way is this:

int fourthByte = ((int)(byte[3]) & 0xFF;

Here you have explicitly masked out all but the single byte that you're after and ensured that the only non-zero byte in your new int will be your one byte copied from location byte[3].

Try this:

int n = 0x01234567;
unsigned char* byte = (unsigned char*) (&n);
printf("%x %x %x %x\n", ((int)(byte[0]) & 0xFF),
((int)(byte[1]) & 0xFF),
((int)(byte[2]) & 0xFF),
((int)(byte[3]) & 0xFF));
Thanks for the clarifications.
 
Top