I'm looking for an efficient-to-unpack (in terms of small number of basic ALU ops in the generated code) way of encoding 3 base-6 digits (i.e. 3 numbers in the range [0,5]) in 8 bits. Only one is needed at a time, so approaches that need to decode all three in order to access one are probably not good unless the cost of decoding all three is very low.

The obvious method is of course:

```
x = b%6;   //  8 insns
y = b/6%6; // 13 insns
z = b/36;  //  5 insns

```

The instruction counts are measured on x86\_64 with gcc>=4.8 which knows how to avoid divs.

Another method (using a different encoding) is:

```
b *= 6
x = b>>8;
b &= 255;

b *= 6
y = b>>8;
b &= 255;

b *= 6
z = b>>8;

```

This encoding has more than one representation for many tuples (it uses the whole 8bit range rather than just [0,215]) and appears more efficient if you want all 3 outputs, but wasteful if you only want one.

Are there better approaches?

Target language is C but I've tagged this `assembly` as well since answering requires some consideration of the instructions that would be generated.