High level solution:
include math.h and the like and just use log() function.
Low level solution:
Actually the binary numbers are "logarithmic" by nature: Log2(256) = 8, Log2(128) = 7 etc. Each bit position add 6 dB to the total so you can just loop trough the bit positions to find the integer part or "characteristic", then use a table lookup for the fraction or "mantissa":
http://en.wikipedia.org/wiki/Common_logarithm
Algorithm can be like this, using 8 bit table lookup for fraction part:
**Code:**
first_set_bit = 31; // MSB of unsigned int
if (input==0) return; // Log(0) error
while (input&(1<<first_set_bit)==0) first_set_bit--;
lookup_address = // the 8 bits following first_set_bit downwards
fraction_part = lookup(lookup_address);
integer_part = first_set_bit;
Of course the result must be scaled to fit the application, ie it is not scaled to mathematical log10(). |