bitset rotational shift?

P

PengYu.UT

I need to do rotational shift on bitset. But I couldn't find that
operation in the standard. Is there any workaround or other packages to
do this?

Thanks,
Peng
 
H

Howard Hinnant

I need to do rotational shift on bitset. But I couldn't find that
operation in the standard. Is there any workaround or other packages to
do this?

You can do it the same way you would on an unsigned integral type (which
shifts):

#include <bitset>
#include <string>
#include <iostream>

template <std::size_t N>
inline
void
rotate(std::bitset<N>& b, unsigned m)
{
b = b << m | b >> (N-m);
}

int main()
{
std::bitset<20> b(std::string("00001111111111111111"));
std::cout << b << '\n';
rotate(b, 4);
std::cout << b << '\n';
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top