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

仿真结果:

相关推荐
风_峰1 天前
Ubuntu Linux SD卡分区操作
嵌入式硬件·ubuntu·fpga开发
FPGA_Linuxer1 天前
FPGA 40 DAC线缆和光模块带光纤实现40G UDP差异
网络协议·fpga开发·udp
风_峰2 天前
Petalinux相关配置——ZYNQ通过eMMC启动
嵌入式硬件·ubuntu·fpga开发
风_峰2 天前
【ZYNQ开发篇】Petalinux和电脑端的静态ip地址配置
网络·嵌入式硬件·tcp/ip·ubuntu·fpga开发
碎碎思2 天前
一块板子,玩转 HDMI、USB、FPGA ——聊聊开源项目 HDMI2USB-Numato-Opsis
fpga开发
ooo-p2 天前
FPGA学习篇——Verilog学习Led灯的实现
学习·fpga开发
嵌入式-老费2 天前
Zynq开发实践(FPGA之选择开发板)
fpga开发
风_峰2 天前
PuTTY软件访问ZYNQ板卡的Linux系统
linux·服务器·嵌入式硬件·fpga开发
电子凉冰3 天前
FPGA入门-状态机
fpga开发
Aczone283 天前
硬件(十)IMX6ULL 中断与时钟配置
arm开发·单片机·嵌入式硬件·fpga开发