Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to compare two data in 2s complement form?

Status
Not open for further replies.

eexuke

Full Member level 4
Joined
Mar 25, 2004
Messages
196
Helped
10
Reputation
20
Reaction score
3
Trophy points
1,298
Activity points
1,934
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

Advanced Member level 4
Joined
Aug 13, 2004
Messages
118
Helped
8
Reputation
14
Reaction score
2
Trophy points
1,298
Activity points
1,426
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

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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
Joined
Jan 22, 2005
Messages
43
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
322
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
Joined
Jun 19, 2004
Messages
196
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,296
Location
Texas
Activity points
1,950
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
Joined
Jun 21, 2005
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,308
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
Joined
Jun 19, 2004
Messages
196
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,296
Location
Texas
Activity points
1,950
Okay, I get your point. So fastest way to impliment on a machine is to simply subtract using some high level language.
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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
Joined
Nov 18, 2004
Messages
74
Helped
5
Reputation
10
Reaction score
4
Trophy points
1,288
Activity points
688
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

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
I fell asleep about halfway through your code! ;)

How about this?

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

Advanced Member level 1
Joined
Dec 21, 2001
Messages
467
Helped
28
Reputation
56
Reaction score
4
Trophy points
1,298
Activity points
3,333
change the complement code into the reverse code,
then compare them from the MSB to the LSB
 

dBUGGER

Advanced Member level 4
Joined
Apr 18, 2005
Messages
108
Helped
13
Reputation
26
Reaction score
1
Trophy points
1,298
Location
Milky Way
Activity points
2,430
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,
 

adap

Member level 5
Joined
Feb 16, 2005
Messages
93
Helped
12
Reputation
24
Reaction score
1
Trophy points
1,288
Activity points
2,261
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top