 <p>If you <em>must</em> use a loop, you can do something like</p> <pre><code>uint64_t text = 0; for (int i = 15; i &gt;= 0; --i) { text &lt;&lt;= 4; text |= a[i] &amp; 0x0f; // Masking in case a[i] have more than the lowest four bits set } </code></pre>