	#include <iostream>
	#include <bitset>
	
	using namespace std;
	
	template<std::size_t num_bits>
	void writeBits(ostream& os, const std::bitset<num_bits>& thebits)
	{
		for(std::size_t bit_pos = 0; bit_pos < num_bits;)
		{
			unsigned char currentByte = 0;
			for(int currentBit = 0; currentBit < sizeof(unsigned char) && bit_pos < num_bits; ++currentBit, ++bit_pos)
			{
				currentByte |= thebits[bit_pos] ? (0x80 >> currentBit) : 0;
			}
			os << currentByte;
		}
	}
	
	int main() {
		std::bitset<4> nibble(std::string("1100"));
		std::bitset<8> byte(std::string("11001010"));
		std::bitset<23> number_of_bits(std::string("10101010101010101010101"));
		
		writeBits(std::cout,nibble);
		writeBits(std::cout,byte);
		writeBits(std::cout,number_of_bits);
		
		return 0;
	}