Boost logo

Boost Users :

From: Graham Reitz (graham.cpp_at_[hidden])
Date: 2007-05-29 15:23:55


Can anyone suggest a more efficient method to break apart a 64 bit dynamic
bitset into 32 bit unsigned int chunks?

The conversion is needed because eventually a C style system IO call needs
to be performed that can't deal with a boost::dynamic_bitset or std::bitset;

Thanks for any help/advice,
Graham

This works, but it feels inefficient:

#include <iostream>
#include "boost/dynamic_bitset.hpp"

using namespace std;
using namespace boost;

// Create a 64 bit dynamic bitset from two 32 bit words, not 64 bit
portable, illustration only
dynamic_bitset<> create_64_bitset(unsigned int ms_word, unsigned int
ls_word);

int main(int argc, char* argv[])
{
    dynamic_bitset<> bitset_64(
        create_64_bitset(static_cast<unsigned>(0xaaaaaaaa),
                                    static_cast<unsigned>(0xbbbbbbbb)));

    cout << bitset_64 << endl;

    // This whole portion, below, seems inefficient. Is it? Is there a
better method?

    // Break 64 bit dynamic bitset into 32 bit words
    dynamic_bitset<> temp_bitset(bitset_64);

    // Get least sig. 32 bits
    unsigned int ls_word = ((bitset_64 <<= 32) >>= 32).to_ulong();

    // Get most sig 32 bits
    unsigned int ms_word = (temp_bitset >>= 32).to_ulong();

    cout << ls_word << endl << ms_word << endl;

    cin >> argc;

    return 0;
}

dynamic_bitset<> create_64_bitset(unsigned int ms_word, unsigned int
ls_word)
{
    // Create a 64 bitset
    dynamic_bitset<> most_sig_word(64, ms_word);

    most_sig_word <<= 32;

    dynamic_bitset<> least_sig_word(64, ls_word);

    // Combine the two into one 64 bit bitset
    most_sig_word |= least_sig_word;

    return most_sig_word;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net