Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ostream>
- #include <iostream>
- //#include<stdio.h>
- #include <cstdlib>
- #include <iterator>
- #include <vector>
- #include <boost/dynamic_bitset.hpp>
- // Make the block size one byte
- typedef boost::dynamic_bitset<unsigned char> Bitset;
- typedef std::vector<unsigned char> bytesArray;
- bytesArray dynamic_bitset_to_bytes(Bitset bitset){
- bytesArray bytes;
- boost::to_block_range(bitset, std::back_inserter(bytes));
- return bytes;
- }
- Bitset convert_bytes_to_dynamic_bitset(int bitset_size, bytesArray bytes){
- //revert values from bytes to dynamic bitset
- Bitset bitset2(bitset_size);
- int bitcount=0;
- for(int byteCount=0; byteCount<bytes.size(); ++byteCount) {
- int mask=0x80;
- for(int individualBit=8; individualBit>0;--individualBit) {
- bitset2[(byteCount*8) + individualBit-1]=bytes[byteCount] & mask ? 1 : 0;
- mask >>= 1;
- }
- }
- return bitset2;
- }
- int main()
- {
- Bitset bitset(40); // 40 bits
- // Assign random bits
- for (int i=0; i<40; ++i)
- {
- bitset[i] = std::rand() % 2;
- }
- // Copy bytes to buffer
- bytesArray bytes;
- bytes = dynamic_bitset_to_bytes(bitset);
- Bitset bitset2(40);
- bitset2 = convert_bytes_to_dynamic_bitset(40, bytes);
- std::cout << "bitset1: " << bitset << std::endl;
- std::cout << "bitset2: " << bitset2 << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement