Bitwise vs. Logical Operators
Description
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.
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
);
Answer
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
);
// or_bitwise 按位或
assign out_or_bitwise[2] = a[2] | b[2];
assign out_or_bitwise[1] = a[1] | b[1];
assign out_or_bitwise[0] = a[0] | b[0];
//out_or_logical 逻辑或
assign out_or_logical = a||b;
// 按位取反
assign out_not[5:3] = ~b[2:0];
assign out_not[2:0] = ~a[2:0];
endmodule
运算符 | 表达式 | 释义 | ||
---|---|---|---|---|
算术运算符 | +,-,*,/,% | 加,减,乘,除,取余 | ||
赋值运算符 | =,<= | 阻塞型赋值,非阻塞型赋值 | ||
关系运算符 | >,<,==,>=,<= | 大于,小于,等于,大于等于,小于等于 | ||
逻辑运算符 | &&,\ | \ | ,! | 逻辑与,或,非 |
条件运算符 | ?: | 条件运算符 | ||
位运算符 | ~,\ | ,\^,^~ | bitwise非,或,与,归约 | |
移位运算符 | <<,>> | 左移,右移 | ||
拼接运算符 | { } | 拼接 |
- 归约与(&):全部是一。
- 归约与非(~&):有一个零。
- 归约或(|):有一个一。
- 归约或非(~|):全部是零。
- 归约异或(^):一的个数是奇数。
- 归约同或(~^):一的个数是偶数。