8-bit values may be concatenated to get larger numbers or truncated to get

smaller numbers.

For example, if you have 0-255 this is 0 to 0xFF or 00000000 to 11111111 in

binary.

If you want the 0-31, you can simply AND with 0x1F to get the required

range:

in C this is

outValue = inValue & 0x1F;

If you want a larger power of 2 then you just concatenate to the required

number of bits and AND with the number of bits you want to have,

For 4194303 (2^22-1) in C this is:

outValue = ((inValue1 << 16) | (inValue2 <<8) | inValue3) & 0x3FFFFF;

The simplest way to get another integer range is to concatenate the number

of bits required to get a higher number that you want then discard all

values above it (this is ok, since every value is equally probable).

For 2456743 in C this can be done like this:

tempValue = ((inValue1 << 16) | (inValue2 <<8) | inValue3) & 0x3FFFFF;

/* Gives a value between 0 and 4194303 */

if(tempValue <= 2456743) /* only use if <= the required range */

{

outValue=tempValue;

}

There are more efficient methods of doing this but are dependent on the

application (Google will help you with this).