比较不同大小的位域
如果使用按位运算符(&
、|
等)来比较两个不同大小的位域,会发生什么情况?
例如,比较 0 1 1 0
与 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
位运算符始终作用于提升的操作数。因此,究竟会发生什么情况取决于一个(或两个)位域是否已签名(因为这可能会导致符号扩展)。
因此,对于您的示例值,具有二进制值
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 theint
6
, and the bit-field with the binary value0 0 1 0 0 0 0 1
will be promoted to theint
33
, and those are the operands that will be used with whatever the operation is.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.
如果您实际上将这些值用作位域,那么比较不同大小的位域有何意义?它会给你带来有意义的结果吗?
也就是说,两个操作数都将提升为最小大小
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.