Question: C PROGRAMMING // CMPT 295 Assignment 2 Four-bit Full Adder // DO NOT include a main function in your final submission. #include typedef char bit;

C PROGRAMMING

C PROGRAMMING // CMPT 295 Assignment 2 Four-bit Full Adder // DO

// CMPT 295 Assignment 2 Four-bit Full Adder // DO NOT include a main function in your final submission.

#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; }

NOT include a main function in your final submission. #include typedef char

4. Download the assignment2.c file from the course web-page. It contains C functions for a half adder and full adder; read through it carefully and make sure you understand how they work. (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. Hints: Start by writing code that uses the halfAdder and fullAdder functions and see if they produce the correct output for different input values Think of your input and output in terms of binary; work with and print out hex values as you did for assignment 1 // DO NOT include a main function in your final submission. #include typedef char bit; 8 // Adders return a sum and carry-out. Sum may be multiple bits in size, but carry-out is I always one bit in size. typedef struct = { int sum; 32 bit carryOut; } adderReturn; 18 // 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; 28 // 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)); 32 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, VI 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; 49 return output; 50 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!