Write a MATLAB function the performs the operation of a half-adder, call it half
ID: 1810980 • Letter: W
Question
Write a MATLAB function the performs the operation of a half-adder, call it half_add. The function should have two inputs, a and b, and return two outputs, X and c. Then, write another function, call it full_add, that invokes half_add to perform the operation of a full adder. The function full_add should have three inputs, a, b and c_in, and return two outputs, s and c_out. Finally, write a program that uses the function full_add to find and display s and c_out for each possible combination of a, b, and c_in.
Explanation / Answer
clear all; clc
% Code segment for defining HALF ADDER
function [(X,c)] = half_add(a,b)
X= xor(a,b);
c = and(a,b);
end
% Code for performing FULL ADDER function invoking Half-adders
function [(s, c_out)]= full_add(a,b,c_in)
% define intermediate variables after performing one half -adder function
[X,c] = half_add(a,b)
% Output of Second half-adder function
[S,c_2] = half_add(X,c_in)
%Output Carry
c_out= or(c,c_2)
end
%program that uses the function full_add to find and display s and c_out for each possible combination of a, b, and c_in
a=[0 0 0 0 1 1 1 1];
b=[0 0 1 1 0 0 1 1];
c_in= [0 1 0 1 0 1 0 1];
for k = 1:8
[s(k),c_out(k)] = full_add(a,b,c_in)
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.