Take the 2-minute tour ×

First of all, I am using a User Defined function called showBin() that would print a binary of a integer.As you can see this function checks each bit of a given number from right most position to left most position(MSB to LSB).

Am using a Union with two members, I have stored 512 in its first member(short unsigned int a),(we know that normally(in Big Endian) the binary of 512 is 0000 0010 0000 0000) and in next step tried to print the values of array c[0] and c[1]
As my Machine is Little Endian it clearly outputs: 0 2,Now this means that under the hood(in memory) the binary is stored as: 0000 0000 0000 0010

But when i print the value of a using showBin() it shows 0000 0010 0000 0000 which is in normal(Big Endian) way, why does this not output in Little Endian way i,e 0000 0000 0000 0010 ???? .even if my computer stores the number in BIG Endian then it should output 2 0 right?? ???? Am really Confused..????..can any one Please explain this behavior in simple words.

void showBin(unsigned short int a);
int main()
    union myun
        short unsigned int a; //2 bytes
        char c[2]; //2 bytes
    union myun som;
    printf("%hu %hu \n",som.c[0],som.c[1]);
    printf("512 in Binary:");showBin(som.a);printf("\n");//Doubt here..
return 0;

void showBin(unsigned short int a)
     int i;
     unsigned short int mask=0;
     unsigned short int res=0;
        mask=0;//resetting imp..
        mask=mask | (1<<i);
        res=a & mask;
share|improve this question
By your logic, int x = 1; should make x equal to 0x01000000 on a 32-bit little-endian machine. Quite clearly, that's not how it works. You can only experience endianness when inspecting data directly in memory; values in variables that are treated as their proper data type will not exhibit any endian-dependent behavior. –  unwind Apr 24 at 13:38
@unwind Ok i think am getting something.so that means that any values stored in variable will be stored always in (normal)BIG Endian fashion???? –  Mudassir Hussain Apr 24 at 13:42
There is no endianness concept relevant to the values as long as you treat them as their "natural" type. A value might be in a register, which might not have addressable bytes, even. It doesn't matter. It only matters for values stored in memory, when you access them byte by byte. –  unwind Apr 24 at 13:44

2 Answers 2

Your implementation of showBin is not endianess dependent.

Consider your unsigned integer mask, left shifting it by one will be exactly the same as multiplying it by two on all platforms. In fact, C and C++ are built in such a way that endianess does not affect anything you do with standards compliant code.

If you wish to experience endianess, you will need to do something that is undefined in standards C and C++, but actually fairly simple:

int x = 42;
char* m = (char*)&x;
for(size_t i = 0; i < sizeof(x); ++i) {
    unsigned char value = (unsigned char)m[i];
    // do something to your bytes - e.g. use showBin to display the binary representation
share|improve this answer
am sorry am not comfortable with c++,it was mistake that i added c++ tag.i removed it.can you please explain that in c. –  Mudassir Hussain Apr 24 at 13:44
It does not actually change anything beyond the syntax of the two casts –  gha.st Apr 24 at 13:50

The shift and mask operations do not reveal the system's endianness. Only the order of the bytes in memory indicates endianness.

For C, endianness is mostly an implementation detail that is abstracted away from you. The instructions that your target likely uses take registers as operands and those registers are logically one-for-one with the int type(s). So the shift and mask operations are usually native. Endianness comes into play with how this target executes a LOAD or STORE operation (reading/writing registers from/to memory).

Regardless of the endianness of your target, the left shift operator << will end up compiling to the native left shift instruction. And that left shift will operate the same way that you think it should (shifting towards more signficant bits). But don't call it "big endian" because it's not really a matter of endianness.

Endianness is not about "How do I write this numeral on the whiteboard?", it's about "How do I serialize this data?"

From wikipedia (emphasis mine):


share|improve this answer

Your Answer


