0
votes

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;
    som.a=512;
    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;
     for(i=15;i>=0;i--)
     {
        mask=0;//resetting imp..
        mask=mask | (1<<i);
        res=a & mask;
        (res==0)?printf("0"):printf("1");
     }
}
2
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
@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
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

2 Answers

2
votes

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
}
0
votes

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):

In computing, memory commonly stores binary data by organizing it in 8-bit units (bytes). When a data word uses multiple such units, the order of storing these bytes into memory becomes important. The terms endian and endianness, refer to how bytes of a data word are ordered within memory.