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
// 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; }
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started