Note that there are some explanatory texts on larger screens.

plurals
  1. POGet Integer From Bits Inside `std::vector<char>`
    primarykey
    data
    text
    <p>I have a <code>vector&lt;char&gt;</code> and I want to be able to get an unsigned integer from a range of bits within the vector. E.g.</p> <p><img src="https://i.stack.imgur.com/q7N7u.png" alt="visualisation of bitvalues"></p> <p>And I can't seem to be able to write the correct operations to get the desired output. My intended algorithm goes like this:</p> <ul> <li><code>&amp;</code> the first byte with <code>(0xff &gt;&gt; unused bits in byte on the left)</code></li> <li><code>&lt;&lt;</code> the result left the number of output bytes * number of bits in a byte</li> <li><code>|</code> this with the final output</li> <li>For each subsequent byte: <ul> <li><code>&lt;&lt;</code> left by the (byte width - index) * bits per byte</li> <li><code>|</code> this byte with the final output</li> </ul></li> <li><code>|</code> the final byte (not shifted) with the final output</li> <li><code>&gt;&gt;</code> the final output by the number of unused bits in the byte on the right</li> </ul> <p>And here is my attempt at coding it, which does not give the correct result:</p> <pre><code>#include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;cstdint&gt; #include &lt;bitset&gt; template&lt;class byte_type = char&gt; class BitValues { private: std::vector&lt;byte_type&gt; bytes; public: static const auto bits_per_byte = 8; BitValues(std::vector&lt;byte_type&gt; bytes) : bytes(bytes) { } template&lt;class return_type&gt; return_type get_bits(int start, int end) { auto byte_start = (start - (start % bits_per_byte)) / bits_per_byte; auto byte_end = (end - (end % bits_per_byte)) / bits_per_byte; auto byte_width = byte_end - byte_start; return_type value = 0; unsigned char first = bytes[byte_start]; first &amp;= (0xff &gt;&gt; start % 8); return_type first_wide = first; first_wide &lt;&lt;= byte_width; value |= first_wide; for(auto byte_i = byte_start + 1; byte_i &lt;= byte_end; byte_i++) { auto byte_offset = (byte_width - byte_i) * bits_per_byte; unsigned char next_thin = bytes[byte_i]; return_type next_byte = next_thin; next_byte &lt;&lt;= byte_offset; value |= next_byte; } value &gt;&gt;= (((byte_end + 1) * bits_per_byte) - end) % bits_per_byte; return value; } }; int main() { BitValues&lt;char&gt; bits(std::vector&lt;char&gt;({'\x78', '\xDA', '\x05', '\x5F', '\x8A', '\xF1', '\x0F', '\xA0'})); std::cout &lt;&lt; bits.get_bits&lt;unsigned&gt;(15, 29) &lt;&lt; "\n"; return 0; } </code></pre> <p>(In action: <a href="http://coliru.stacked-crooked.com/a/261d32875fcf2dc0" rel="nofollow noreferrer">http://coliru.stacked-crooked.com/a/261d32875fcf2dc0</a>)</p> <p>I just can't seem to wrap my head around these bit manipulations, and I find debugging very difficult! If anyone can correct the above code, or help me in any way, it would be much appreciated! </p> <p>Edit:</p> <ul> <li>My bytes are 8 bits long</li> <li>The integer to return could be 8,16,32 or 64 bits wside</li> <li>The integer is stored in big endian</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.
 

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