thomas
thomas

Reputation: 161

How to implement dynamic bitset in my specific code

I am using bitset and to improve the performance of my code I want to change it to dynamic bitset, but after reading some posts related to this, I still don't know the way to define my code.

So I attached my code and I would like to know if any of you could help me giving me some ideas about what should I modify and how.

Thanks in advance :)

// Program that converts a number from decimal to binary and show the positions
// where the bit of the number in binary contains 1

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

using namespace std;


int main()
{

    unsigned long long int dec;
    bitset<5000> binaryNumber;
    bitset<5000> mask;
    mask = 0x1;

    cout << "Write a number in decimal: ";
    cin >> dec;

    // Conversion from decimal to binary
    int x;
    for (x = 0; x < binaryNumber.size(); x++)
    {
        binaryNumber[x] = dec % 2;
        dec = dec / 2;
    }

    cout << "The number " << dec << " in binary is: ";
    for (x = (binaryNumber.size() - 1); x >= 0; x--)
    {
        cout << binaryNumber[x];
    }
    cout << endl;

    // Storage of the position with 1 values
    vector<int> valueTrue;
    for (int r = 0; r < binaryNumber.size(); r++) //
    {
        if (binaryNumber.test(r) & mask.test(r)) // if both of them are bit "1"
                                                 // we store in valueTrue vector
        {
            valueTrue.push_back(r);
        }
        mask = mask << 1;
    }


    int z;
    cout << "Bit 1 are in position: ";
    for (z = 0; z < valueTrue.size(); z++)
    {
        cout << valueTrue.at(z) << " ";
    }
    cout << endl;

    system("pause");
    return 0;
}

Upvotes: 8

Views: 11798

Answers (6)

camino
camino

Reputation: 10574

Another solution using std::bitset is to define a large enough bitset, for example bitset<1000>.

Then you can use a variable to control the actual bits and you still can use the member functions of bitset

Upvotes: 0

vedranm
vedranm

Reputation: 528

For those who don't use Boost - you can use vector<bool> which is optimized so each element uses only 1 bit.

http://www.cplusplus.com/reference/stl/vector/

Upvotes: 7

varagrawal
varagrawal

Reputation: 3032

You can do it without using the boost library.

You can dynamically assign the bitset. So instead of

for(x=0;x<binaryNumber.size();x++)
{
    binaryNumber[x]=dec%2;
    dec=dec/2;
}

You can simply do:

 binaryNumber = dec;  

Yes it does work!!

Then rather than using a separate vector to store the positions of the 1s, you can do something like this:

for(int i=0;i<binaryNumber.size();i++){
    if(binaryNumber[i])
        cout << "Position of 1: " << i << endl;
}

Hope it helps.

Upvotes: -1

Cubbi
Cubbi

Reputation: 47408

Here's your program roughly re-written with dynamic_bitset

#include <iostream>
#include <climits>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/dynamic_bitset.hpp>
int main()
{
    std::cout<<"Write a number in decimal: ";
    unsigned long long dec;
    std::cin >> dec;

    // Conversion from decimal to binary
    std::string str;
    for(unsigned long long d = dec; d>0; d/=2)
        str.insert(str.begin(), boost::lexical_cast<char>(d&1) );
    boost::dynamic_bitset<> binaryNumber(str);
    std::cout << "The number " << dec << " in binary is: " << binaryNumber<<'\n';

   // Storage of the position with 1 values
   std::vector<size_t> valueTrue;
   for( size_t pos = binaryNumber.find_first();
        pos != boost::dynamic_bitset<>::npos;
        pos = binaryNumber.find_next(pos))
       valueTrue.push_back(pos);

   std::cout<<"Bit 1 are in position: ";
   for(size_t z=0; z < valueTrue.size(); ++z)
       std::cout << valueTrue[z] << " ";
   std::cout << "\n";
}

test run: https://ideone.com/OdhWE

Note, you cannot immediately construct the bitset from your integer because its constructor expects unsigned long. If you can get by with unsigned longs, the whole conversion loop is unnecessary

Upvotes: 4

Tristram Gr&#228;bener
Tristram Gr&#228;bener

Reputation: 9711

The easiest way to have a dynamic bitset is to use one ;) http://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html

UPDATE : providing a full example

#include<iostream>
#include <boost/dynamic_bitset.hpp>
int main() {
    unsigned long long dec;
    std::cout << "Write a number in decimal: ";
    std::cin >> dec;
    boost::dynamic_bitset<> bs(64, dec);
    std::cout << bs << std::endl;
    for(size_t i = 0; i < 64; i++){
        if(bs[i])
            std::cout << "Position " << i << " is 1" << std::endl;
    }
    //system("pause");
    return 0;
}   

Upvotes: 11

Steve
Steve

Reputation: 1800

Try using Boost's dynamic_bitset.

Upvotes: 3

Related Questions