Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split std::bitset in two halves?

I am implementing DES algorithm and I need to split std::bitset<56> permutationKey in two halves.

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;

I tried to typecast bitset<56> to bitset<28> but it didn't work.

Other way to achieve the same thing is to iterate and assign each bit individually. I want to achieve it without using loops there must be another way.

I was able to do it with primitive types

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

How can I split bitset like this?

like image 473
Abdul Rehman Avatar asked Apr 03 '16 13:04

Abdul Rehman


1 Answers

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() );
like image 114
Richard Hodges Avatar answered Sep 18 '22 12:09

Richard Hodges