Continue to Site

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.

[SOLVED] comparing unsigned numbers

Status
Not open for further replies.

nsgil85

Member level 4
Joined
Dec 11, 2012
Messages
73
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,833
Hi everyone

What is the syntex for comparing 2 unsigned number with diffrent length
for example:

signal sig_A :unsigned(3 downto 0) := to_unsigned(1, sig_A 'range) ;
signal sig_B :unsigned(2 downto 0) := to_unsigned(3, sig_B 'range) ;

if sigB > sig_A then
...


will i need to convert both numbers first to integer and then compare?


Thanks
Gil
 

you can compare unsigned values of different length, there are no special requirements. The greater/less operators will treat the comparison as a numeric comparison between unsigned values.

Some developers will also compare std_logic_vectors of equal length using greater/less. The comparison is done lexically, which matches a comparison between unsigned values. But only when the values don't have 'Z' or 'X' or etc... and are of equal length.


Basically, there is no need to change sizes if the types are "unsigned". You may see people resize the values because they had issues (incorrectly) comparing std_logic_vectors in the past.
 

Some developers will also compare std_logic_vectors of equal length using greater/less. The comparison is done lexically, which matches a comparison between unsigned values. But only when the values don't have 'Z' or 'X' or etc... and are of equal length.

Unless you're using the non-standard std_logic_unsigned library (or the standard 2008 library numeric_std_unsigned) then the results from this can be counter intuitive and give odd simulation results. I highly recommend you use the numeric_std library type unsigned.
 

thank you for the advice

Ok, so what can be done to compare variable with signal unsigned type?

BTY
I use numeric_std lib

Gil
 

will i need to convert both numbers first to integer and then compare?

This is strongly compiler dependent; if in doubt use assembler instructions (perhaps inline)
 

This is strongly compiler dependent; if in doubt use assembler instructions (perhaps inline)

The OP is talking about HDL - not CPU programming!
HDL has no assembler instructions.

Ok, so what can be done to compare variable with signal unsigned type?

You can just compare two unsigned directly. No need to modify anything about them.
Do you have a specific use case you want help with?
 

No assembler for today :)

TrickyDicky, yes as following:


signal sig_C :unsigned(2 downto 0) := to_unsigned(3, sig_C 'range) ;

process:
variable var_D :unsigned(3 downto 0) := to_unsigned(1, sig_D 'range) ;
begin
if var_D > sig_C then
...

thanks
Gil
 

What exactly do you want help with - it should work as written - always false because 1 is not greater than 3
 
found my mistake, it didn't compile due to range issue,

Thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top