Question

In: Computer Science

Write a program in C that allows you to determine the endianness of your computer. Hint:...

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}

Solutions

Expert Solution

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.


Related Solutions

For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write and implement an interactive C++ program to find...
For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than or equal to a given value of n, using the algorithm known as the Sieve of Eratosthenes. A prime number p is an integer greater than 1 that is divisible only by 1 and p (itself). The algorithm begins by initializing a set container to contain all the integers in the range 2 to n....
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Computer Architecture and Organization(c++) Write a C++ program that prompts the user to enter a hexadecimal...
Computer Architecture and Organization(c++) Write a C++ program that prompts the user to enter a hexadecimal value, multiplies it by ten, then displays the result in hexadecimal. Your main function should a) declare a char array b) call the readLn function to read from the keyboard, c) call a function to convert the input text string to an int, d) multiply the int by ten, e) call a function to convert the int to its corresponding hexadecimal text string, f)...
Write an interactive program in c++ that allows the user toenter 2-15 vectors (use the...
Write an interactive program in c++ that allows the user to enter 2-15 vectors (use the if statement). Display the resultant on the screen.
write a complete C++ program that allows the user to choose from one of several options....
write a complete C++ program that allows the user to choose from one of several options. The program will display a menu and each option will be in a separate function. The program will continue looping until the user chooses to quit. The module descriptions are given below. a. Prompt for and take as input 3 floating point values (l, w, and h). The program will then calculate the volume (volume = l*h*w ). The program should then output to...
Write a C++ program that allows a user choose item to purchase from a list; ask...
Write a C++ program that allows a user choose item to purchase from a list; ask for the amount to calculate the cost of the purchase; offer to add a tip; add a delivery fee based on the subtotal; then calculate the total amount that the user needs to pay. ---------------------------------- ----- GROCERY SHOPPING ITEMS ----- Milk     - $5.99 / gallon Egg      - $6.99 / dozen Cheese   – $10.98 / 8oz Pasta    – $2.75 / packet ---------------------------------- Other Values to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT