Packing values into a single int
<p>Let's say I have a couple of variables like apple, orange, banana</p>

<p>I have 8 apples, 1 orange, 4 bananas.</p>

<p>Is it possible to somehow convert those values into a single integer and also revert back to their original values based on the computed integer value?</p>

<p>I found an example online.</p>

<pre><code>int   age, gender, height;
short packed_info;
. . .
// packing
packed_info = (((age &lt;&lt; 1) | gender) &lt;&lt; 7) | height;
. . .
// unpacking
height = packed_info &amp; 0x7f;
gender = (packed_info &gt;&gt;&gt; 7) &amp; 1;
age    = (packed_info &gt;&gt;&gt; 8);
</code></pre>

<p>But it doesn't seem to work as it should when I entered random numbers.</p>
