# how to compare two data in 2s complement form?

Status
Not open for further replies.

#### eexuke

##### Full Member level 4
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!

#### masai_mara

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)

#### echo47

Subtract the two numbers and check if the result is zero or negative.
Don't forget to make the result one bit wider than the input numbers.

#### gogogo

##### Member level 2
i think substract the two number is good!
you only check the result which can give you which one is bigger!

#### Lord Loh.

##### Full Member level 4
I don't see the point.

Is it not always true that if A(binary) > B(binary) then
A(2 comp)<B(2 comp)

01<10 but 10>01

The problem should arise in 1' ????

Or have I got something grossly wrong ?

#### xyt2001640

##### Newbie level 3
if the two numbers are positive, not any more to say! A>B is always right.

but if both nagetive and neglecting the sign bit, then
if A(binary) < B(binary) then A(2 comp)>B(2 comp)

ie,101 and 110( the first 1 is the sign bit),then 01<10. so if 2 comp, 101 is -3 and 110 is -2;else binary, 101 is -1 and 110 is -2.

so i think 1A and 1B, if A>B,then 1A<1B(binary) and 1A>1B(2 comp)

#### Lord Loh.

##### Full Member level 4
Okay, I get your point. So fastest way to impliment on a machine is to simply subtract using some high level language.

#### echo47

You don't need a high-level language. Simply implement a conventional binary subtractor. Its very similar to an adder.

Fastest? Hmmm, maybe there's a faster way to do a 2's comp magnitude comparison. That I don't know.

#### bendrift

##### Member level 4
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

#### echo47

Code:
module compare16 (a, b, equality, greater);
input [15:0] a, b;
output equality, greater;

assign equality = (a == b);
assign greater  = (a > b);
endmodule
Most synthesizers will generate a subtractor to implement the > operator.

#### visualart

change the complement code into the reverse code,
then compare them from the MSB to the LSB

#### dBUGGER

eexuke said:
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.

Best Regards,

##### Member level 5
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.

Status
Not open for further replies.