Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble figuring out why my procedure to print out bits of each integer in an array isn't working
    primarykey
    data
    text
    <p>I'm trying to make a class that optimizes an array of booleans by storing an ordered array of integers whose bits individual bits can be accessed to see if they're on or off. Yes, I know that </p> <pre><code>std::vector&lt;bool&gt; </code></pre> <p>does something like this, but I'm trying to make my own implementation, for practice. </p> <p>For instance, an unsigned short int is 16 bits, so I can pack 100 bools into an array of of 7 unsigned short ints where the last element acts as padding.</p> <p>As I test this, I see that something is already going wrong, possibly something with my print() function. In main you can see that I made a BitPack object of 32 bools, which will be stored in an array of 2 unsigned short ints, and I've verified that this is so. However, my print function isn't working because it gives me the following output, which isn't 32 zeros, as it should be.</p> <pre><code>0000000000000000 </code></pre> <p>I've looked over my print function numerous times and can't figure out what is wrong. Sorry about any indentations that got lost when I copied my code here. Any help greatly appreciated.</p> <pre><code>#include &lt;iostream&gt; #include &lt;limits.h&gt; #include &lt;assert.h&gt; typedef unsigned short int usi; class BitPack { public: BitPack(int); ~BitPack(); bool getVal(int); int getSize(); void setVal(int, bool); void print(); private: const static int USI_BITS = sizeof(usi)*CHAR_BIT; usi* _booArr; int _booArrLen; int _numBoos; }; BitPack::BitPack(int sz) { assert (sz &gt; 0); _numBoos = sz; _booArrLen = _numBoos/USI_BITS+(_numBoos % USI_BITS ? 1 : 0); _booArr = new usi[_booArrLen]; for (int i = 0; i &lt; _booArrLen; ++i) _booArr[i] = 0; } BitPack::~BitPack() { delete[] _booArr; } bool BitPack::getVal(int indx) { assert (indx &gt; 0); usi bA_indx_val = _booArr[indx/USI_BITS]; bA_indx_val &gt;&gt;= (bA_indx_val % USI_BITS); return (bA_indx_val % 2 ? true : false); } int BitPack::getSize() { return (_numBoos); } void BitPack::setVal(int indx, bool valset) { assert (indx &gt; 0); bool curval = getVal(indx); if ((curval == true) &amp;&amp; (valset == false)) { _booArr[indx/USI_BITS] += (1 &lt;&lt; (indx % USI_BITS)); } else if ((curval == true) &amp;&amp; (valset == false)) { _booArr[indx/USI_BITS] -= (1 &lt;&lt; (indx % USI_BITS)); } } void BitPack::print() { int i = 0; usi thisval; while (i &lt; _booArrLen - 1) { thisval = _booArr[i]; for (int j = 0; j &lt; USI_BITS; ++j) { std::cout &lt;&lt; (thisval % 2 ? '1' : '0'); thisval &gt;&gt;= 1; } i++; } thisval = _booArr[i]; for (int j = 0; j &lt; _numBoos % USI_BITS; ++j) { std::cout &lt;&lt; (thisval % 2 ? '1' : '0'); thisval &gt;&gt;= 1; } } int main (int argc, char* const argv[]) { BitPack bp(32); bp.print(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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