二进制数比较

发布于 2024-09-01 04:48:49 字数 166 浏览 5 评论 0原文

如果我有一个 32 位二进制补码数字,并且我想知道知道两个数字相等的最简单方法是什么...知道这个的最快按位运算符是什么?我知道对两个数字进行异或并检查结果是否为零效果很好......还有其他的吗?

如果一个数字大于0怎么办?我可以检查第 31 位,看看它是否大于或等于 0..但是 bgtz 怎么样?

If I have a 32 bit two's complement number and I want to know what is the easiest way to know of two numbers are equal... what would be the fastest bitwise operator to know this? I know xor'ing both numbers and check if the results are zero works well... any other one's?

how about if a number is greater than 0?? I can check the 31'st bit to see if it's greater or equal to 0..but how about bgtz?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ㄖ落Θ余辉 2024-09-08 04:48:49

与您的评论相反,“==”是 Verilog 的一部分,除非我今晚的记忆力比平常差很多,否则它应该可以很好地综合。例如,您可以编写如下内容:

// warning: untested, incomplete and utterly useless in any case.
// It's been a while since I wrote much Verilog, so my syntax is probably a bit off
// anyway (might well be more like VHDL than it should be).
//
module add_when_equal(clock, a, b, x, y, z);
input clock;
input [31:0] a, b, x, y;
output [31:0] z;
reg [31:0] a, b, x, y, z;

always begin: main
   @(posedge clock);
   if (a == b)
       z <= x + y;
end
endmodule;

Verilog 还支持您通常期望的其他比较运算符(!=、<= 等)。合成器相当“智能”,因此像 x != 0 这样的东西通常会合成到 N 输入 OR 门而不是比较器。

Contrary to your comments, '==' is part of Verilog, and unless my memory is a lot worse than usual tonight, it should synthesize just fine. Just for example, you could write something like:

// warning: untested, incomplete and utterly useless in any case.
// It's been a while since I wrote much Verilog, so my syntax is probably a bit off
// anyway (might well be more like VHDL than it should be).
//
module add_when_equal(clock, a, b, x, y, z);
input clock;
input [31:0] a, b, x, y;
output [31:0] z;
reg [31:0] a, b, x, y, z;

always begin: main
   @(posedge clock);
   if (a == b)
       z <= x + y;
end
endmodule;

Verilog also supports the other comparison operators you'd normally expect (!=, <=, etc.). Synthesizers are fairly "smart", so something like x != 0 will normally synthesize to an N-input OR gate instead of a comparator.

热鲨 2024-09-08 04:48:49
// this should work as comparator for Equality
wire [31:0] Cmp1, Cmp2;
wire Equal;
assign Equal  =  &{Cmp1 ~^ Cmp2}; // using XNOR
assign Equal  = ~|{Cmp1  ^ Cmp2}; // using XOR
// this should work as comparator for Equality
wire [31:0] Cmp1, Cmp2;
wire Equal;
assign Equal  =  &{Cmp1 ~^ Cmp2}; // using XNOR
assign Equal  = ~|{Cmp1  ^ Cmp2}; // using XOR
顾北清歌寒 2024-09-08 04:48:49

如果您可以进行异或,然后将结果与零进行比较,那么您可以将结果与某个值进行比较,如果您可以将某个值与某个值进行比较,那么您可以只比较这两个值,而无需使用异或和 32 位零。

if you can xor and then compare the result with zero then you can compare a result with some value and if you can compare something to a value then you can just compare the two values without using an xor and a 32 bit zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文