Question
****CODE is in C***** /* This allows the user to set or modify short and long integers and then examine the results in both decimal
****CODE is in C*****
/* This allows the user to set or modify short and long integers and then examine the results in both decimal and binary format. Programmer: Henry Walker, Grinnell College Written in C by Henry Walker (January 28, 2005) Last revised by Henry Walker (January 30, 2005) print_binary revised by Jerod Weinman (January 4, 2019) */
#include
void print_binary (int digits, int number);
int main (void) { short short_int = 1; int inte = 1; int number; char option;
printf (" "); printf ("This program allows experimentation with different sizes of integers "); printf ("Initial integer values: decimal binary "); printf (" Short (16-bit) integer:%13hd ", short_int); print_binary (16, short_int); printf (" "); printf (" Regular (32-bit) integer:%13d ", inte); print_binary (32, inte); printf (" ");
while (1) { printf ("Menu Options "); printf (" I - initialize each number to a value you have entered "); printf (" A - add 1 to each integer "); printf (" S - subtract 1 from each number "); printf (" M - multiple each number by 2 "); printf (" D - divide each number by 2 "); printf (" Q - Quit "); printf ("Enter option: "); scanf (" %c", &option);
switch (option) { case 'i': case 'I': printf (" Enter value that will be assigned to each integer: "); scanf ("%d", &number); short_int = (short) number; inte = number; break; case 'a': case 'A': short_int += (short) 1; inte += 1; break; case 's': case 'S': short_int -= (short) 1; inte -= 1; break; case 'm': case 'M': short_int *= (short) 2; inte *= 2; break; case 'd': case 'D': short_int /= (short) 2; inte /= 2; break; case 'q': case 'Q': printf ("Program terminated "); return 0; break; default: printf ("Unrecognized option -- please try again "); continue; } // switch printf ("Resulting integer values decimal binary "); printf (" Short (16-bit) integer:%13hd ", short_int); print_binary (16, short_int); printf (" "); printf (" Regular (32-bit) integer:%13d ", inte); print_binary (32, inte); printf (" ");
} // while(1) } // main
void print_binary (int digits, int number) { /* this function prints the bit representation of number, assuming that digits gives the number of bits in the original declaration of number
when called with short ints, digits should be 15 (at least on Pentium 4 machines) when called with regular ints, digits should be 32 */ for (int position = digits - 1; position >= 0; position--) printf ("%X", (number >> position) & 1); } // print_binary
Run integer-rep, which initially shows the binary representation of the number 1. Use the operation "A" to add 1 to the values. Then use "A" again, and again, and again. (When starting with the value 1, the integers will become 2, 3, 4, and 5.) Review the binary representation of each integer, and explain why it has the binary representation printed. 2. Use the "M" and A options to set the value to 12. Then use the "M" option 5 times, each time multiplying the values by 2 to obtain 24, 48, 96, 192, and 384. Explain why the binary representation of each integer looks as it does, including how the pattern of O's and 1's obtained changes as you go from one of these numbers to the next. 3. Try to use the "I" option to set the integer to a 5-digit positive integer (e.g., 10000), a 10-digit positive integer (e.g., 2000000000), a 15-digit positive integer, and a 20-digit positive integer. Explain what happens in each case. S . com..... 4. As in Steps 1-3, use the C program integer-rep.c and the "S" option to set the program's values to -1, -2, -3, and -5. I 5. Use the "I" option to enter -32766. Then use the "S" option four times to subtract 1 from -32766. What results do you get with the 16-bit integer form and with the 32-bit integer? Explain why you get each result. 6. Use the "A" option several times to add 1 to your result of step 5. What can you conclude about the maximum integer that can be stored in 16 bits (assuming the processing is allowed to handle both negative and positive numbers)? 7. Use a similar approach to find the maximum integer that can be stored in a 32-bit (signed) integer. Explain your result and how you got it
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started