How do I rearrange bits in bitset?

I need help with a code that rearranges the bits in bitset.

Suppose I write input binary: 11010010011011:

the output is: 0000000000000000000000000000000000000000000000000011010010011011

but I wanted the output: 1101001001101100000000000000000000000000000000000000000000000000

note that I used bitset.

this is a part of my code

using bitmask = bitset<64>

1
2
3
4
5
6
7
8
9
int main()
{
	bitmask input;
	dtmf num;
	std::vector<unsigned short int> vec;
	std::cin >> input;
	std::cout << input << std::endl;
	input = num.rotate(input, 10);// this function only shift 10 bits to left
    std::cout << input << std::endl;


1
2
3
bitmask dtmf::rotate(bitmask& bitin,unsigned int n) {
	return (bitin << n);
}
Last edited on
Your question isn't clear. Is this what you wanted?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <bitset>
#include <algorithm>
using namespace std;

using BitType = bitset<64>;

BitType bitRotate( BitType B )
{
   string str = B.to_string();
   int first1 = str.find( '1' );
   if ( first1 != string::npos ) rotate( str.begin(), str.begin() + first1, str.end() );
   return BitType( str );
}

int main()
{
   BitType B( "11010010011011" );
   cout << B << '\n';
   cout << bitRotate( B ) << '\n';
}

0000000000000000000000000000000000000000000000000011010010011011
1101001001101100000000000000000000000000000000000000000000000000
Last edited on
Exactly!!!
Topic archived. No new replies allowed.