#include "stdafx.h" #include <iostream> #include <iomanip> #include <boost\dynamic_bitset.hpp> template <class Block, class Alloc, class BlockOutputIterator> BlockOutputIterator get_bit_range(boost::dynamic_bitset<Block, Alloc> const& b, std::size_t begin, std::size_t end, BlockOutputIterator out) { Block value = 0; int pos = 0; while (begin != end) { value |= (b.test(begin) << pos++); if (pos == boost::dynamic_bitset<Block,Alloc>::bits_per_block) { pos = 0; *out = value; ++out; value = 0; } ++begin; } if (pos != 0) { *out = value; } return out; } int _tmain(int argc, _TCHAR* argv[]) { typedef unsigned char block_type; boost::dynamic_bitset<block_type> original(std::string("00011110000111100011100010001111000110001110000001111111000")); std::cout << original << std::endl; block_type value[2] = { 0, 0 }; get_bit_range(original, 3, 12, value); std::cout << std::hex << std::setfill('0') << std::setw(2) << (int) value[0] << std::setw(2) << (int) value[1] << std::endl; return 0; } |