Language C++ PLEASE ANSWER THESE 4 SHORT QUESTIONS This exercise should help you
ID: 3574053 • Letter: L
Question
Language C++
PLEASE ANSWER THESE 4 SHORT QUESTIONS
This exercise should help you get clear in your own mind how numbers are stored inside a computer, and what the relationship between the bit patterns for chars, short-int's and int's is, as well as demonstrates how to read and write binary data for all the standard data types.
Here is a sample program and data file (sample_binary.dat). It reads the data file 5 different times: first into a char array, then into a short int array, then into int, float and double arrays. It outputs the values in the arrays (in hex and in integer for the integer data types).
Study the program to understand what it does, then compile and run it, and examine the output. Take some time to make sure the output makes sense to you. Note that the bytes appear reversed (from the hex values for chars) when output in hexadecimal as short ints and ints. This is because Intel processors are "little-endian" – they store the least-significant byte first in memory. However, since we humans expect the most-significant digits first, when a little-endian value is output in hex, c++ outputs the most-significant byte first to keep us happy.
Verify that the integer values output for the first 2 bytes of the char output, 0x74 and 0x68 are correct. You can do that by googling up the ascii table, and finding the hexadecimal and integer values of the characters. Then calculate the integer value of the first short int value, sibuf[0], which is hexadecimal 0x6874, and convince yourself that the integer value of 26740 is correct. If you're not sure how to do that: try 256 * 'h' plus 't' (256*104 + 116) and see if it does add up to the same value as the first short int, sibuf[0].
Then, explain in your own words in a text file:
1. How to calculate the value of a short int and of an int by looking at the values in hexadecimal. Give an example for a short int and an int (suggestion: work out the values for sibuf[1] and ibuf[1]).
2. How the same bytes can represent such different values when read into the different data types
3. Explain in your own words why the hexadecimal values for the chars, short ints and ints aren't in the same order. If you're not clear, google "little-endian" and do a little reading.
4. Take a stab at explaining to yourself why the float and double values can be so wildly varying. (Hint, some of the bits must represent the exponent.) You don't need to write your explanation down; but if you're curious, google IEEE 754. (Wikipedia's page is rather involved, but there's a nice (vaguely) simple example at http://class.ece.iastate.edu/arun/Cpre305/ieee754/ie4.html . (A little googling should also turn up an IEEE 754 converter, which might be helpful to make sense of the values the sample program puts out.)
Name your explanation file binary_input_output.txt
-------------------Sample programs are posted below----------------------------------------
1. Sample Program:
----------------------------------------------------------------------------------------------
2. sample_binary.dat
the quick brown fox jump
----------------------------------------------------------------------------------------------
Explanation / Answer
the values of short int and int are calculated using sibuf
for (int i=0;i<12;i++)
cout << setw(6)<< dec << sibuf[i]<<" ";
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.