Question: Please help me with this code question. The code is below: #include typedef char bit; // Adders return a sum and carry-out. Sum may be
Please help me with this code question.
The code is below:
#include
typedef char bit;
// Adders return a sum and carry-out. Sum may be multiple bits in size, but carry-out is // always one bit in size. typedef struct { int sum; bit carryOut; } adderReturn;
// Implements the half adder logic from class. // PRE: the bits to be added are in the least significant bit of x and y // POST: the sum and carry-out of adding the two bits is returned adderReturn halfAdder(bit x, bit y) { adderReturn output; output.sum = x^y; output.carryOut = x&y;
return output; }
// Implements the full adder logic from class, using two half-adders. // PRE: the bits to be added are in the least significant bit of x, y, and carryIn // POST: the sum and carry-out of adding the three bits is returned adderReturn fullAdder(bit x, bit y, bit carryIn) { adderReturn output; output.sum = x^y^carryIn; output.carryOut = (x&y) | (carryIn&(x^y));
return output; }
// Implements a four-bit full adder using multiple calls to the fullAdder function. // PRE: the least significant four-bits of a and b contain the numbers to be added, // and the least significant bit of carryIn contains the carry-in to be used // POST: the four-bit sum and single-bit carry-out is returned. Note that the carry-out // signals whether overflow occurred. adderReturn fourBitAdder(int a, int b, bit carryIn) { adderReturn output; return output; }
(a) In class we saw that a full adder can be built out of two half adders plus an or gate. Rewrite the fullAdder function so that it uses the halfAdder function plus Boolean logic to compute the sum and carry-out. (b) Implement the fourbitAdder function to add two four-bit values together using the fullAdder function. Test your code thoroughly. (a) In class we saw that a full adder can be built out of two half adders plus an or gate. Rewrite the fullAdder function so that it uses the halfAdder function plus Boolean logic to compute the sum and carry-out. (b) Implement the fourbitAdder function to add two four-bit values together using the fullAdder function. Test your code thoroughly
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
