2

I have the situation where I need to split a number of bitset blocks in half. Is there a way to merge the following two functions into something resembling the third? Having something like the third feels more right and general.

pair<bitset<32>, bitset<32> > 
split_block_in_half(
  bitset<64> block
){
  bitset<block.size()> filter;
  for(size_t i = 0; i < filter.size()/2; ++i){
    filter.set(i);
  }

  return make_pair<bitset<block.size()/2>, bitset<block.size()/2> >(
    bitset<block.size()/2>(((block ^ (filter << (block.size()/2))) >> (block.size()/2)).to_ulong()),
    bitset<block.size()/2>((block ^ filter).to_ulong())
  );
}


pair<bitset<28>, bitset<28> > 
split_block_in_half(
  bitset<56> block
){
  bitset<block.size()> filter;
  for(size_t i = 0; i < filter.size()/2; ++i){
    filter.set(i);
  }

  return make_pair<bitset<block.size()/2>, bitset<block.size()/2> >(
bitset<block.size()/2>(((block ^ (filter << (block.size()/2))) >> (block.size()/2)).to_ulong()),
bitset<block.size()/2>((block ^ filter).to_ulong())
  );
}



template<
  typename N>
pair<bitset<N>, bitset<N> >
split_block_in_half(
  bitset<2*N> block
){
  bitset<block.size()> filter;
  for(size_t i = 0; i < filter.size()/2; ++i){
    filter.set(i);
  }

  return make_pair<bitset<block.size()/2>, bitset<block.size()/2> >(
    bitset<block.size()/2>(((block ^ (filter << (block.size()/2))) >> (block.size()/2)).to_ulong()),
    bitset<block.size()/2>((block ^ filter).to_ulong())
  );
}

Is there a clever compile time way to merge the two separate functions above, or is this unsupported?

3
  • 2
    template <int N>
    – iBug
    Sep 17, 2018 at 2:24
  • On the one hand it's supertrivial and basic, just asking if a language feature exists, and it does. On the other hand it can be useful to googlers coming here. Don't know whether to vote to close. Sep 17, 2018 at 2:25
  • First comment solved it. I tried searching, but without being a master of templates I don't know enough to find out. I'd guess leave it up for future googlers?
    – Josh
    Sep 17, 2018 at 2:27

2 Answers 2

3

Try an integer value in template parameter:

template <int N>
pair<bitset<N>, bitset<N>> 
split_block_in_half(
    bitset<2*N> block
)
......

The downside is that, since 2*N cannot be deduced from function calls, you need to explicitly specify N:

split_block_in_half<28>(block); // where block is bitset<56>

If you define it like this, you can use SFINAE to restrict that N be even.

template <int N>
std::enable_if_t<
    N % 2 == 0,
    pair<bitset<N/2>, bitset<N/2>>
>
split_block_in_half(bitset<N>);

If you don't have C++14, change std::enable_if_t<> to typename std::enable_if<>::type (C++11).

8
  • Now, this gets the code to compile without error if the function is not used. When using it in the way described, template deduction fails.
    – Josh
    Sep 17, 2018 at 2:35
  • @Josh No, you shouldn't do bitset<block.size()>. You must do bitset<2*N>, i.e., compile-time constant.
    – iBug
    Sep 17, 2018 at 2:41
  • It looks like the "2*N" portion runs afoul of non-deduced contexts section 3. However, this doesn't cleanly capture that block's size should be even. Maybe for a later standard. en.cppreference.com/w/cpp/language/template_argument_deduction Could you update your answer and I'll accept it?
    – Josh
    Sep 17, 2018 at 2:49
  • 1
    "2*N cannot be deduced from function calls" - what about something like this instead? template <int N> pair<bitset<N/2>, bitset<N/2>> split_block_in_half( bitset<N> &block ) and then use SFINAE to make sure it works only when N is even Sep 17, 2018 at 3:07
  • 1
    That is better than what I went with. I ended up using a static_assert(). I'll update to this later today.
    – Josh
    Sep 17, 2018 at 13:30
2

Here's a full working example that doesn't require specifying N. I implemented the logic a bit differently. I assume the goal is to have the pair's first member contain the high order bits and have second contain the low order bits. Do be aware that ^ is the logical XOR operation, though it may resemble the logical AND sometimes used in propositional logic.

template<size_t N>
pair<bitset<N/2>, bitset<N/2>> split_block_in_half(const bitset<N>& block){
    static_assert(N % 2 == 0 && N != 0, "N must be even and non-zero");
    pair<bitset<N/2>, bitset<N/2>> ret;
    for (size_t i = 0; i < N/2; ++i){
        ret.first[i] = block[i + N/2];
        ret.second[i] = block[i];
    }
    return ret;
}

int main() {

    bitset<64> bs { 0x1234567887654321 };

    auto halves = split_block_in_half(bs);

    std::cout << std::hex
        << halves.first.to_ullong() << '\n'
        << halves.second.to_ullong() << '\n';

    // prints:
    // > 12345678
    // > 87654321

    return 0;
}
1
  • I would suggest passing in block by const reference Sep 17, 2018 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.