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] How to find odd or even without using any compare operator ?

Status
Not open for further replies.
If you have the number in binary form, its least significant bit tells you odd (lsb = 1) or even (lsb = 0). You could reference that bit directly or use a logical operator (AND) to produce a zero / non-zero result.
 

Hi RetroTechie,

Thanks for your help. Won't we need a compare operator to check the zero / non-zero result of the AND operator (gate) ?
 

well you can use modulus operator (%2) for this . and i am not able to figure out why you want to use only compare ???
 

try resolving the no to factors if the no has odd no of odd factors then it is eventually odd

this is a least prefered soln but if you are not comparing then this may do it
 

Hi mohi, the output of the modulus operator will be either 0 or 1 right ? Without checking whether the output is 0 or 1, how is it possible to know whether number is odd or even ? Please correct me if I am missing to understand somethign here.
 

The question has been completely answered in post #2.
You can e.g. write in VHDL odd <= my_number(0);

Obviously the expression works without a compare operator. If you are looking for something special, please tell what it is.
 

I think I have over complicated this topic. Thanks to all who helped me here.
I have just googled and found out all the different ways in stackoverflow website (using VHDL). Just pasting them here for boarders reference.

Method1:
if (A mod 2) = 0 then
-- it's even
else
-- it's odd
end if;

Method2:
if (A(0)) then
-- it's odd
else
-- it's even

Method3:
function is_even(val : integer) return boolean is
begin
return val mod 2 = 0;
end;

Method4:
function is_even(val : integer) return boolean is
constant vec: signed(31 downto 0) := to_signed(val, 32);
begin
return vec(0) = '0';
end;

I agree that Method1 and Method3 (similarly Method2 and Method4) are one and the same. But they are just two different coding styles.
But the one difference I think is, Method3 and Method4 are not synthesizable. Can someone please confirm this ?
 

I think I have over complicated this topic.
You're hitting the point.

Seriously speaking, I still don't understand what's the problem with a "compare operator". In my view, methods 1, 3 and 4 are using a "=" compare operator. Why not?

All 4 methods are basically doing the same. If syntactically correct, they'll synthesize to the same gate level logic, the said XNOR gate.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top