Wednesday, April 2, 2008

A quick formula for extracting numbers.

We know how to extract booleans out of larger integers, but what if our base is larger than one. What if we want to store numbers like example a 3. We know that if we whant to store a number of any base we can just go and take for example, if we have 3,2,1 in base 4(numbers from 0 to 3) and we want to store them in a single decimal integer we can do (3)(4^0)+(2)(4^1)+(1)(4^2),(in other place number*(base^position)) but what if after we get a number 27 we want to extract them individually. well the formula is rather easy it is floor(number/pow(base,position))%base so if we take 27 and we want the numper in position 1 (that we know its 2) we do this:
floor(27/pow(4,1)%4
floor(27/4)%4
floor(6.75)%4
6%4
2.
This way we can store things that hold more than just yes or no questions. Using the other formulas to store items we can create highly portable data by compressing all possibilities in numbers, Converting to larger bases for example hexadecimal or one that uses all the letters of the alphabet makes it more portable.

No comments: