In: Computer Science
Write a program in C that allows you to determine the endianness of your computer. Hint: use unsigned char* ptr.
Your task is to explain this code. Make sure that you are detailed in your explanation by explaining the significant lines. In particular, correct explanation of lines 12, 13, 15, 18, 21, 22, 24, 25, 26 and 27 will earn 1 point each. Additionally, explain the overall purpose of the logical operations in lines 24-27 for 2 points. Your explanation should not just reiterate the code. For example, don’t simply say that the for loop iterates 4 times, but rather, explain why and what doing so accomplishes. It would be beneficial to run the code and examine memory contents in gdb.
Here’s an example explanation for line #20: Line #20 prints out the hexadecimal value of x to the console. The %#010x format specifier specifies that the value is printed in hexadecimal (the ‘x’ flag). The # indicates to prepend “0x” to the number. The ‘0’ flag following it indicates that the number should be padded by 0s at the left. The number following it, in this case ‘10’ is the total width of the number we want to display, including the ‘0x’. For example, if the value of x is 1C, then 6 0s are padded to fill up the 10-character space.
1/*
2 * endian.c
3 * Determines endianess. If endianess cannot be determined
4 * from input value, defaults to "big endian"
5 * Bob Plantz - 22 June 2009
6 */
7
8#include
9
10 int main(void)
11{
12 unsigned char *ptr;
13 int x, i, bigEndian;
14
15 ptr = (unsigned char *)&x;
16
17 printf("Enter a non-zero integer: ");
18 scanf("%i", &x);
19
20 printf("You entered %#010x and it is stored\n", x);
21 for (i = 0; i < 4; i++)
22 printf(" %p: %02x\n", ptr + i, *(ptr + i));
23
24 bigEndian = (*ptr == (unsigned char)(0xff & (x >> 24))) &&
25 (*(ptr + 1) == (unsigned char)(0xff & (x >> 16))) &&
26 (*(ptr + 2) == (unsigned char)(0xff & (x >> 8))) &&
27 (*(ptr + 3) == (unsigned char)(0xff & x));
28 if (bigEndian)
29 printf("which is big endian.\n");
30 else
31 printf("which is little endian.\n");
32
33 return 0;
34}
Line 12: Declaring a unsigned character pointer, because dereferencing the character pointer can give the value at particular memory without the signed notation. Line 13: Declaring variable to read user input as integer, and to use for counter. Line 15: Get the address of the first byte of user entered integer.. int is 4 byte. &x makes ptr point to first byte only. Line 18: %i is used to read the value in user specified base. It can read decimal, octal or hex value. Line 21: The counter runs for 4 bytes, because integer contains 4 bytes. Hence loops runs till i < 4. Line 22: We print the address of each byte and then unsigned hex version of the value on that byte. Line 24: x >> 24, Shifts the bytes to left side on user input integer. So we basically compare the value of first memory location with the value of left 8 bits of the user entered number. Line 25: x >> 16, We shift right by 2 bytes, So that we get the value of second leftmost byte and then compare it with 2 bytes of memory. Line 26: x >> 8, We shift right by 1 bytes, So that we get the value of third leftmost byte and then compare it with 3rd bytes of memory. Line 27: 0xff means the least significant byte, and then compare it with 4th byte of memory. Line 24 to 27: The Big Endian machine stores the Lowest byte of the numbers on the higher memory locations, and highest byte on the lower memory locations.. So if 0x12345678 need to be stored in big endian machine starting from address 100, then location 100 contains: 12 location 101 contains: 34 location 102 contains: 56 location 103 contains: 78 So, We one by one find the byte on first, second, third and fourth position.. and then compare it with bytes of numbers on first, second, third and fourth position. If they are in exactly same order, then it is a big endian machine, else it is a little endian machine.
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.