I am assigning a wire
with logic between a register and a counter.
wire wire1;
reg signal;
reg [5:0] count;
assign wire1 = signal & !|count; // line 1
assign wire1 = signal & ! |count; // line 2
assign wire1 = signal & !(|count); // line 3
assign wire1 = signal & ~|count; // line 4
Icarus gives an error for 'line 1' and 'line 2', but not 'line 3' and 'line 4'. The message is,
Operand of unary ! is not a primary expression
I think it is a parser error (both work with brackets). I am using |count
as count==0
but found the reduction easier as many tools will complain about bit length of text '0' as 32 bits. I.e., I need count==COUNT_ZERO
where it is some local parameter of the correct length.
Is there actually a difference between '!' and '~' for one-bit constructs?
I am assigning a wire
with logic between a register and a counter.
wire wire1;
reg signal;
reg [5:0] count;
assign wire1 = signal & !|count; // line 1
assign wire1 = signal & ! |count; // line 2
assign wire1 = signal & !(|count); // line 3
assign wire1 = signal & ~|count; // line 4
Icarus gives an error for 'line 1' and 'line 2', but not 'line 3' and 'line 4'. The message is,
Operand of unary ! is not a primary expression
I think it is a parser error (both work with brackets). I am using |count
as count==0
but found the reduction easier as many tools will complain about bit length of text '0' as 32 bits. I.e., I need count==COUNT_ZERO
where it is some local parameter of the correct length.
Is there actually a difference between '!' and '~' for one-bit constructs?
Share Improve this question edited Jan 31 at 17:29 toolic 62.2k20 gold badges79 silver badges127 bronze badges asked Jan 31 at 17:01 artless-noise-bye-due2AIartless-noise-bye-due2AI 22.5k6 gold badges73 silver badges109 bronze badges 3 |2 Answers
Reset to default 1As you mentioned in the comments on your question, this is the difference between your 2 lines:
~|
: These 2 characters form a single operator, namely the reduction-OR!|
: These 2 characters represent two operators, namely logic-NOT and bitwise OR
Almost all simulators on EDA Playground generate a compile error on this line:
assign wire1 = signal & !|count; // line 1
This means the problem is not limited to the Icarus Verilog (iverilog
) simulator.
From a coding style perspective, I prefer creating a new wire just for this expression:
wire counter_expired = |count;
Verilog/SystemVerilog has multi character operands and has BNF rules to prevent misunderstandings like this. In this case it has nothing to do with the width of the operands. Use parentheses or a space to separate the operators.
i++
,a || b
,a && b
,p->elem
, etc. It is not 'precedence' in a traditional sense and white space may matter, so it is a lexical issue. Of course, '!' and '~' by themselves are equivalent, my misconceptions was that they were individual tokens. – artless-noise-bye-due2AI Commented Feb 4 at 13:55