【发布时间】:2017-01-30 17:15:40
【问题描述】:
下面我构建了一个我正在处理的使用位字段的代码的小示例。 在实现比较运算符时,我注意到它没有按预期工作。问题在于 -1 似乎并不小于 1。
考虑了一会儿,在我看来构造函数中的掩码有问题。
我在那里添加了“aValue & MAX_VALUE”,否则编译器会发出转换警告。但是,首先使用 MAX_VALUE 进行掩码,然后强制转换为 int32_t 不会给我正确的结果。
有谁知道我该如何解决这个问题?
当我在 main() 方法中编写 minusOne.v = -1; 时,我似乎确实得到了预期的结果。但我不确定如何在构造函数中编写它(没有收到转换警告)。
#include <iostream>
#include <cstdint>
using namespace std;
struct int27_t
{
int32_t v:27;
constexpr static int32_t MIN_VALUE = -(1L << 26);
constexpr static int32_t MAX_VALUE = (1L << 26) - 1;
constexpr static int32_t MASK = 0x7FFFFFF;
int27_t() : v(0) {}
explicit int27_t(int32_t aValue) : v(static_cast<int32_t>(aValue & MAX_VALUE)) {}
friend bool operator<(const int27_t& aLhs, const int27_t& aRhs);
};
bool operator<(const int27_t& aLhs, const int27_t& aRhs)
{
return aLhs.v < aRhs.v;
}
int main()
{
int27_t minusOne{-1};
int27_t plusOne{1};
cout << "MAX_VALUE == " << int27_t::MAX_VALUE << " == 0x" << hex << int27_t::MAX_VALUE << dec << endl;
cout << "-1 == " << minusOne.v << " == 0x" << hex << minusOne.v << dec << endl;
cout << "-1 cast to int32_t: " << static_cast<int32_t>(minusOne.v) << " == 0x" << hex << static_cast<int32_t>(minusOne.v) << dec << endl;
cout << "-1 < 1 ? " << (minusOne < plusOne) << endl;
cout << endl;
}
程序输出
MAX_VALUE == 67108863 == 0x3ffffff
-1 == 67108863 == 0x3ffffff
-1 cast to int32_t: 67108863 == 0x3ffffff
-1 < 1 ? 0
【问题讨论】:
-
这里真的没有理由使用位域。在现代平台上,该结构无论如何都会被四舍五入到 32 位。同样,屏蔽和two's-complement 不兼容,符号位会被撕掉。
-
@tadman,我理解使用 MAX_VALUE 撕裂符号位进行掩蔽。但是我该如何解决呢?使用 MASK 值进行屏蔽会导致转换警告。
-
我认为@kamajii 有一些很好的建议,我鼓励你遵循它。
标签: c++ bit-fields