|
pack
Syntax:
#include <boost/bit_logic/bit_container_type.hpp> void pack ( PackT const & pack_data ) ; void pack <EncodingT> ( PackT const & pack_data ) ; void pack ( PackT const & pack_data, size_type upper_bit_idx, size_type lower_bit_idx ) ; void pack <EncodingT> ( PackT const & pack_data, size_type upper_bit_idx, size_type lower_bit_idx ) ; Template Arguments :
Note: These functions are equivelant for all bit_container types. bit_array<N>, bit_vector, bit_container_adaptorPack function encodes a series of bits within a Bit Container with a given value. The value of the packed type is checked to see if it fits within the given range of bits selected. For example:
bit_array<10> a ; // a could also be a bit_vector<>(10)
enum color { red = 0, green = 1, blue = 2, pink = 22 } ;
a.pack( red, 1, 0 );
color my_color = a.unpack<color> ( 1, 0);
This above code allows for an enumerated type to be represented in 2 bits. The value 0 can be represented by 2 bits, and a.pack( pink, 2, 0); This code throws an exception as the binary value of 22 : 10110 requires a minimum of 5 bits to be represented fully. This container adheres to the strict guarantee and no data within v has been harmed by the attempt to pack data that would overflow into it. uint32_t my_uint = 42 ; /// 101010 binary representation a.pack( my_uint, 8, 3 ) ; uint32_t another_uint = a.unpack>uint32_t< (9,3); This code packs the 32 bit unsigned integral type of value 42 into the most significant 7 bits of a. The value requires a minmum of 6 bits to be precisely represented within that bit range. Signed integral bit representation within a bit_container is defaulted to 2's complement. int32_t my_int = -1 ; a.pack( my_int, 8, 0 ) ; cout << a << endl ; This will print out : 111111111 a.pack<signed_magnitude>( my_int, 8, 0 ) ; cout << a << endl ; This will print out : 100000001 pack can throw any of the following exceptions. Exceptions:
|