a single function (and the include directive to your header file) to perform the endian swap operation of the 32-bit input argument passed to this function. Endian ness refers to how the bytes are stored in memory. In a 32- bit word, there are 4 bytes – B0, B1, B2, and B3. Let us assume that B0 refers to the least significant byte and B3 the most significant byte. The function will swap (exchange) bytes B0 and B3, as well as bytes B1 and B2. One team member, and only one team member, will be responsible for the source code in this file, though collaboration with other team members may be done if needed.
Below is the code with an example run of the function:
#include <stdio.h>
int endian_swap (int number) {
unsigned char left,second,third, right;
int newno;
// Extract the four bytes from the 32 bit integer
right = number & 0xFF;
third = (number >> 8) & 0xFF;
second = (number >> 16) & 0xFF;
left = (number >> 24) & 0xFF;
// Generate a new number with the bytes switched
//OR the four bytes (after bit-shifting) to get the new
number
newno = (right << 24) | (third << 16) | (second
<< 8) | left;
return (newno);
}
int main()
{
int i = 4326051, j;
printf ("Original number in hex: %08x\n", i);
j = endian_swap (i);
printf ("Endian switched number in hex: %08x\n", j);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.