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
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
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(std/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;
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
