Verilog中&(按位与)与&&(逻辑操作符)的区别

按位操作符用于直接操作整数的二进制位,而逻辑操作符则用于连接或比较布尔值,并控制程序的流程。本文详细介绍Verilog中按位与操作与逻辑操作符之间的区别,并通过相关代码示例和仿真波形演示其具体操作结果。

1 逻辑操作符

逻辑操作符有:·

● &&(逻辑与)

● ||(逻辑或)

● !(逻辑非)

这些操作符在逻辑值0或1上操作。逻辑操作的结构为0或1。例如,假定:

crd ='b0; //0为假

Dgs='b1; //1为真

那么:

Crd &&Dgs 结果为0(假)

Crd || Dgs 结果为1(真)

!Dgs 结果为0(假)

对于向量操作,非0向量作为1处理。例如,假定:

A_Bus = 'b0110; B__Bus=b0100;那么:

A_Bus || B_Bus 结果为1

A_Bus && B_Bus 结果为1

并且:

!A_Bus 与!B_Bus的结果相同。

结果为0。

如果任意一个操作数包含x,结果也为x。

!x 结果为x

2 按位操作符

按位操作符有:

● ~(一元非)

● &(二元与)

● |(二元或)

● ^(二元异或)

● .~^,^~(二元异或非)

这些操作符在输入操作数的对应位上按位操作,并产生向量结果。下表显示对于不同操作符按步操作的结果。

|-----|---|---|---|---|
| &与 | 0 | 1 | X | Z |
| 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 1 | X | X |
| X | 0 | X | X | X |
| Z | 0 | X | X | X |
[按位与]

|-----|---|---|---|---|
| |或 | 0 | 1 | X | Z |
| 0 | 0 | 1 | X | Z |
| 1 | 1 | 1 | 1 | 1 |
| X | X | 1 | X | X |
| Z | X | 1 | X | X |
[按位或]

|------|---|---|---|---|
| ^异或 | 0 | 1 | X | Z |
| 0 | 0 | 1 | X | X |
| 1 | 1 | 0 | X | X |
| X | X | X | X | X |
| Z | X | X | X | X |
[按位异或]

|-------|---|---|---|---|
| ^异或非 | 0 | 1 | X | Z |
| 0 | 1 | 0 | X | X |
| 1 | 0 | 1 | X | X |
| X | X | X | X | X |
| Z | X | X | X | X |
[按位异或非]

|-----|---|---|---|---|
| ~非 | 0 | 1 | X | Z |
| | 1 | 0 | X | X |
[按位非]

例如,假定,

A = 'b0110;

B = 'b0100;

那么:

AIB 结果为0110

A&B 结果为0100

如果操作数长度不相等,长度较小的操作数在最左侧添0补位。例如,

'b0110 ^ 'b10000

与如下式的操作相同:

b00110 ^b10000

结果为'b10110。

3 仿真

为了对逻辑操作符和按位操作符有更明显的对比,这里编写相应代码,通过仿真观察逻辑操作符与按位操作符的实际区别。

代码:

复制代码
`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2024/05/17 09:00:09
// Design Name: 
// Module Name: and_test
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module and_test(
input        clk,
input  [3:0] a,
input  [3:0] b,
output [3:0] and_0,
output [3:0] and_1,
output [3:0] or_0,
output [3:0] or_1,
output [3:0] not_0,
output [3:0] not_1,
output [3:0] xor_0,
output [3:0] xor_not_0
    );
    
  assign  and_0 = a & b;
  assign  and_1 = a && b;
  
  assign  or_0 = a | b;
  assign  or_1 = a || b;
  
  assign not_0 = !a;
  assign not_1 = ~a;
  
  assign xor_0 = a ^ b;
  assign xor_not_0 = a ~^ b;
endmodule

仿真代码:

复制代码
`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2024/04/17 11:51:13
// Design Name: 
// Module Name: SIM
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module SIM(
    );
    reg        clk;
    reg  [3:0] a;
    reg  [3:0] b;
    wire [3:0] and_0;
    wire [3:0] and_1;
    wire [3:0] or_0;
    wire [3:0] or_1;
    wire [3:0] not_0;
    wire [3:0] not_1;
    wire [3:0] xor_0;
    wire [3:0] xor_not_0;
    
and_test and_test(
.  clk(clk),
.  a(a),
.  b(b),
.  and_0(and_0),
.  and_1(and_1),
.  or_0(or_0),
.  or_1(or_1),
.  not_0(not_0),
.  not_1(not_1),
.  xor_0(xor_0),
.  xor_not_0(xor_not_0)
    );
    
    initial
      begin      
        clk = 'b0;
        a = 4'b0010;
        b = 4'b1110;        
      
      end
     always #5 clk = !clk;
endmodule

仿真结果:

相关推荐
千宇宙航6 小时前
闲庭信步使用图像验证平台加速FPGA的开发:第十课——图像gamma矫正的FPGA实现
图像处理·计算机视觉·缓存·fpga开发
fei_sun7 小时前
【FPGA】LUT如何实现组合逻辑、时序逻辑
fpga开发
小眼睛FPGA8 小时前
【RK3568+PG2L50H开发板实验例程】FPGA部分 | 以太网传输实验例程
科技·单片机·嵌入式硬件·ai·fpga开发·fpga
千宇宙航9 小时前
闲庭信步使用图像验证平台加速FPGA的开发:第十二课——图像增强的FPGA实现
图像处理·计算机视觉·fpga开发
hahaha60161 天前
通过Tcl脚本命令:set_param labtools.auto_update_hardware 0
fpga开发
霖001 天前
FPGA通信设计十问
运维·人工智能·经验分享·vscode·fpga开发·编辑器
悲喜自渡7212 天前
硬件加速(FPGA)
fpga开发
雨霁初曦2 天前
串行数据检测器,检测到011,Y输出1,否则为0.
fpga开发·数字电路与逻辑设计·logisim
Major_pro2 天前
MIG_IP核的时钟系统
fpga开发
小眼睛FPGA2 天前
【RK3568+PG2L50H开发板实验例程】FPGA部分 | DDR3 读写实验例程
科技·嵌入式硬件·ai·fpga开发·fpga