Given below is the completed code along with comments. Output of the program is shown at the
end. You will need to create an input file containing multiple lines of input as specified in the
question. I have given a sample input file with just 1 line of input.
Please do rate the answer if it helped. Thank you very much.
#include
/*
extractBits extracts the specified number of bits starting specified start location from left.
bit numbering starts with 0 from left. len specifies the number of bits to extract.
So extract leftmost 4 bits of a number n, the call should be extractBits(n, 0, 4). Here start = 0
means the 0th bit from left.
Similarly to extract bit 16 through 20 (i.e 5 bits), we use extractBits(n, 16, 5)
The way this function works is - First calculate the remaining number of bits on the rightside .
Since int takes 32 bits, we subtract (start+len) from 32 to get remaining bits on right.
it first clears all the bits upto starting bit by left shift <<
by start bits. Now we shift back the same number of times to right +
*/
int extractBits(unsigned n, int start, int len)
{
int rightRemaining = 32 - (start + len);
//shift left and then right by start no. of bits clears leftside bits
n = (n << start) >> start;
//now shifting by remaining number of bits on right will extract only needed bits
n = n >> rightRemaining;
return n;
}
int main (int argc, char *argv[]) {
// Get the filename to open, then open the file. If we can\'t
// open the file, complain and quit.
char *filename;
// *** Your Code *** if argv[1] exists, set filename to it,
// otherwise set filename to \"Lab3_data.txt\";
if(argv[1] != NULL)
filename = argv[1];
else
filename = \"Lab3_data.txt\";
// Open the file; if opening fails, say so and return 1.
// otherwise say what file we\'re reading from.
FILE *in_file;
in_file = fopen(filename, \"r\"); // NULL if the open failed
// *** Your Code *** if in_file is NULL, complain and quit
// otherwise say that we\'ve opened the filename
if (in_file == NULL){
printf(\"Couldn\'t open file %s\ \", filename);
return 1;
}
printf(\"Opened file %s for reading...\ \", filename);
// Repeatedly read and process each line of the file. The
// line should have a hex integer and two integer lengths.
int val, len1, len2, len3;
int nbr_vals_on_line
= fscanf(in_file, \"%x %d %d\", &val, &len1, &len2);
// Read until we hit end-of-file or a line without the 3 values.
while (nbr_vals_on_line == 3) {
// We\'re going to break up the value into bitstrings of
// length len1, len2, and len3 (going left-to-right).
// The user gives us len1 and len2, and we calculate
// len3 = the remainder of the 32 bits of value.
// All three lengths should be > 0, else we complain
// and go onto the next line.
//
len3 = 32 - (len1 + len2);
// *** Your Code***
// if any of the lengths aren\'t > 0,
// print out the value and the lengths and complain
// about the lengths not all being positive
if(len1 <= 0 || len2 <= 0 || len3 <= 0)
{
printf(\"Invalid lengths : len1=%d, len2=%d, len.
Given below is the completed code along with comments. Output of the.pdf
1. Given below is the completed code along with comments. Output of the program is shown at the
end. You will need to create an input file containing multiple lines of input as specified in the
question. I have given a sample input file with just 1 line of input.
Please do rate the answer if it helped. Thank you very much.
#include
/*
extractBits extracts the specified number of bits starting specified start location from left.
bit numbering starts with 0 from left. len specifies the number of bits to extract.
So extract leftmost 4 bits of a number n, the call should be extractBits(n, 0, 4). Here start = 0
means the 0th bit from left.
Similarly to extract bit 16 through 20 (i.e 5 bits), we use extractBits(n, 16, 5)
The way this function works is - First calculate the remaining number of bits on the rightside .
Since int takes 32 bits, we subtract (start+len) from 32 to get remaining bits on right.
it first clears all the bits upto starting bit by left shift <<
by start bits. Now we shift back the same number of times to right +
*/
int extractBits(unsigned n, int start, int len)
{
int rightRemaining = 32 - (start + len);
//shift left and then right by start no. of bits clears leftside bits
n = (n << start) >> start;
//now shifting by remaining number of bits on right will extract only needed bits
n = n >> rightRemaining;
return n;
}
int main (int argc, char *argv[]) {
// Get the filename to open, then open the file. If we can't
// open the file, complain and quit.
2. char *filename;
// *** Your Code *** if argv[1] exists, set filename to it,
// otherwise set filename to "Lab3_data.txt";
if(argv[1] != NULL)
filename = argv[1];
else
filename = "Lab3_data.txt";
// Open the file; if opening fails, say so and return 1.
// otherwise say what file we're reading from.
FILE *in_file;
in_file = fopen(filename, "r"); // NULL if the open failed
// *** Your Code *** if in_file is NULL, complain and quit
// otherwise say that we've opened the filename
if (in_file == NULL){
printf("Couldn't open file %s ", filename);
return 1;
}
printf("Opened file %s for reading... ", filename);
// Repeatedly read and process each line of the file. The
// line should have a hex integer and two integer lengths.
int val, len1, len2, len3;
int nbr_vals_on_line
= fscanf(in_file, "%x %d %d", &val, &len1, &len2);
// Read until we hit end-of-file or a line without the 3 values.
while (nbr_vals_on_line == 3) {
3. // We're going to break up the value into bitstrings of
// length len1, len2, and len3 (going left-to-right).
// The user gives us len1 and len2, and we calculate
// len3 = the remainder of the 32 bits of value.
// All three lengths should be > 0, else we complain
// and go onto the next line.
//
len3 = 32 - (len1 + len2);
// *** Your Code***
// if any of the lengths aren't > 0,
// print out the value and the lengths and complain
// about the lengths not all being positive
if(len1 <= 0 || len2 <= 0 || len3 <= 0)
{
printf("Invalid lengths : len1=%d, len2=%d, len3=%d ", len1, len2, len3);
}
else
{
// Calculate the 3 bitstrings x1, x2, x3 of lengths
// len1, len2, and len3 (reading left-to-right).
// ***Your Code ***
int x1 = extractBits(val, 0, len1);
int x2 = extractBits(val, len1, len2);
int x3 = extractBits(val, len1 + len2, len3);
// Calculate the value of x2 read as a 1's complement int.
// Calculate the value of x3 read as a 2's complement int.
// *** Your Code***
4. //~x2 gives decimal equivalent of the 1s complement, we need to use minus sign along
//~x3 + 1 gives decimal equivalent of the 2s complement, we need to use minu sign
int x2_deci = - extractBits(~(x2), 32 - len2, len2); //we are interested only in len2 rightmost bits,
which start at 32-len2
int x3_deci = - extractBits(~(x3) + 1 , 32 - len3, len3); //we need only len3 rightmost bits, which
start at 32-len3
// Print out the original value as a hex string, print out
// (going left-to-right) each length (in decimal) and selected
// bitstring (in hex), and its decimal value. We read x1 as
// unsigned, x2 in 1's complement, and x3 in 2's complement.
//
printf("Value = %#08x ", val);
printf("Its leftmost %2d bits are %#x = %d as an unsigned integer ",
len1, x1, x1 );
// *** Your Code *** print len2, x2, and decimal value of x2 in 1's comp
printf("Its next %2d bits are %#x = %d as an unsigned integer ",
len2, x2, x2_deci);
// *** Your Code *** print len3, x3, and decimal value of x3 in 2's comp
printf("Its remaining %2d bits are %#x = %d as an unsigned integer ",
len3, x3, x3_deci);
}
printf(" ");
nbr_vals_on_line = fscanf(in_file, "%x %d %d", &val, &len1, &len2);
}
return 0;
}
5. input file: Lab3_data.txt
1234abcd 16 4
output
$ ./a.out
Opened file Lab3_data.txt for reading...
Value = 0x1234abcd
Its leftmost 16 bits are 0x1234 = 4660 as an unsigned integer
Its next 4 bits are 0xa = -5 as an unsigned integer
Its remaining 12 bits are 0xbcd = -1075 as an unsigned integer
Solution
Given below is the completed code along with comments. Output of the program is shown at the
end. You will need to create an input file containing multiple lines of input as specified in the
question. I have given a sample input file with just 1 line of input.
Please do rate the answer if it helped. Thank you very much.
#include
/*
extractBits extracts the specified number of bits starting specified start location from left.
bit numbering starts with 0 from left. len specifies the number of bits to extract.
So extract leftmost 4 bits of a number n, the call should be extractBits(n, 0, 4). Here start = 0
means the 0th bit from left.
Similarly to extract bit 16 through 20 (i.e 5 bits), we use extractBits(n, 16, 5)
The way this function works is - First calculate the remaining number of bits on the rightside .
Since int takes 32 bits, we subtract (start+len) from 32 to get remaining bits on right.
it first clears all the bits upto starting bit by left shift <<
by start bits. Now we shift back the same number of times to right +
*/
int extractBits(unsigned n, int start, int len)
{
int rightRemaining = 32 - (start + len);
6. //shift left and then right by start no. of bits clears leftside bits
n = (n << start) >> start;
//now shifting by remaining number of bits on right will extract only needed bits
n = n >> rightRemaining;
return n;
}
int main (int argc, char *argv[]) {
// Get the filename to open, then open the file. If we can't
// open the file, complain and quit.
char *filename;
// *** Your Code *** if argv[1] exists, set filename to it,
// otherwise set filename to "Lab3_data.txt";
if(argv[1] != NULL)
filename = argv[1];
else
filename = "Lab3_data.txt";
// Open the file; if opening fails, say so and return 1.
// otherwise say what file we're reading from.
FILE *in_file;
in_file = fopen(filename, "r"); // NULL if the open failed
// *** Your Code *** if in_file is NULL, complain and quit
// otherwise say that we've opened the filename
if (in_file == NULL){
printf("Couldn't open file %s ", filename);
return 1;
}
printf("Opened file %s for reading... ", filename);
7. // Repeatedly read and process each line of the file. The
// line should have a hex integer and two integer lengths.
int val, len1, len2, len3;
int nbr_vals_on_line
= fscanf(in_file, "%x %d %d", &val, &len1, &len2);
// Read until we hit end-of-file or a line without the 3 values.
while (nbr_vals_on_line == 3) {
// We're going to break up the value into bitstrings of
// length len1, len2, and len3 (going left-to-right).
// The user gives us len1 and len2, and we calculate
// len3 = the remainder of the 32 bits of value.
// All three lengths should be > 0, else we complain
// and go onto the next line.
//
len3 = 32 - (len1 + len2);
// *** Your Code***
// if any of the lengths aren't > 0,
// print out the value and the lengths and complain
// about the lengths not all being positive
if(len1 <= 0 || len2 <= 0 || len3 <= 0)
{
printf("Invalid lengths : len1=%d, len2=%d, len3=%d ", len1, len2, len3);
}
else
{
// Calculate the 3 bitstrings x1, x2, x3 of lengths
// len1, len2, and len3 (reading left-to-right).
8. // ***Your Code ***
int x1 = extractBits(val, 0, len1);
int x2 = extractBits(val, len1, len2);
int x3 = extractBits(val, len1 + len2, len3);
// Calculate the value of x2 read as a 1's complement int.
// Calculate the value of x3 read as a 2's complement int.
// *** Your Code***
//~x2 gives decimal equivalent of the 1s complement, we need to use minus sign along
//~x3 + 1 gives decimal equivalent of the 2s complement, we need to use minu sign
int x2_deci = - extractBits(~(x2), 32 - len2, len2); //we are interested only in len2 rightmost bits,
which start at 32-len2
int x3_deci = - extractBits(~(x3) + 1 , 32 - len3, len3); //we need only len3 rightmost bits, which
start at 32-len3
// Print out the original value as a hex string, print out
// (going left-to-right) each length (in decimal) and selected
// bitstring (in hex), and its decimal value. We read x1 as
// unsigned, x2 in 1's complement, and x3 in 2's complement.
//
printf("Value = %#08x ", val);
printf("Its leftmost %2d bits are %#x = %d as an unsigned integer ",
len1, x1, x1 );
// *** Your Code *** print len2, x2, and decimal value of x2 in 1's comp
printf("Its next %2d bits are %#x = %d as an unsigned integer ",
len2, x2, x2_deci);
// *** Your Code *** print len3, x3, and decimal value of x3 in 2's comp
9. printf("Its remaining %2d bits are %#x = %d as an unsigned integer ",
len3, x3, x3_deci);
}
printf(" ");
nbr_vals_on_line = fscanf(in_file, "%x %d %d", &val, &len1, &len2);
}
return 0;
}
input file: Lab3_data.txt
1234abcd 16 4
output
$ ./a.out
Opened file Lab3_data.txt for reading...
Value = 0x1234abcd
Its leftmost 16 bits are 0x1234 = 4660 as an unsigned integer
Its next 4 bits are 0xa = -5 as an unsigned integer
Its remaining 12 bits are 0xbcd = -1075 as an unsigned integer