How will this code work on a big end machine?

If I have a code:

uint64_t a = 0x1111222233334444;
uint32_t b = 0;
b = a;
printf("a is %llx ",a);
printf("b is %x ",b);

and output:

 a is 1111222233334444 b is 33334444

Questions :

  • Will the behavior be the same on a big end machine?

  • If I assign a value to b or execute a type, will the result be the same as the big endian?

+5
source share
3 answers

The code that you have there will work the same. This is because downcasting behavior is defined by the C standard.

However, if you did this:

uint64_t a = 0x0123456789abcdefull;
uint32_t b = *(uint32_t*)&a;
printf("b is %x",b)

Then it will be dependent on the end.

EDIT:

Little Endian: b - 89abcdef

Big Indian: B - 01234567

+9
source

When assigning variables, the compiler processes things for you, so the result will be the same for big-endian.

, big-endian.

+1
Direct assignment

will give the same result for both small and large endian.

The type of memory on the large end computer will be displayed

a - 1111222233334444 b - 11112222

0
source

All Articles