Dear all,
In order to decide which one is bigger of two data in 2s complement form ,the only idea I have is to translate them into decimal..... Do you have any other good ways in doing it?
Thanks!
well, you can just compare it like any two binary numbers and invert the result after comparison.
ie, if a=1001, b=1111;
then for binary you have b>a but for 2's complement invert that and say a>b.
(this works if numbers are unsigned only)
i give a example here, maybe u find a better way to implement it!
always @(posedge clk or negedge rst_)
begin
if(!rst_)
begin
do_o<=#unit_delay 0; //the large value register
id_out_o<=#unit_delay 1'b0; //the id register 0 for di1_i or 1 for di2_i
end
else
if(di1_i[word_g]==di2_i[word_g])
begin
if(di1_i[word_g-1:0]<di2_i[word_g-1:0])
begin
do_o<= #unit_delay di2_i;
id_out_o<= #unit_delay 1'b1;
end
else
begin
do_o<= #unit_delay di1_i;
id_out_o<= #unit_delay 1'b0;
end
end
else
begin
if(di2_i[word_g]==1'b0)
begin
do_o<=#unit_delay di2_i;
id_out_o<=#unit_delay 1'b1;
end
else
begin
do_o<=#unit_delay di1_i;
id_out_o<=#unit_delay 1'b0;
end
end
end
Dear all,
In order to decide which one is bigger of two data in 2s complement form ,the only idea I have is to translate them into decimal..... Do you have any other good ways in doing it?
Thanks!
Hi eexuke,
Since you told 2's complement, our no's are signed and can be +se , -ve or 0
First check If A[MSB] != B[MSB] then {If anyone is +ve that no. is greater}
If A[MSB] = 0 then A>B
else B>A end if;
else
IF A[MSB] = 0 then {means B[MSB] also is 0 and both nos. are +ve}
IF A[MSB-1:0] > B[MSB-1:0] then
A>B;
Else B> A; End if;
Else {means both nos are negative }
IF A[MSB-1:0] > B[MSB-1:0] then
B > A;
Else A > B; End if;
End if;
Hope i am correct. Just try it.
In order to check two numbers you have to subtract them. If the MSB of the result is 0 and the number of the result (the remaining digits without the msb) is 0 then the two numbers are equal.
If the msb of the result is 0 and the number is /=0 then the result is positive which means that if result = a - b then a is the bigger number than b.
If the msb of the result is 1 then the result is negative which means that b is bigger.