单 (&) 和双 (&&) & 和二元运算符之间有什么区别? [英] What is the difference between single (&) and double (&&) ampersand binary operators?

查看:40
本文介绍了单 (&) 和双 (&&) & 和二元运算符之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 IEEE 1800-2005 或更高版本中,&&& 二进制运算符有什么区别?它们是等价的吗?

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

我注意到这些覆盖点定义的行为相同,其中 a 和 b 是 bit 类型:

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

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

推荐答案

&& 是一个布尔运算符,我们称之为逻辑与".这并不意味着它必须对布尔操作数进行操作,而是它的返回类型是布尔值.在 SV 中,boolean 的意思是:

&& 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

当逻辑 AND 对单个位操作数进行运算时,结果是显而易见的,但在对向量进行运算时会出现问题.例如:

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) ...

为了这个逻辑运算,向量被测试是否等于 0.如果是,那么它的布尔值被定义为假",否则为真".在上面的例子中,结果是真".

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".

& 是按位 AND 和归约 AND 运算符.是按位执行还是归约执行由上下文决定:

& 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.

这篇关于单 (&) 和双 (&&) & 和二元运算符之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆