Incorrect non restoring division for unsigned integers !!

5 visualizaciones (últimos 30 días)
Life is Wonderful
Life is Wonderful el 6 de Sept. de 2022
Editada: Yash el 12 de Sept. de 2023
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
Q = -30
R = 0

Respuestas (1)

Yash
Yash el 7 de Sept. de 2023
Editada: Yash el 12 de Sept. de 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:
  1. The Most Significant Bit (MSB) of "Q" is should be assigned as the Least Significant Bit (LSB) of "R" while left shifting "RQ".
  2. After left shifting of "RQ", there should be a statement to update "R" based on its signed bit, either by adding or subtracting "D".
  3. After updating "R", the LSB of Q should be updated depending on the signed bit of R.
  4. 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.
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.
To know more about the algorithm, I suggest you refer the following article: link.
I hope this helps you address the issue.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by