Verilog学习之Bitwise operators:Vectorgates

36 篇文章 14 订阅

Build a circuit that has two 3-bit inputs that computes the bitwise-OR of the two vectors, the logical-OR of the two vectors, and the inverse (NOT) of both vectors. Place the inverse of b in the upper half of out_not (i.e., bits [5:3]), and the inverse of a in the lower half.

构建一个具有两个 3 位输入的电路,用于计算两个向量的按位或、两个向量的逻辑或以及两个向量的逆 (NOT)。将 b 的倒数放在 out_not 的上半部分(即位 [5:3]),将 a 的倒数放在下半部分。

Bitwise vs. Logical Operators

Earlier, we mentioned that there are bitwise and logical versions of the various boolean operators (e.g., norgate). When using vectors, the distinction between the two operator types becomes important. A bitwise operation between two N-bit vectors replicates the operation for each bit of the vector and produces a N-bit output, while a logical operation treats the entire vector as a boolean value (true = non-zero, false = zero) and produces a 1-bit output.

早些时候,我们提到了各种布尔运算符(例如,norgate)的按位和逻辑版本。使用向量时,两种运算符类型之间的区别变得很重要。两个 N 位向量之间的按位运算复制向量的每个位的运算并产生 N 位输出,而逻辑运算将整个向量视为布尔值(真 = 非零,假 = 零)和产生 1 位输出。

Look at the simulation waveforms at how the bitwise-OR and logical-OR differ.

Module Declaration

module top_module( 
    input [2:0] a,
    input [2:0] b,
    output [2:0] out_or_bitwise,
    output out_or_logical,
    output [5:0] out_not
);

Even though you cannot assign to a wire more than once, you can use a part select on the left-hand-side of an assign. You don't need to assign to the entire vector all in one statement.

即使您不能多次分配给电线,您也可以使用分配左侧的零件选择。您无需在一个语句中全部分配给整个向量。

本题将关注逐位逻辑运算符(&)和逻辑运算符(&&)之间的差别

 module top_module( 
    input [2:0] a,
    input [2:0] b,
    output [2:0] out_or_bitwise,
    output out_or_logical,
    output [5:0] out_not
);
    assign out_or_bitwise = a | b;
    assign out_or_logical = a || b;

    assign out_not[2:0] = ~a;	// Part-select on left side is o.
    assign out_not[5:3] = ~b;	//Assigning to [5:3] does not conflict with [2:0]

endmodule

module top_module(
    input [2:0] a, 
    input [2:0] b, 
    output [2:0] out_or_bitwise,
    output out_or_logical,
    output [5:0] out_not
);
    
    assign out_or_bitwise = a | b;
    assign out_or_logical = a || b;

    assign out_not[2:0] = ~a;    // Part-select on left side is o.
    assign out_not[5:3] = ~b;    //Assigning to [5:3] does not conflict with [2:0]
    
endmodule


 

 

 

 

 

 

 

◆逻辑操作符

逻辑操作符主要有 3 个:

&&(逻辑与), ||(逻辑或),!(逻辑非)。

逻辑操作符的计算结果是一个 1bit 的值,0 表示假,1 表示真,x 表示不确定。

如果一个操作数不为 0,它等价于逻辑 1;如果一个操作数等于 0,它等价于逻辑 0。如果它任意一位为 x 或 z,它等价于 x。

如果任意一个操作数包含 x,逻辑操作符运算结果不一定为 x。

逻辑操作符的操作数可以为变量,也可以为表达式。例如:

A = 3;

B = 0;

C = 2'b1x ;

A && B    //     为假

A || B    //     为真!

A       //     为假

! B       //     为真

A && C    //     为X,不确定

A || C    //     为真,因为A为真

(A==2) && (! B)  //为假,此时第一个操作数为表达式

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shining0596

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值