Comparing integer numbers

D

Deepu

Guest
Hi All,

I had a question on how to compare 5-6 integer numbers.

always @ (a or b)
begin
if ( a == 1)
begin
if (b == 1000 || 2000 || 3000 || 4000 || 5000 || 6000)
begin
$display ("All is Fine");
end
end
end

In the above example 'a' will be '1' and then when it enters the if
loop, it doesnot enter the 2nd 'if' loop even though b is 1000 or 2000
so on, Is this way of comparing allowed? How else i can do the
comparison of integer numbers.

Thanks
 
Deepu wrote:
Hi All,

I had a question on how to compare 5-6 integer numbers.

always @ (a or b)
begin
if ( a == 1)
begin
if (b == 1000 || 2000 || 3000 || 4000 || 5000 || 6000)
begin
$display ("All is Fine");
end
end
end

In the above example 'a' will be '1' and then when it enters the if
loop, it doesnot enter the 2nd 'if' loop even though b is 1000 or 2000
so on, Is this way of comparing allowed? How else i can do the
comparison of integer numbers.

Thanks
Wrap your head around "operator precedence" and find it in a Verilog
reference (or C manual for that matter).

If b==1000, the if executes. Next, if 2000 is logically true, the if
executes. It was my recollection - flawed if you never entered the loop
- that nonzero integers evaluated to TRUE meaning the second loop should
have always been executed.

What you probably want is

if( b==1000 || b==2000 || b==3000 || b==4000 || b==5000 || b==6000 )

The Verilog language treats each logical entity as an entity onto its
own, not as part of a list to use in a previous evaluation as you
assumed. Since any operation can produce a result and those results are
used to determine your logic, you need to understand how to properly
compartmentalize your evaluations to achieve your goals.

Good luck.
 
On Sun, 16 Mar 2008 17:21:55 -0700 (PDT), Deepu <pradeep.bg@gmail.com>
wrote:

Hi All,

I had a question on how to compare 5-6 integer numbers.

always @ (a or b)
begin
if ( a == 1)
begin
if (b == 1000 || 2000 || 3000 || 4000 || 5000 || 6000)
begin
$display ("All is Fine");
end
end
end

In the above example 'a' will be '1' and then when it enters the if
loop, it doesnot enter the 2nd 'if' loop even though b is 1000 or 2000
so on, Is this way of comparing allowed? How else i can do the
comparison of integer numbers.
It's allowed but it doesn't do what you want. The expression of the if
condition is equivalent to ((b==1000)||2000|| ...) so it's always
true. What you want seems to be
if (b==1000 || b == 2000 || b==3000 ...).

HTH.
 
On Sun, 16 Mar 2008 17:21:55 -0700 (PDT),
Deepu <pradeep.bg@gmail.com> wrote:

Hi All,

I had a question on how to compare 5-6 integer numbers.

always @ (a or b)
begin
if ( a == 1)
begin
if (b == 1000 || 2000 || 3000 || 4000 || 5000 || 6000)
begin
$display ("All is Fine");
end
end
end
As others have said, this isn't what you think.

A nice way to code it is...

case (b)
1000, 2000, 3000, 4000, 5000, 6000:
$display("It's one of the six choices");
endcase

although the *meaning* of this case statement is exactly
the same as a series of if...else if.... tests.

Alternatively, if you are using SystemVerilog,
you can write this:

if (b inside {1000, 2000, 3000, 4000, 5000, 6000} )
$display("b is a member of the set of values");

--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 

Welcome to EDABoard.com

Sponsor

Back
Top