Reputation: 1
I am newbie to big endian system and trying to understand basic behaviour in it. I observed even when I assign same value to different data type, the value for u64 is different, while little endian system has same result.
Psuedo-Code and Explanation for above problem statement:
union types {
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
};
I am assigning 1 to each type and print u64 values. Sample Code;
union types a={0};
a.u8 = 1;
printf("A. u64 %016lx\n", a.u64);
a.u16 = 1;
printf("B. u64 %016lx\n", a.u64);
a.u32 = 1;
printf("C. u64 %016lx\n", a.u64);
a.u64 = 1;
printf("D. u64 %016lx\n", a.u64);
In Big Endian System results are different
A. u64 0100000000000000
B. u64 0001000000000000
C. u64 0000000100000000
D. u64 0000000000000001
While in little endian system, result is always same (0x1).
Can someone please explain why is this so ? Is there any value we can assign which will have same values for all types in big endian System as we have in little endian?
Upvotes: 0
Views: 177
Reputation: 25
Big endian is also for network where data msb starts at first or lower address and address increments and lsb reaches. For the output that you gave it is correct that way. lsb for u8 u16 u32 and u64 are 1 while rest are zeros. msb is the first byte of buffer. Little endian will have lsb at start address which is first address of buffer and it is easy to see why it is always 0x1 for print of union hex buffer. msb for little endian will be at next highest address for datatype size. Though note that pointer values of u8 u16 u32 u64 if you print for little endian and big endian and interpret that will give further clarity.
Upvotes: 0