c – 将std :: bitset分成两半?

我正在实现DES算法,我需要拆分std :: bitset< 56> permutationKey分两半.

std::bitset<56> permutationKey(0x133457799BBCDF);
std::bitset<28> leftKey;
std::bitset<28> rightKey;

std::bitset<56> divider(0b00000000000000000000000000001111111111111111111111111111);

rightKey = permutationKey & divider;
leftKey = (permutationKey >> 28) & divider;

我试图对bitset< 56>进行类型转换. bitset< 28>但它不起作用.

实现相同目的的其他方法是迭代并单独分配每个位.我想在不使用循环的情况下实现它必须有另一种方式.

我能用原始类型做到这一点

uint64_t key = 0b0001010101010101110110001100001110000011111100000000011111000000;
                    //00010101.01010101.11011000.11000011---|---10000011.11110000.00000111.11000000
uint32_t right = (uint32_t)key;
uint32_t left = key >> 32;

我怎么能像这样拆分bitset?

最佳答案
std::bitset<56> permutationKey(0x133457799BBCDF);
std::bitset<56> divider(0b00000000000000000000000000001111111111111111111111111111);

auto rightKey = std::bitset<28> ( (permutationKey & divider).to_ulong() );
auto leftKey = std::bitset<28> ( ((permutationKey >> 28) & divider).to_ulong() );

转载注明原文:c – 将std :: bitset分成两半? - 代码日志