Connecting Tech Pros Worldwide Forums | Help | Site Map

bitset rotational shift?

PengYu.UT@gmail.com
Guest
 
Posts: n/a
#1: Mar 27 '06
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


Howard Hinnant
Guest
 
Posts: n/a
#2: Mar 27 '06

re: bitset rotational shift?


In article <1143439864.704032.283850@i39g2000cwa.googlegroups .com>,
"PengYu.UT@gmail.com" <PengYu.UT@gmail.com> wrote:
[color=blue]
> 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?[/color]

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';
}
Closed Thread