编辑:例如here的Godbolt链接!
因此,我有一个示例来展示我制作的宏及其用例:
#include <bitset>
#include <iostream>
#define bit_mask(size, offset) (~(~0 << size) << offset)
#define bit_masked_set(dst, src, mask, offset) (dst = (dst & ~mask) | (src << offset))
#define bit_masked_get(src, mask, offset) ((src & mask) >> offset)
constexpr unsigned front_mask = bit_mask(16, 16);
constexpr unsigned back_mask = bit_mask(16, 0);
int main() {
std::cout << std::bitset<32>(front_mask) << " = front_mask (bit_mask(16, 16))\n";
std::cout << std::bitset<32>(back_mask) << " = back_mask (bit_mask(16, 0))\n\n";
unsigned value = 0;
std::cout << std::bitset<32>(value) << " = value (initial)\n\n";
bit_masked_set(value, 1, front_mask, 16);
std::cout << std::bitset<32>(value) << " = value (after bit_masked_set(value, 1, front_mask, 16))\n\n";
bit_masked_set(value, 1, back_mask, 0);
std::cout << std::bitset<32>(value) << " = value (after bit_masked_set(value, 1, back_mask, 0))\n\n";
std::cout << std::bitset<32>(value) << " = value (final)\n";
}
哪个输出:
11111111111111110000000000000000 = front_mask (bit_mask(16, 16))
00000000000000001111111111111111 = back_mask (bit_mask(16, 0))
00000000000000000000000000000000 = value (initial)
00000000000000010000000000000000 = value (after bit_masked_set(value, 1, front_mask, 16))
00000000000000010000000000000001 = value (after bit_masked_set(value, 1, back_mask, 0))
00000000000000010000000000000001 = value (final)
我将Visual Studio 2017与-Wall -c ++ 17一起使用,对此感到满意。但是我注意到,当我将其粘贴到Godbolt网站时,gcc / clang / etc会发出警告或错误提示,例如:
error: constexpr variable 'front_mask' must be initialized by a constant expression
...
error: left operand of shift expression '(-1 << 16)' is negative [-fpermissive]
...
etc
因为这些是预处理程序宏,它们使用分配给已知数据类型的常量值不应该不是问题。
Edit2:
我正在玩这个游戏,发现将我的bit_mask
宏更改为:
#define bit_mask(size, offset) (~((unsigned long long)~0 << size) << offset)
似乎可以解决此问题。我不确定100%是否需要额外的long long
,但我认为它不会有任何伤害,它会更灵活。
Edit3:
将代码从宏显着更新为简单的结构:
#include <bitset>
#include <iostream>
#include <type_traits>
struct BitMask {
using Mask_Type = unsigned long long;
Mask_Type mask;
unsigned offset;
constexpr BitMask(unsigned size, unsigned offset) :
mask{~((Mask_Type)~0 << size) << offset},
offset{offset} {
}
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
constexpr T set_bits(T& dst, T src) const {
return dst = (dst & ~mask) | (src << offset);
}
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
constexpr T get_bits(T src) const {
return (src & mask) >> offset;
}
};
constexpr BitMask front_mask = BitMask(16, 16);
constexpr BitMask back_mask = BitMask(16, 0);
int main() {
std::cout << std::bitset<32>(front_mask.mask) << " = front_mask (bit_mask(16, 16))\n";
std::cout << std::bitset<32>(back_mask.mask) << " = back_mask (bit_mask(16, 0))\n\n";
unsigned value = 0;
std::cout << std::bitset<32>(value) << " = value (initial)\n\n";
front_mask.set_bits(value, 1U);
std::cout << std::bitset<32>(value) << " = value (after bit_masked_set(value, 1, front_mask, 16))\n\n";
back_mask.set_bits(value, 1U);
std::cout << std::bitset<32>(value) << " = value (after bit_masked_set(value, 1, back_mask, 0))\n\n";
std::cout << std::bitset<32>(value) << " = value (final)\n";
}
答案 0 :(得分:0)
因此,由于#define bit_mask(size, offset) (~((unsigned long long)~0 << size) << offset)
似乎可以解决问题,因此我将其标记为已回答。如果有人发布这不是正确的解决方案,而他们提供了其他解决方案,那么我会接受他们的回答,只要它可行。