- 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 !!
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Yash
2023-9-7
编辑:Yash
2023-9-12
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!