Lab 10.c #include \"stdio.h\" /* structure is a user defined data type which all
ID: 3825471 • Letter: L
Question
Lab 10.c#include "stdio.h"
/* structure is a user defined data type which allows you to combine data items of different kinds. Structures are used to represent a record. The following structure combines sum and carryOut variables.
*/ struct fullAdderReturn { int sum; int carryOut; };
// typedef will give our structure (struct fullAdderReturn) a new name: adder_type // now we can use adder_type the same way as other data type (int, float, etc. ) typedef struct fullAdderReturn adder_type;
/* *** function declaration ***
A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code.
Functions serve two purposes: 1 - Functions allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else'.
2 - Functions make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.
A function can take a number of parameters, do required processing and then return a value. There may be a function which does not return any value.
A function declaration is usually declared at the top of a C source file. A function declaration is sometime called function prototype or function signature.
For instance Demo() function which returns an integer, and takes two parameters (par1 and par2) is decleared as follows: int Demo( int par1, int par2);
// decleration of nthBinaryBit
function name: nthBinaryBit fucntion return type: int (integer) function takes two parameters: int number, int n The following function takes an integer number, and returns its nth bit in binary example: nthBinaryBit (10, 1) = 0 10 in binary: 1010 nthBinaryBit (10, 2) = 1 10 in binary: 1010 nthBinaryBit (10, 3) = 0 10 in binary: 1010 nthBinaryBit (10, 4) = 1 10 in binary: 1010 */ int nthBinaryBit(int number, int n);
// decleration of oneBitAdder // fucntion return type: adder_type // function takes three parameters: int a, int b, int carryIn // The following function takes an integer number, and returns its nth bit in binary adder_type oneBitAdder(int a, int b, int carryIn);
// nthBinaryBit function body int nthBinaryBit(int number, int n) { int bit; //variable to hold bit
for (int i = 0; i < n; i++) { if (number & 1) //bit detection (&: bitwise AND) bit = 1; else bit = 0; number >>= 1; //shift right number by 1 } return bit; }
// oneBitAdder function body adder_type oneBitAdder(int a, int b, int carryIn) { adder_type output;
//*** ADD COMMENTS output.sum = a ^ b ^ carryIn; //^ : bitwise xor
//*** ADD COMMENTS output.carryOut = (a & b) | (a & carryIn) | (a & carryIn); // | :bitwise OR return output; }
int main() { int a = 1; int b = 0; int n = 1; int c;
c = nthBinaryBit(a, n); printf("The %d(st/nd/th) bit of %d: %d", n, a, c);
int Cin = 0;
//*** ADD COMMENTS adder_type adder01 = oneBitAdder(a, b, Cin);
printf(" input a = %d", a); printf(" input b = %d", b); printf(" input Cin = %d", Cin);
//access sum and carryOut printf(" a+b sum = %d", adder01.sum); printf(" a+b carryOut = %d", adder01.carryOut);
return 0; }
Lab 10-C Programing Pipeline and Cache Category: Individual pre: 1. Brows to https languages c re 2. Continue as Anonymous (you can also make a new account or login if you have already created an account last week) 3. Download Lab10.c from canvas and copy the code to your online IDE. Task A: Due at the End of the lab (L01: 10:45 am, L02: 1:45 pm, LO3: 4:45pm) 1. Download C Programming Reference.pdf 2. Review the Lab10.c code and add proper comments where asked: ADD COMMENTS 3. Declare a new function fourBitAdder as: adder type fourBitAdder(int a, int b, int carryln); 4. Implement the fourBitAdder function with using four oneBitAdder functions. You may use helper variables to pass the values between functions. 5. Use fourBitAdder function in a for loop to increment a number 15 times. That is, starting with a sum of 0, add 1 to it 15 times. 6. Assume each loop iteration takes 1000 ns (intopTime 1000). Your program should report the total execution time of the for loop. add opTime 1000; at end of for loop). Task B: Due: May 2nd, 2017 Using oneBitAdder function and helper variables, implement a 4-bit adder pipeline.
Explanation / Answer
#include <stdio.h>
/*
structure is a user defined data type which allows you to combine data items of different kinds.
Structures are used to represent a record. The following structure combines sum and
carryOut variables.
*/
struct fullAdderReturn {
int sum;
int carryOut;
};
// typedef will give our structure (struct fullAdderReturn) a new name: adder_type
// now we can use adder_type the same way as other data type (int, float, etc. )
typedef struct fullAdderReturn adder_type;
/*
*** function declaration ***
A function is a module or block of program code which deals with a particular task.
Making functions is a way of isolating one block of code from other independent blocks of code.
Functions serve two purposes:
1 - Functions allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else'.
2 - Functions make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.
A function can take a number of parameters, do required processing and then return a value. There may be a function which does not return any value.
A function declaration is usually declared at the top of a C source file. A function declaration is sometime called function prototype or function signature.
For instance Demo() function which returns an integer, and takes two parameters (par1 and par2) is decleared as follows:
int Demo( int par1, int par2);
// decleration of nthBinaryBit
function name: nthBinaryBit
fucntion return type: int (integer)
function takes two parameters: int number, int n
The following function takes an integer number, and returns its nth bit in binary
example:
nthBinaryBit (10, 1) = 0 10 in binary: 1010
nthBinaryBit (10, 2) = 1 10 in binary: 1010
nthBinaryBit (10, 3) = 0 10 in binary: 1010
nthBinaryBit (10, 4) = 1 10 in binary: 1010
*/
int nthBinaryBit(int number, int n);
// decleration of oneBitAdder
// fucntion return type: adder_type
// function takes three parameters: int a, int b, int carryIn
// The following function takes an integer number, and returns its nth bit in binary
adder_type oneBitAdder(int a, int b, int carryIn);
//TASK Declare method header
adder_type fourBitAdder(int a, int b, int carryIn);
// nthBinaryBit function body
int nthBinaryBit(int number, int n) {
int bit; //variable to hold bit
int i;
for (i = 0; i < n; i++) {
if (number & 1) //bit detection (&: bitwise AND)
bit = 1;
else
bit = 0;
number >>= 1; //shift right number by 1
}
return bit;
}
// oneBitAdder function body
adder_type oneBitAdder(int a, int b, int carryIn) {
adder_type output;
//*** ADD COMMENTS
output.sum = a ^ b ^ carryIn; //^ : bitwise xor
//*** ADD COMMENTS
output.carryOut = (a & b) | (a & carryIn) | (a & carryIn); // | :bitwise OR
return output;
}
adder_type fourBitAdder(int a, int b, int carryIn) {
adder_type r0, r1, r2, r3;
adder_type output;
r0 = oneBitAdder(nthBinaryBit(a,1),nthBinaryBit(b,1),0); //get LSB addition result
r1 = oneBitAdder(nthBinaryBit(a,2),nthBinaryBit(b,2),r0.carryOut);
r2 = oneBitAdder(nthBinaryBit(a,3),nthBinaryBit(b,3),r1.carryOut);
r3 = oneBitAdder(nthBinaryBit(a,4),nthBinaryBit(b,4),r2.carryOut); //get MSB results last, suchthat carry is MSB - 1 result's carry out
/*We then convert binary to decimal*/
output.carryOut = r3.carryOut;
output.sum = nthBinaryBit(r3.sum,1);
output.sum <<= 1;
output.sum += nthBinaryBit(r2.sum,1);
output.sum <<= 1;
output.sum += nthBinaryBit(r1.sum,1);
output.sum <<= 1;
output.sum += nthBinaryBit(r0.sum,1);
return output;
}
/*
int main() {
int a = 1;
int b = 0;
int n = 1;
int c;
c = nthBinaryBit(a, n);
printf("The %d(st/nd/th) bit of %d: %d", n, a, c);
int Cin = 0;
//*** ADD COMMENTS
adder_type adder01 = oneBitAdder(a, b, Cin);
printf(" input a = %d", a);
printf(" input b = %d", b);
printf(" input Cin = %d", Cin);
//access sum and carryOut
printf(" a+b sum = %d", adder01.sum);
printf(" a+b carryOut = %d", adder01.carryOut);
return 0;
}
*/
int main() {
adder_type result;
int i, k= 1, a = 0, opTime = 0;
result.sum = 0;
result.carryOut = 0;
for (i = 0; i<15; i++) {
result = fourBitAdder(result.sum, k, result.carryOut);
opTime += 1000;
}
printf ("Sum = %d ",result.sum);
printf ("Carry =%d ",result.carryOut);
printf (" Operational time: %d ",opTime);
}
I tried my best ot jeep the code as simple as possible. I have also commented few lines of the code i edited. If you still find any difficulty with the code, please feel freee to comment below. I shall be glad to help yiu. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.