-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryAdd.m
More file actions
26 lines (24 loc) · 770 Bytes
/
Copy pathbinaryAdd.m
File metadata and controls
26 lines (24 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function result = binaryAdd( A, B, n )
%BINARYADD adds two binary numbers
% result = binaryAdd(A, B , n) simulates a n-bit binary adder which adds
% binary numbers stored in arrays A & B. Carry bit is dismissed.
result = zeros(1,n);
carries = zeros(1,n+1); %An array for storing the carry at each position
%A loop is created over the binary array. At each position, there are four
%possibilites: 0+0=1, 0+1=1, 1+1=0(with carry), 1+1+carry=1 (with carry)
for i = n : -1 : 1
partialSum = A(i)+B(i)+carries(i+1);
switch partialSum
case 0
result(i)= 0;
case 1
result(i)=1;
case 2
result(i)=0;
carries(i)=1;
case 3
result(i)=1;
carries(i)=1;
end
end
end