Jump to content

Four bit adder: Difference between revisions

Added a solution for MATLAB
(make it a bit easier to find relevant parts of the J implementation)
(Added a solution for MATLAB)
Line 774:
 
See also: "A Formal Description of System/360” by Adin Falkoff
 
=={{header|MATLAB}}==
The four bit adder presented can work on matricies of 1's and 0's, which are stored as characters, doubles, or booleans. MATLAB has functions to convert decimal numbers to binary, but these functions convert a decimal number not to binary but a string data type of 1's and 0's. So, this four bit adder is written to be compatible with MATLAB's representation of binary. Also, because this is MATLAB, and you might want to add arrays of 4-bit binary numbers together, this implementation will add two column vectors of 4-bit binary numbers together.
 
<lang MATLAB>function [S,v] = fourBitAdder(input1,input2)
 
%Make sure that only 4-Bit numbers are being added. This assumes that
%if input1 and input2 are a vector of multiple decimal numbers, then
%the binary form of these vectors are an n by 4 matrix.
assert((size(input1,2) == 4) && (size(input2,2) == 4),'This will only work on 4-Bit Numbers');
%Converts MATLAB binary strings to matricies of 1 and 0
function mat = binStr2Mat(binStr)
mat = zeros(size(binStr));
for i = (1:numel(binStr))
mat(i) = str2double(binStr(i));
end
end
%XOR decleration
function AxorB = xor(A,B)
AxorB = or(and(A,not(B)),and(B,not(A)));
end
 
%Half-Adder decleration
function [S,C] = halfAdder(A,B)
S = xor(A,B);
C = and(A,B);
end
 
%Full-Adder decleration
function [S,Co] = fullAdder(A,B,Ci)
[SAdder1,CAdder1] = halfAdder(Ci,A);
[S,CAdder2] = halfAdder(SAdder1,B);
Co = or(CAdder1,CAdder2);
end
 
%The rest of this code is the 4-bit adder
binStrFlag = false; %A flag to determine if the original input was a binary string
%If either of the inputs was a binary string, convert it to a matrix of
%1's and 0's.
if ischar(input1)
input1 = binStr2Mat(input1);
binStrFlag = true;
end
if ischar(input2)
input2 = binStr2Mat(input2);
binStrFlag = true;
end
 
%This does the addition
S = zeros(size(input1));
 
[S(:,4),Co] = fullAdder(input1(:,4),input2(:,4),0);
[S(:,3),Co] = fullAdder(input1(:,3),input2(:,3),Co);
[S(:,2),Co] = fullAdder(input1(:,2),input2(:,2),Co);
[S(:,1),v] = fullAdder(input1(:,1),input2(:,1),Co);
%If the original inputs were binary strings, convert the output of the
%4-bit adder to a binary string with the same formatting as the
%original binary strings.
if binStrFlag
S = num2str(S);
v = num2str(v);
end
end %fourBitAdder</lang>
 
Sample Usage:
<lang MATLAB>>> [S,V] = fourBitAdder([0 0 0 1],[1 1 1 1])
 
S =
 
0 0 0 0
 
 
V =
 
1
 
>> [S,V] = fourBitAdder([0 0 0 1;0 0 1 0],[0 0 0 1;0 0 0 1])
 
S =
 
0 0 1 0
0 0 1 1
 
 
V =
 
0
0
 
>> [S,V] = fourBitAdder(dec2bin(10,4),dec2bin(1,4))
 
S =
 
1 0 1 1
 
 
V =
 
0
 
>> [S,V] = fourBitAdder(dec2bin([10 11],4),dec2bin([1 1],4))
 
S =
 
1 0 1 1
1 1 0 0
 
 
V =
 
0
0
 
>> bin2dec(S)
 
ans =
 
11
12</lang>
 
=={{header|Python}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.