简体   繁体   中英

Check if genvar is odd in verilog

I am new to verilog hdl, I would like to know how to write the following code right:

genvar cnt;
for( cnt = 0 ; cnt < 5 ; cnt = cnt + 1) begin
   if (cnt is odd) begin
     // do something
    end else begin
    // do something else
      end
end

I'm not sure why you'd need to do this in verilog. Verilog is an hardware description language not just another programming language.

To check if cnt is odd I suggest you just check the last bit of the variable.

If the last bit is 1 the number will be odd.

if (cnt[0]) begin
    //cnt is odd
    //do something
end else begin
    //cnt is even
    //do something else
end

一种方法是使用模运算符 ( % ):

if (cnt % 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM