SlideShare a Scribd company logo
1 of 9
Download to read offline
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, 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***
//~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;
}
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);
//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, 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***
//~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;
}
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

More Related Content

Similar to Given below is the completed code along with comments. Output of the.pdf

Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
Rabia Khalid
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
Yashpatel821746
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 
Maxima Finding problem In the 2-dimension space, we shall say that .pdf
Maxima Finding problem In the 2-dimension space, we shall say that .pdfMaxima Finding problem In the 2-dimension space, we shall say that .pdf
Maxima Finding problem In the 2-dimension space, we shall say that .pdf
arrowit1
 
You are required to open an input file (shown below) then read one c.pdf
You are required to open an input file (shown below) then read one c.pdfYou are required to open an input file (shown below) then read one c.pdf
You are required to open an input file (shown below) then read one c.pdf
arpaqindia
 
Python language data types
Python language data typesPython language data types
Python language data types
Harry Potter
 
Python language data types
Python language data typesPython language data types
Python language data types
Young Alista
 
Python language data types
Python language data typesPython language data types
Python language data types
Luis Goldster
 
Python language data types
Python language data typesPython language data types
Python language data types
Tony Nguyen
 
Python language data types
Python language data typesPython language data types
Python language data types
Fraboni Ec
 
Python language data types
Python language data typesPython language data types
Python language data types
James Wong
 

Similar to Given below is the completed code along with comments. Output of the.pdf (20)

Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Data Handling
Data Handling Data Handling
Data Handling
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Maxima Finding problem In the 2-dimension space, we shall say that .pdf
Maxima Finding problem In the 2-dimension space, we shall say that .pdfMaxima Finding problem In the 2-dimension space, we shall say that .pdf
Maxima Finding problem In the 2-dimension space, we shall say that .pdf
 
Block Encryption Algorithm Project.docx
Block Encryption Algorithm Project.docxBlock Encryption Algorithm Project.docx
Block Encryption Algorithm Project.docx
 
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
 
You are required to open an input file (shown below) then read one c.pdf
You are required to open an input file (shown below) then read one c.pdfYou are required to open an input file (shown below) then read one c.pdf
You are required to open an input file (shown below) then read one c.pdf
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 

More from aparnacollection

What is an OS • Interface between application programs and hardwa.pdf
What is an OS • Interface between application programs and hardwa.pdfWhat is an OS • Interface between application programs and hardwa.pdf
What is an OS • Interface between application programs and hardwa.pdf
aparnacollection
 
The multiplication sign simply means that the molecules have their i.pdf
The multiplication sign simply means that the molecules have their i.pdfThe multiplication sign simply means that the molecules have their i.pdf
The multiplication sign simply means that the molecules have their i.pdf
aparnacollection
 
Program-a. library is importedimport java.awt.; import j.pdf
Program-a. library is importedimport java.awt.; import j.pdfProgram-a. library is importedimport java.awt.; import j.pdf
Program-a. library is importedimport java.awt.; import j.pdf
aparnacollection
 
package com.test;public class Team {    private String teamId;.pdf
package com.test;public class Team {    private String teamId;.pdfpackage com.test;public class Team {    private String teamId;.pdf
package com.test;public class Team {    private String teamId;.pdf
aparnacollection
 
(a) The American Institute of Certified Accountants. (AICPA)(b) Th.pdf
(a) The American Institute of Certified Accountants. (AICPA)(b) Th.pdf(a) The American Institute of Certified Accountants. (AICPA)(b) Th.pdf
(a) The American Institute of Certified Accountants. (AICPA)(b) Th.pdf
aparnacollection
 
Yes. The compound is actively active. It is mainl.pdf
                     Yes. The compound is actively active. It is mainl.pdf                     Yes. The compound is actively active. It is mainl.pdf
Yes. The compound is actively active. It is mainl.pdf
aparnacollection
 
Adding elements public void add(String element) { For fi.pdf
 Adding elements public void add(String element) {  For fi.pdf Adding elements public void add(String element) {  For fi.pdf
Adding elements public void add(String element) { For fi.pdf
aparnacollection
 
1) Investment Grade Domestic Bonds As bond bears a fixed rate of i.pdf
    1) Investment Grade Domestic Bonds As bond bears a fixed rate of i.pdf    1) Investment Grade Domestic Bonds As bond bears a fixed rate of i.pdf
1) Investment Grade Domestic Bonds As bond bears a fixed rate of i.pdf
aparnacollection
 

More from aparnacollection (20)

}Solution}.pdf
}Solution}.pdf}Solution}.pdf
}Solution}.pdf
 
What is an OS • Interface between application programs and hardwa.pdf
What is an OS • Interface between application programs and hardwa.pdfWhat is an OS • Interface between application programs and hardwa.pdf
What is an OS • Interface between application programs and hardwa.pdf
 
Using a broad definition of file(note that it doesnt count h.pdf
Using a broad definition of file(note that it doesnt count h.pdfUsing a broad definition of file(note that it doesnt count h.pdf
Using a broad definition of file(note that it doesnt count h.pdf
 
unable to open it ....it asks for username and passwordSolution.pdf
unable to open it ....it asks for username and passwordSolution.pdfunable to open it ....it asks for username and passwordSolution.pdf
unable to open it ....it asks for username and passwordSolution.pdf
 
This is called a dehydrationreaction. a) H2SO4 H20Solution.pdf
This is called a dehydrationreaction. a) H2SO4  H20Solution.pdfThis is called a dehydrationreaction. a) H2SO4  H20Solution.pdf
This is called a dehydrationreaction. a) H2SO4 H20Solution.pdf
 
The multiplication sign simply means that the molecules have their i.pdf
The multiplication sign simply means that the molecules have their i.pdfThe multiplication sign simply means that the molecules have their i.pdf
The multiplication sign simply means that the molecules have their i.pdf
 
SolutionThe variance of individual assets is measure of total ris.pdf
SolutionThe variance of individual assets is measure of total ris.pdfSolutionThe variance of individual assets is measure of total ris.pdf
SolutionThe variance of individual assets is measure of total ris.pdf
 
soldefnition of critical thinkingit is an intellectual process o.pdf
soldefnition of critical thinkingit is an intellectual process o.pdfsoldefnition of critical thinkingit is an intellectual process o.pdf
soldefnition of critical thinkingit is an intellectual process o.pdf
 
Program-a. library is importedimport java.awt.; import j.pdf
Program-a. library is importedimport java.awt.; import j.pdfProgram-a. library is importedimport java.awt.; import j.pdf
Program-a. library is importedimport java.awt.; import j.pdf
 
package com.test;public class Team {    private String teamId;.pdf
package com.test;public class Team {    private String teamId;.pdfpackage com.test;public class Team {    private String teamId;.pdf
package com.test;public class Team {    private String teamId;.pdf
 
(a) The American Institute of Certified Accountants. (AICPA)(b) Th.pdf
(a) The American Institute of Certified Accountants. (AICPA)(b) Th.pdf(a) The American Institute of Certified Accountants. (AICPA)(b) Th.pdf
(a) The American Institute of Certified Accountants. (AICPA)(b) Th.pdf
 
Yes. The compound is actively active. It is mainl.pdf
                     Yes. The compound is actively active. It is mainl.pdf                     Yes. The compound is actively active. It is mainl.pdf
Yes. The compound is actively active. It is mainl.pdf
 
The probability of A and its complement will sum to oneSolution.pdf
 The probability of A and its complement will sum to oneSolution.pdf The probability of A and its complement will sum to oneSolution.pdf
The probability of A and its complement will sum to oneSolution.pdf
 
H+,CN-Solution H+,CN-.pdf
 H+,CN-Solution H+,CN-.pdf H+,CN-Solution H+,CN-.pdf
H+,CN-Solution H+,CN-.pdf
 
Adding elements public void add(String element) { For fi.pdf
 Adding elements public void add(String element) {  For fi.pdf Adding elements public void add(String element) {  For fi.pdf
Adding elements public void add(String element) { For fi.pdf
 
1) Investment Grade Domestic Bonds As bond bears a fixed rate of i.pdf
    1) Investment Grade Domestic Bonds As bond bears a fixed rate of i.pdf    1) Investment Grade Domestic Bonds As bond bears a fixed rate of i.pdf
1) Investment Grade Domestic Bonds As bond bears a fixed rate of i.pdf
 
When atoms share one or more pairs of electrons t.pdf
                     When atoms share one or more pairs of electrons t.pdf                     When atoms share one or more pairs of electrons t.pdf
When atoms share one or more pairs of electrons t.pdf
 
This is quite simple to do. Barium sulfate is ins.pdf
                     This is quite simple to do. Barium sulfate is ins.pdf                     This is quite simple to do. Barium sulfate is ins.pdf
This is quite simple to do. Barium sulfate is ins.pdf
 
Step1 Cocentration of NaCl =.250 moles Step2 Mol.pdf
                     Step1 Cocentration of NaCl =.250 moles  Step2 Mol.pdf                     Step1 Cocentration of NaCl =.250 moles  Step2 Mol.pdf
Step1 Cocentration of NaCl =.250 moles Step2 Mol.pdf
 
Nitric acid is a strong acid... we can assume com.pdf
                     Nitric acid is a strong acid... we can assume com.pdf                     Nitric acid is a strong acid... we can assume com.pdf
Nitric acid is a strong acid... we can assume com.pdf
 

Recently uploaded

Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
cupulin
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 

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