0

I have a vector<bool> that contains 10 elements. How can I convert it to a binary type;

vector<bool> a={0,1,1,1,1,0,1,1,1,0}

I want to get binary values, something like this:

long long int x = convert2bin(s)
cout << "x = " << x << endl

x = 0b0111101110

Note: the size of vector will be change during run time, max size = 400.

0b is important, I want to use the gcc extension, or some literal type.

user1436187
  • 3,252
  • 3
  • 26
  • 59

7 Answers7

1
std::vector<bool> a = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 0 };
std::string s = "";
for (bool b : a)
{
    s += std::to_string(b);
}
int result = std::stoi(s);
vincentp
  • 1,433
  • 9
  • 12
1

As I understood of comment

Yes it can even hold 400 values

And in question

0b is important

You need to have string, not int.

std::string convert2bin(const std::vector<bool>& v)
{
    std::string out("0b");
    out.reserve(v.size() + 2);
    for (bool b : v)
    {
        out += b ? '1' : '0';
    }  
    return i;
}
ST3
  • 8,826
  • 3
  • 68
  • 92
  • I want to do binary operation later, I put `0b` based on this question: http://stackoverflow.com/questions/2611764/can-i-use-a-binary-literal-in-c-or-c – user1436187 Aug 22 '14 at 12:19
  • `0b` is just a compiler notion to interpret the following number as bin-formatted, it does not affect the underlying type, e.g. `int`. You can't expect the function to generate `0b something` and hope this will be some binary-literal-other-than-usual-int. – Piotr Skotnicki Aug 22 '14 at 12:21
  • At least reserve space in the string. – Lightness Races in Orbit Aug 22 '14 at 12:24
1

If the number of bits is known in advance and by some reason you need to start from an std::array rather than from an std::bitset directly, consider this option (inspired by this book):

#include <sstream>
#include <iostream>
#include <bitset>
#include <array>
#include <iterator>

/**
 * @brief Converts an array of bools to a bitset
 * @tparam nBits the size of the array
 * @param bits the array of bools
 * @return a bitset with size nBits
 * @see https://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_192.html
 */    
template <size_t nBits>
std::bitset<nBits> BitsToBitset(const std::array<bool, nBits> bits)
{
    std::ostringstream oss;
    std::copy(std::begin(bits), std::end(bits), std::ostream_iterator<bool>(oss, ""));
    return std::bitset<nBits>(oss.str());
}

int main()
{
    std::array<bool, 10> a = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 0 };
    unsigned long int x = BitsToBitset(a).to_ulong();
    std::cout << x << std::endl;
    return x;
}
ferdymercury
  • 698
  • 4
  • 15
0

If you really want to do this, you start from the end. Although I support Marius Bancila and advise to use a bitset instead.

int mValue = 0
for(int i=a.size()-1, pos=0; i>=0; i--, pos++)
    {
        // Here we create the bitmask for this value
        if(a[i] == 1)
        {
            mask = 1;
            mask << pos;
            myValue |= mask;
        }
    }
Laszlo Fuleki
  • 332
  • 1
  • 7
0

Your x is just an integer form from a, so can use std::accumulate like following

long long x = accumulate(a.begin(), a.end(), 0, 
                   [](long long p, long long q) 
                   { return (p << 1) + q; }
                  );

For a 400 size, you need a std::string though

P0W
  • 46,614
  • 9
  • 72
  • 119
0

First of all the result of the conversion is not a literal. So you may not use prefix 0b applied to variable x.

Here is an example

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
#include <limits>

int main() 
{
    std::vector<bool> v = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 0 };

    typedef std::vector<bool>::size_type size_type;
    size_type n = std::min<size_type>( v.size(), 
                                       std::numeric_limits<long long>::digits + 1 );

    long long x = std::accumulate( v.begin(), std::next( v.begin(), n ), 0ll,
                                   []( long long acc, int value )
                                   {
                                     return acc << 1 | value;
                                   } );

    for ( int i : v ) std::cout << i;
    std::cout << std::endl;

    std::cout << std::hex << x << std::endl;

    return 0;
}

The output is

0111101110
1ee
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

vector<bool> is already a "binary" type.

Converting to an int is not possible for more bits than available in an int. However if you want to be able to print in that format, you can use a facet and attach it to the locale then imbue() before you print your vector<bool>. Ideally you will "store" the locale once.

I don't know the GNU extension for printing an int with 0b prefix but you can get your print facet to do that.

A simpler way is to create a "wrapper" for your vector<bool> and print that.

Although vector<bool> is always internally implemented as a "bitset" there is no public method to extract the raw data out nor necessarily a standard representation for it.

You can of course convert it to a different type by iterating through it, although I guess you may have been looking for something else?

CashCow
  • 30,981
  • 5
  • 61
  • 92