title: Storing multiple int values into one variable - C++
body: <p>I am doing an algorithmic contest, and I'm trying to  optimize my code. Maybe what I want to do is stupid and impossible but I was wondering.</p>
  <p>I have these requirements:</p>
  <ul>
  <li>An inventory which can contains 4 distinct types of item. This inventory can't contain more than 10 items (all type included). Example of valid inventory: 1 / 1 / 1 / 0. Example of invalid inventories: 11 / 0 / 0 / 0 or 5 / 5 / 5 / 0</li>
  <li>I have some recipe which consumes or adds items into my inventory. The recipe can't add or consume more than 10 items since the inventory can't have more than 10 items. Example of valid recipe: -1 / -2 / 3 /
  0. Example of invalid recipe: -6 / -6 / +12 / 0</li>
  </ul>
  <p>For now, I store the inventory and the recipe into 4 integers. Then I am able to perform some operations like:</p>
  <ul>
  <li>ApplyRecepe: Inventory(1/1/1/0).Apply(Recepe(-1/1/0/0)) = Inventory(0/2/1/0)</li>
  <li>CanAfford: Iventory(1/1/0/0).CanAfford(Recepe(-2/1/0/0)) = False</li>
  </ul>
  <p>I would like to know if it is possible (and if yes, how) to store the 4 values of an inventory/recipe into one single integer and to performs previous operations on it that would be faster than comparing / adding the 4 integers as I'm doing now.</p>
  <p>I thought of something like having the inventory like that:</p>
  <p>int32: XXXX (number of items of the first type) - YYYY (number of items of the second type) - ZZZ (number of items of the third type)  - WWW (number of item of the fourth type)</p>
  <p>But I have two problems with that:</p>
  <ol>
  <li>I don't know how to handle the possible negative values</li>
  <li>It seems to me much slower than just adding the 4 integers since I have to bit shift the inventory and the recipe to get the value I want and then proceed with the addition.</li>
  </ol>