Using a oneBitAdder function and helper variables, implement a 4-bit adder pipel
ID: 3831747 • Letter: U
Question
Using a oneBitAdder function and helper variables, implement a 4-bit adder pipeline written in C.
Below is the code we were given with the oneBitAdder function and a function to find the nth bit of a number. Conceptually, I understand how a 4-bit adder works and how a pipeline works. But how am I supposed to program this in C to show how a 4-bit pipelined adder works?
#include "stdio.h"
struct fullAdderReturn {
int sum;
int carryOut;
};
// decleration of struct adder_type
typedef struct fullAdderReturn adder_type;
// decleration of nthBinaryBit
int nthBinaryBit(int number, int n);
// oneBitAdder function body
adder_type oneBitAdder(int a, int b, int carryIn) {
adder_type output;
output.sum = a ^ b ^ carryIn; //^ : bitwise xor
output.carryOut = (a & b) | (a & carryIn) | (b & carryIn); // | :bitwise OR
return output;
}
// 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;
}
Explanation / Answer
#include "stdio.h"
struct fullAdderReturn {
int sum;
int carryOut;
};
// decleration of struct adder_type
typedef struct fullAdderReturn adder_type;
// decleration of nthBinaryBit
int nthBinaryBit(int number, int n);
// oneBitAdder function body
adder_type oneBitAdder(int a, int b, int carryIn) {
adder_type output;
output.sum = a ^ b ^ carryIn; //^ : bitwise xor
output.carryOut = (a & b) | (a & carryIn) | (b & carryIn); // | :bitwise OR
return output;
}
// 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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.