Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> #include <strings.h> // if you need string library int main (

ID: 3887998 • Letter: #

Question

#include <stdio.h>

#include <strings.h> // if you need string library

int main (int argc, char *argv[]) {

// If argv[1] exists, set filename to it, otherwise set

// filename to "Lab3_data.txt".

//

char *filename;

filename = "Lab3_data.txt"; // *** STUB ***

// Open the file and say what file it was. If opening

// failed, say so and return with error code 1.

//

FILE *in_file;

in_file = fopen(filename, "r"); // NULL if the open failed

// *** STUB ***

// Read the next hex integer value to process. We

// initialize the value read in to zero, so that if the

// fscanf fails due to bad input or end of file, we'll

// stop the loop.

//

int val_to_inspect = 0;

fscanf(in_file,"%x", &val_to_inspect);

// Loop until the value to inxpect is zero.

//

while (val_to_inspect != 0) {

printf("%x", val_to_inspect);

// Read the endpoints and selection type. We initialize

// to bad values in case the fscanf fails.

//

int right_end = -1, left_end = -1;

char type = ' ';

// fscanf(in_file, ...); *** STUB ***

// If the endpoints are bad, complain

//

// *** STUB ***

// If type type is bad, complain

//

// *** STUB ***

// Otherwise process this input

// else {

// Print the endpoints

// *** STUB ***

// Get the selected bits: Calculate the length

// of the selection, calculate a mask of all

// 1's, and use logical and to select the bits.

// Print the selection in hex.

//

// Hint: The mask of n 1's equals 2^n - 1.

//

// *** STUB ***

// For type u, print the selection value as an

// unsigned integer (using format %u)

//

// Example

// if (type == 'u') {

// printf(" = %u (unsigned) ", selection);

// }

// For types 1 or 2, calculate the value of the

// selection as a 1's or 2's complement integer

// and print the result. Note we skip leading

// 0's, so the length of the selection is key.

//

// Hint: In n bits, 011...11 = 2^n-1 is the

// largest positive 1's or 2's complement

// integer. If bitstring b begins with a 1,

// then b (unsigned) - 2^n is b as an n-bit 2's

// complement value.

//

// *** STUB ***

// }

// Read the next value to inspect and continue the loop

//

val_to_inspect = 0; // *** STUB ***

}

printf(" Program quitting ");

return 0;

}

Thisprogram should process data contained 1n aIile: the Iilename is s mmand-li (argv [l]) or defaults to Lab3_data.txt. pecified as a co ne argument Here is pseudocode for the program Read a hexadecimal integer (as a 32-bit integer) while the integer is nonzero Read two integers specifying right and left endpoints or a 32-b1t integer. Read a type character u, l, or Select the n-bit substring of the integer specified by the endpoints (where n is the length of the selection) . Print the substring as a hexadecimal value and as an n-bit unsigned, l's complement, or 2's complement integer (as specified by the type Read the next hexadecimal lnteger 1234abcd Example of input: Since the last 12 bits of 1234abcd are 1011 110 1101, we calculate 11100 as the bitstring and print out hex lc, which is 28 as a 5-bit unsigned integer. (In 5-bit1's ement For each request, you should verify that the endpoints are legal for a 32-bit integer, that the right endpoint is a the left endpoint, and the type is u, 1, or 2. (If not, generate an error message and go on to the next set of data.)

Explanation / Answer

#include <stdio.h>

#include <strings.h>   // if you need string library

int main (int argc, char *argv[]) {

   char *filename;
   if(argc > 1) filename = argv[1];
   else filename = "Lab3_data.txt"; // *** STUB ***
   // Open the file and say what file it was. If opening
   // failed, say so and return with error code 1.
   FILE *in_file = NULL;
   in_file = fopen(filename, "r"); // NULL if the open failed
   if(!in_file)
   {
       printf("Opening of %s failed ", filename);
       return 1;
   }
   printf("File named "%s" opened successfully ", filename);
   // Read the next hex integer value to process. We
   // initialize the value read in to zero, so that if the
   // fscanf fails due to bad input or end of file, we'll
   // stop the loop.
   int val_to_inspect = 0;
   fscanf(in_file,"%x", &val_to_inspect);
   // Loop until the value to inxpect is zero.
   while (val_to_inspect != 0) {
       printf("Value to inspect: 0x%x ", val_to_inspect);
       // Read the endpoints and selection type. We initialize
       // to bad values in case the fscanf fails.
       int right_end = -1, left_end = -1;
       char type = ' ';
       fscanf(in_file, "%d %d %c", &right_end, &left_end, &type); //*** STUB ***
       // If the endpoints are bad, complain
       // *** STUB ***
       if(right_end>32 && right_end<0 && left_end<0 &&
           left_end>32 && left_end>right_end)
       {
           printf("Bad End points ");
           continue; // continue loop
       }
       // If type type is bad, complain
       // *** STUB ***
       if(!(type != 'u' || type != '1' || type != '2'))
       {
           printf("Bad type : %c ", type);
       }
       // Otherwise process this input
       // else {
       // Print the endpoints
       // *** STUB ***
       printf("End points Right:%d and Left:%d ", right_end, left_end);
       printf("Type: %c ", type);
       // Get the selected bits: Calculate the length
       // of the selection, calculate a mask of all
       // 1's, and use logical and to select the bits.
       // Print the selection in hex.
       int length_of_selection = right_end - left_end + 1;
       unsigned int mask = 0xffffffff;
       mask = mask >> 32-length_of_selection;
       mask = mask << left_end;
       int masked_int = val_to_inspect & mask;
       int msb = ((1<<right_end) & masked_int)?1:0;
       // Hint: The mask of n 1's equals 2^n - 1.
       // *** STUB ***
       // For type u, print the selection value as an
       // unsigned integer (using format %u)
       // Example
       if (type == 'u') {
           printf(" = %u (unsigned) ", masked_int>>left_end);
       }
       if (type == '2') {
           int magnitude = (masked_int>>left_end) - (1<<length_of_selection-1)*msb*2;
           printf(" = %d (2's complement) ", magnitude);
       }
       if (type == '1') {
           int max_value = (1<<length_of_selection)-1;
           printf("max_value: %d ", max_value);
           int magnitude = (masked_int>>left_end);
           if(msb == 1) magnitude = -1 * (max_value-magnitude);
           printf(" = %d (1's complement) ", magnitude);
       }
       // For types 1 or 2, calculate the value of the
       // selection as a 1's or 2's complement integer
       // and print the result. Note we skip leading
       // 0's, so the length of the selection is key.
       // Hint: In n bits, 011...11 = 2^n-1 is the
       // largest positive 1's or 2's complement
       // integer. If bitstring b begins with a 1,
       // then b (unsigned) - 2^n is b as an n-bit 2's
       // complement value.
       // *** STUB ***
       // }
       // Read the next value to inspect and continue the loop
       //
       val_to_inspect = 0;
       fscanf(in_file,"%x", &val_to_inspect);
       // *** STUB ***
   }
   printf(" Program quitting ");
   return 0;

}
Will work for selection 31 bit selection