user61029: Hey I wrote this function to convert an bool array to int:
(Math Lob included):
bool b_array[5];

int convertToInt(start_index,stop_index){
    int run=0;
    int result=0;
    for(int id=start_index;id<=stop_index;id++){
        result+=b_array[id] * pow(2.0,run);
        run++;
    }
    return result;
} 

but it doesnt this work?
Thanks and kind regards 
Nic

user4143: Assuming little-endian bools (least significant bool first), you should use simple bit-shifting, not floating point power calculations:
for(int id=start_index;id<=stop_index;id++){
    result |= b_array[id] ? (1 << run) : 0;
    run++;
}

The ternary operation (b_array[id] ? (1 << run) : 0) means:

If b_array[id] is true,  then


Or result with 1 left-shifted run times and store in result

otherwise


Or result with 0 and store the result (no-operation).



61029: <p>Hey I wrote this function to convert an bool array to int:
(Math Lob included):</p>

<pre><code>bool b_array[5];

int convertToInt(start_index,stop_index){
    int run=0;
    int result=0;
    for(int id=start_index;id&lt;=stop_index;id++){
        result+=b_array[id] * pow(2.0,run);
        run++;
    }
    return result;
} 
</code></pre>

<p>but it doesnt this work?</p>

<p>Thanks and kind regards 
Nic</p>
 4143: <p>Assuming little-endian bools (least significant bool first), you should use simple bit-shifting, not floating point power calculations:</p>

<pre><code>for(int id=start_index;id&lt;=stop_index;id++){
    result |= b_array[id] ? (1 &lt;&lt; run) : 0;
    run++;
}
</code></pre>

<p>The <em>ternary</em> operation (<code>b_array[id] ? (1 &lt;&lt; run) : 0</code>) means:</p>

<ul>
<li>If <code>b_array[id]</code> is true,  then

<ul>
<li>Or <code>result</code> with 1 left-shifted <code>run</code> times and store in <code>result</code></li>
</ul></li>
<li>otherwise

<ul>
<li>Or <code>result</code> with 0 and store the result (no-operation).</li>
</ul></li>
</ul>
