Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions