比较不同大小的位域

发布于 2024-11-14 20:55:12 字数 739 浏览 2 评论 0原文

如果使用按位运算符(&| 等)来比较两个不同大小的位域,会发生什么情况?

例如,比较 0 1 1 00 0 1 0 0 0 0 1

0 1 1 0 0 0 0 0 The smaller one is extended with zeros and pushed to the
0 0 1 0 0 0 0 1 most-significant side.

Or...

0 0 0 0 0 1 1 0 The smaller one is extended with zeros and pushed to the
0 0 1 0 0 0 0 1 least-significant side.

Or...

0 1 1 0 The longer one is truncated from its least-significant side,
0 0 1 0 keeping its most significant side.

Or...

0 1 1 0 The longer one is truncated from its most-significant side,
0 0 0 1 keeping its least-significant side.

What happens if you use a bitwise operator (&, |, etc.) to compare two bitfields of different sizes?

For example, comparing 0 1 1 0 with 0 0 1 0 0 0 0 1:

0 1 1 0 0 0 0 0 The smaller one is extended with zeros and pushed to the
0 0 1 0 0 0 0 1 most-significant side.

Or...

0 0 0 0 0 1 1 0 The smaller one is extended with zeros and pushed to the
0 0 1 0 0 0 0 1 least-significant side.

Or...

0 1 1 0 The longer one is truncated from its least-significant side,
0 0 1 0 keeping its most significant side.

Or...

0 1 1 0 The longer one is truncated from its most-significant side,
0 0 0 1 keeping its least-significant side.

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

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

发布评论

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

评论(3

半岛未凉 2024-11-21 20:55:12

位运算符始终作用于提升的操作数。因此,究竟会发生什么情况取决于一个(或两个)位域是否已签名(因为这可能会导致符号扩展)。

因此,对于您的示例值,具有二进制值 0 1 1 0 的位字段将提升为 int 6,并且二进制值 0 0 1 0 0 0 0 1 的位域将被提升为 int 33,这些操作数将与任何操作一起使用。

The bitwise operators always work on promoted operands. So exactly what might happen can depend on whether one (or both) bitfields are signed (as that may result in sign extension).

So, for your example values, the bit-field with the binary value 0 1 1 0 will be promoted to the int 6, and the bit-field with the binary value 0 0 1 0 0 0 0 1 will be promoted to the int 33, and those are the operands that will be used with whatever the operation is.

浮华 2024-11-21 20:55:12

0 0 0 0 0 1 1 0 较小的一个用零扩展并推入
0 0 1 0 0 0 0 1 最低有效侧。

0 0 0 0 0 1 1 0 The smaller one is extended with zeros and pushed to the
0 0 1 0 0 0 0 1 least-significant side.

若相惜即相离 2024-11-21 20:55:12

如果您实际上将这些值用作位域,那么比较不同大小的位域有何意义?它会给你带来有意义的结果吗?

也就是说,两个操作数都将提升为最小大小 int/unsigned,其符号取决于原始操作数的符号。然后这些提升的值将与按位运算符进行比较。

这与您的第二个示例相同:较小的一个在 MSB 侧用零填充(如果您愿意,可以推到 LSB 侧)。

如果一个操作数有符号且为负,而另一个操作数无符号,则在进行位运算之前,负数将转换为全等的无符号数。

如果您指的是 std::bitset 而不是整数,则无法对不同大小的位集进行按位运算。

If you're actually using the values as bitfields, what's the meaning of comparing bitfields of different sizes? Would it generate a meaningful result for you?

That said, both operands will be promoted to a minimum size of int/unsigned with signedness depending on the signedness of the original operands. Then these promoted values will be compared with the bitwise operator.

This behaves as your second example: The smaller one is padded with zeroes on the MSB side (pushed to LSB side if you prefer).

If one operand is signed and negative while the other is unsigned, the negative one will be converted to the congruent unsigned number before the bit operation takes place.

If instead of integral numbers you mean std::bitset, you can't do bitwise operations on bitsets of differing sizes.

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