Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>#include &lt;bitset&gt; #include &lt;sstream&gt; #include &lt;iostream&gt; #include &lt;cinttypes&gt; namespace my { template&lt;size_t S&gt; std::istream &amp;operator &gt;&gt; (std::istream &amp;is, std::bitset&lt;S&gt; &amp;bits) { std::uint8_t byte; size_t i = 0; while(i &lt; S &amp;&amp; (is &gt;&gt; byte)) for(size_t j = 0; j &lt; 8 &amp;&amp; i &lt; S; ++j) bits[i++] = (byte &gt;&gt; j) &amp; 1; return is; } } int main() { constexpr size_t bytes = 2; std::string bit_string("\x00\xFF", bytes); std::istringstream bit_stream(bit_string); std::bitset&lt;8 * bytes&gt; b; { using namespace my; bit_stream &gt;&gt; b; } std::cout &lt;&lt; b &lt;&lt; std::endl; for(size_t i = 0; i &lt; b.size(); ++i) std::cout &lt;&lt; b[i]; std::cout &lt;&lt; std::endl; } </code></pre> <h3>output:</h3> <pre><code>1111111100000000 0000000011111111 </code></pre> <h3>run live:</h3> <p><a href="http://ideone.com/Is7xRy" rel="nofollow">http://ideone.com/Is7xRy</a></p> <h3>notes:</h3> <ul> <li><code>bitset</code> size is a non-type template parameter. You can't set it at runtime. So, this will only work with preallocation of the bitset and you'll get 0 padding for a larger bitset.</li> <li>If you want a runtime sized bitset, check out <a href="http://www.boost.org/doc/libs/1_53_0/libs/dynamic_bitset/dynamic_bitset.html" rel="nofollow">boost::dynamic_bitset</a>.</li> <li>The sample is expected to work for files just by replacing <code>istringstream</code> by <code>ifstream</code> appropriately .</li> </ul>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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