- The Most Significant Bit (MSB) of "Q" is should be assigned as the Least Significant Bit (LSB) of "R" while left shifting "RQ".
- After left shifting of "RQ", there should be a statement to update "R" based on its signed bit, either by adding or subtracting "D".
- After updating "R", the LSB of Q should be updated depending on the signed bit of R.
- Since this algorithm involves many bitwise operations like left shift, right shift, and bitwise assignment, it is recommended to use arrays to represent binary numbers instead of decimal numbers.
Incorrect non restoring division for unsigned integers !!
3 views (last 30 days)
Show older comments
Hi there,
For unsigned integer values, I am attempting to construct a non-restoring division algorithm, but it appears that I am having difficulties getting the quotient and remainder right.
Can you give me some advice on how to improve the implementation?
Many thanks
clearvars;clc;
n = 32;
N = 11;
D = 3;
R = zeros(1,1);
Q = 0;
D = bitshift(D, 1);
for i = 1 : (n-1) %.. 0 do -- for example 31..0 for 32 bits
if R >= 0
Q = accumpos(Q,1);
R = 2 * R - D;
else
Q = accumneg(Q,1);
R = 2 * R + D;
end
end
Q = Q - not(Q); % -- Appropriate if −1 digits in Q are represented as zeros as is common.
if R < 0
Q = accumneg(Q,1);
R = R + D; %-- Needed only if the remainder is of interest.
end
Q ,R
0 Comments
Answers (1)
Yash
on 7 Sep 2023
Edited: Yash
on 12 Sep 2023
Hi,
I can understand that you are interested in implementing the non-restoring division for unsigned integers.
I have reviewed your code and noticed a few areas that require attention:
Kindly refer to the below example code snippet that demonstrates how to initialize the arrays using binary representations:
dividendBin = dec2bin(dividend);
divisorBin = dec2bin(divisor);
% Initialize the registers
A = zeros(1, numBits+1);
Q = zeros(1, numBits);
% Set the dividend and divisor in the registers
Q(end-length(dividendBin)+1:end) = dividendBin - '0';
M = [zeros(1, numBits - length(divisorBin)+1) divisorBin-'0'];
Refer the below code to implement the algorithm:
for i = 1:numBits
% Check signed bit of A
A0 = A(1);
%left Shift AQ
A = [A(2:end) Q(1)];
Q = [Q(2:end) 0];
if A0==0
A = addBin(A,negM);
else
A = addBin(A,M);
end
% Decide LSB of Q
if(A(1)==1)
Q(end)=0;
else
Q(end)=1;
end
end
quotient = bin2dec(num2str(Q));
remainder = bin2dec(num2str(A));
In the above example, "addBin" is a custom MATLAB function which adds two arrays containing binary representations, "negM" is the 2's complement of M.
I hope this helps you address the issue.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!