简体   繁体   中英

Convert vector<bool> to binary

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.

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);

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;
}

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;
        }
    }

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

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM