randRange( 0, 32) getLargestPower2( Dec, 32 )

Express the decimal number Dec in binary notation (with no leading zeroes).

Dec.toString(2)

Find the largest power of 2 that will go into Dec and put a '1' in its place. (Binary place values: 32 16 8 4 2 1)

The largest power of 2 that goes into Dec is Divisor so put a 1 in the Divisors place.

Subtract Divisor from Dec giving Dec - Divisor. Now find the largest power of 2 that goes into Dec-Divisor and put a 1 in its place.

Continue dividing and substracting until you get to the ones place (2 to the 0 power).

Add up all the powers of 2 that have a 1 in their place.

The answer is Dec.toString(2)