It’s common in systems code to need to switch data between big-endian and little-endian representations. This is because networks represent multi-byte integers using big-endian representation (the most-significant byte in a multi-byte integer is transmitted first), whereas x86-family processors store multi-byte integers using little-endian representation (the least-significant byte in a multi-byte integer is stored first).
1. Complete this function, which translates an integer from big-endian
representation to little-endian representation by swapping bytes. For
instance, big_to_little(0x01020304)
should return 0x04030201
. Your return
statement must refer to the u.c
array, and must not refer to x
. This
function is compiled on x86-64 Linux (as every function is unless we say
otherwise).
unsigned big_to_little(unsigned x) {
union {
unsigned intval;
unsigned char c[4];
} u;
u.intval = x;
return ______________________________________;
}
2. Complete the function again, but this time you should write a single
expression that refers to x
(possibly multiple times).
unsigned big_to_little(unsigned x) {
return ______________________________________;
}