Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the number of bits is a compile-time constant:</p> <pre><code>#include &lt;bitset&gt; ... std::bitset&lt;100&gt; b; b[2]=true; </code></pre> <p>If it's not, use <a href="http://www.boost.org/doc/libs/1_40_0/libs/dynamic_bitset/dynamic_bitset.html" rel="noreferrer">Boost.dynamic_bitset</a></p> <p>Or, if you're desperate, std::vector, which is indeed a packed bit vector:</p> <pre><code>#include &lt;vector&gt; ... std::vector&lt;bool&gt; b(100); b[2]=true; </code></pre> <p>You seem to want to use a library that requires bit vectors packed in an array of bytes. Without knowing exactly what order it places the bits in, I can only note that:</p> <p>1) all of the above will probably use at least 32-bit ints with bits ordered least->most or most->least significant</p> <p>2) on little endian (Intel/AMD) CPUs, this means that the memory occupied by the bytes an array of ints may not be consistent with the ordering of bits within the int. if it's "bit 0 is the lsb of int 0, ... bit 32 is the lsb of int 1, ..." then that's the same in little endian as "bit 0 is the lsb of char 0, ... bit 32 is the lsb of char 4 ...", in which case you can just cast a pointer to the int array to a pointer to char array</p> <p>3) supposing the native order of bytes in your bit set / vector isn't exactly what the library needs, then you have to either have to create your own that has the layout they want, or transcribe a copy into their layout. </p> <p>a) if the order of bits within a byte is different, a 256 entry lookup table giving the byte with bits reversed would be efficient. you could generate the table with a small routine.</p> <p>b) to reverse bytes from little&lt;->big endian:</p> <pre><code>inline void endian_swap(unsigned short&amp; x) { x = (x&gt;&gt;8) | (x&lt;&lt;8); } inline void endian_swap(unsigned int&amp; x) { x = (x&gt;&gt;24) | ((x&lt;&lt;8) &amp; 0x00FF0000) | ((x&gt;&gt;8) &amp; 0x0000FF00) | (x&lt;&lt;24); } inline void endian_swap(unsigned long long&amp; x) { x = (x&gt;&gt;56) | ((x&lt;&lt;40) &amp; 0x00FF000000000000) | ((x&lt;&lt;24) &amp; 0x0000FF0000000000) | ((x&lt;&lt;8) &amp; 0x000000FF00000000) | ((x&gt;&gt;8) &amp; 0x00000000FF000000) | ((x&gt;&gt;24) &amp; 0x0000000000FF0000) | ((x&gt;&gt;40) &amp; 0x000000000000FF00) | (x&lt;&lt;56); } </code></pre> <p>To get/set a particular bit within a word, with bit #0 in the least significant bit of word 0:</p> <pre><code>typedef unsigned char block_t; const unsigned block_bits=8; inline void set_bit(block_t *d,unsigned i) { unsigned b=i/block_bits; unsigned bit=i-(block_bits*b); // same as i%b block_t &amp;bl=d[b]; bl|=(1&lt;&lt;bit); // or bit with 1 (others anded w/ 0) } inline void clear_bit(block_t *d,unsigned i) { unsigned b=i/block_bits; unsigned bit=i-(block_bits*b); // same as i%b block_t &amp;bl=d[b]; bl&amp;=(~(1&lt;&lt;bit)); // and bit with 0 (other bits anded w/ 1) } inline void modify_bit(block_t *d,unsigned i,bool val) { if (val) set_bit(d,i) else clear_bit(d,i); } inline bool get_bit(block_t const* d,unsigned i) { unsigned b=i/block_bits; unsigned bit=i-(block_bits*b); // same as i%b return d[b]&amp;(1&lt;&lt;bit); } </code></pre> <p>Obviously if the rule for bit organization differs, you have to change the above.</p> <p>Using the widest possible int your CPU processes efficiently as block_t is best (dont' forget to change <code>block_bits</code>), unless the endianness doesn't work out w/ the library you're using.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload