What is the difference between single (&) and double (&&) ampersand binary operators?

Victor Lyuboslavsky picture Victor Lyuboslavsky · Jun 26, 2013 · Viewed 57.3k times · Source

In IEEE 1800-2005 or later, what is the difference between & and && binary operators? Are they equivalent?

I noticed that these coverpoint definitions behave identically where a and b are of type bit:

  • cp: coverpoint a & b;
  • cp: coverpoint a && b;

Answer

Vasiliy picture Vasiliy · Jun 26, 2013

&& is a boolean operator which we call "logical AND". This doesn't mean that it must operate on boolean operands, but that its return type is boolean. In SV, boolean means:

1'b1 \\ true
1'b0 \\ false
1'bx \\ undef

When logical AND operates on single bit operands the result is obvious, but the issue arises when it operates on a vector. For example:

logic [1:0] vector;
...
vector = 2'b10;
if (1'b1 && vector) ...

For purpose of this logical operation, vector is tested for equality to 0. If it is, then its boolean value is defined as "false", otherwise "true". In the above example, the result is "true".

& is a bitwise AND and reduction AND operators. Whether it is executed as bitwise or reduction is determined by the context:

logic [1:0] vector1;
logic [1:0] vector2;
logic [1:0] vector3;
...
vector1 = 2'b10;
vector2 = 2'b01;
...
vector3 = vector2 & vector1; // bitwise; vector3 = 2'b00
if ( (&vector1) || (&vector2) ) ... // reduction; the result of each reduction is 1'b0

Bitwise operator performs logical AND operation on each pair of corresponding bits of operands. The result is a vector which width equals to maximal width of operands.

Reduction operator performs logical AND operation between all the bits of a single vector. The result is a single bit boolean value.

NOTE: when executed on a single bit operands, the results of bitwise and logical operators are the same. However, when even one of the operands is a vector, the results may differ.