Array of characters, or array of integers 1s and 0s? One way perhaps (for a character array), use the bitset to_string() member function along with the string c_str() member function to get a c-style character string representation of the bitset that you can then strcpy into a temp array.Originally Posted by devour89
Again, should output: 11011010Code:#include <cstring> #include <bitset> #include <iostream> #include <string> // Not sure if needed in this instance, just in case int main() { std::bitset<8> bits(218); char array[9]; // Extra space to hold NULL copied during strcpy strcpy(array,bits.to_string().c_str()); std::cout << array << std::endl; return 0; }
Edit: Forgot to mention that you can use the array indexing operator [] to access individual bits from the bitset in an array like manner which can be used in a loop to initialize an array. I.e. for the bitset in the above example we have:
bits[0] = 0
bits[1] = 1
bits[2] = 0
bits[3] = 1
bits[4] = 1
bits[5] = 0
bits[6] = 1
bits[7] = 1
Using that principal might be useful for you.