utils.hpp
1 #ifndef SWISSARMYKNIFE_BITS_UTILS_HPP
2 #define SWISSARMYKNIFE_BITS_UTILS_HPP
3 
4 #include <pre/detail/namespace_compatibility.hpp>
5 
6 #include <iostream>
7 #include <bitset>
8 #include <string>
9 #include <sstream>
10 #include <math.h>
11 #include <boost/assert.hpp>
12 #include <boost/cstdint.hpp>
13 #include <boost/dynamic_bitset.hpp>
14 #include <boost/container/vector.hpp>
15 
16 namespace pre { namespace bits {
17 
22  inline std::string to_string(const boost::dynamic_bitset<boost::uint8_t> & bitset) {
23  std::string result;
24  boost::to_string(bitset, result);
25  return result;
26  }
27 
33  template <typename uintX_t>
34  inline std::string to_binstring(uintX_t value) {
35  std::stringstream bitsRepresentation;
36  const int shift = 8 * sizeof( uintX_t ) - 1;
37  uintX_t one = 1;
38  const uintX_t mask = one << shift;
39 
40  for ( size_t i = 1; i <= shift + 1; i++ ) {
41  bitsRepresentation << ( value & mask ? '1' : '0' );
42  value <<= 1;
43  }
44 
45  return bitsRepresentation.str();
46  }
47 
48  template <typename uintX_t, size_t bitsetSize>
49  inline uintX_t to_uint(const std::bitset<bitsetSize> value) {
50  const size_t uintX_t_size = 8 * sizeof ( uintX_t );
51 
52  BOOST_ASSERT(uintX_t_size >= bitsetSize);
53 
54  const uintX_t one = 1;
55  const uintX_t zero = 0;
56  uintX_t result = 0x0;
57 
58  for (size_t i=0; i < value.size(); ++i) {
59  result |= (value.test(i)) ? (one << i) : (zero << i);
60  }
61 
62  return result;
63  }
64 
72  inline boost::dynamic_bitset<boost::uint8_t> & shift_in_bitset(boost::dynamic_bitset<boost::uint8_t> &hostBitset, const boost::dynamic_bitset<boost::uint8_t> & clientBitset) {
73  boost::dynamic_bitset<boost::uint8_t> clientBitsetCopy = clientBitset;
74  clientBitsetCopy.resize(hostBitset.size()); // Resize to have the hostBitset size
75 
76  hostBitset <<= clientBitset.size();
77  hostBitset |= clientBitsetCopy;
78 
79  return hostBitset;
80  }
81 
87  inline boost::container::vector<boost::uint8_t> to_bytearray(const boost::dynamic_bitset<boost::uint8_t> &bitset) {
88  size_t sizeBytesPadded = static_cast<size_t>(ceil(static_cast<double>(bitset.size()) / CHAR_BIT));
89  boost::container::vector<boost::uint8_t> bytes(sizeBytesPadded);
90  boost::container::vector<boost::uint8_t>::reverse_iterator iter = bytes.rbegin();
91  boost::to_block_range(bitset, iter);
92 
93  return bytes;
94  }
95 
101  inline boost::dynamic_bitset<boost::uint8_t> to_bitset(const boost::container::vector<boost::uint8_t> &byteArray, size_t resultingBitSetSize = 0) {
102  boost::dynamic_bitset<boost::uint8_t> bitset((resultingBitSetSize == 0) ? (byteArray.size() * CHAR_BIT) : resultingBitSetSize);
103  boost::from_block_range(byteArray.rbegin(), byteArray.rend(), bitset);
104 
105  return bitset;
106  }
107 }}
108 
109 #endif
std::string to_string(const boost::dynamic_bitset< boost::uint8_t > &bitset)
Create string representation of the dynamic bitset given.
Definition: utils.hpp:22
boost::dynamic_bitset< boost::uint8_t > to_bitset(const boost::container::vector< boost::uint8_t > &byteArray, size_t resultingBitSetSize=0)
Converts a byte array into a bitset.
Definition: utils.hpp:101
boost::container::vector< boost::uint8_t > to_bytearray(const boost::dynamic_bitset< boost::uint8_t > &bitset)
This converts the given boost::dynamic_bitset to an array of 8-bit wide bytes.
Definition: utils.hpp:87
std::string to_binstring(uintX_t value)
Returns a string representation in bits of the int given.
Definition: utils.hpp:34
boost::dynamic_bitset< boost::uint8_t > & shift_in_bitset(boost::dynamic_bitset< boost::uint8_t > &hostBitset, const boost::dynamic_bitset< boost::uint8_t > &clientBitset)
This function pads the given client bitset in the host by making a copy of clientBitset, resizing the clientBitset or-ing it in the hostBitset and returning the hostBitset.
Definition: utils.hpp:72