Let’s say I have a bitset<28>
called left28
.
I’m looking to left circular shift left28
.
After doing some searching, I came across std::rotl
(C++20) but it doesn’t seem to play nice with bitset
, so I have no idea how I’m going to pull this off.
>Solution :
You can implement a left circular shift by combining right shifts with left shifts.
template<size_t N>
std::bitset<N> rotl( std::bitset<N> const& bits, unsigned count )
{
return bits << count | bits >> (N - count);
// The shifted bits ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^ The wrapped bits
}
Note that unlike std::rotl
, the count in this example is unsigned
.
If you would like it to be signed
, like an int
, write a matching rotr
and have each function call the other when count
is negative.