Binary logarithm without java.lang.Math? -
i have 1 question, how posible perform binary logarithm without using math. have math.log(x)/math.log(2) , works can't use java.lang.math.
what can do?
int bits_necessaris = (int)(log2(nat+1)); //this correct //the function hhave troubles, don't know :( public static int log2(int x) { return (something); } important: when performed math.log(7)/math.log(2) got 2.80xxxxx
so did :
(int) math.ceil(math.log(7)/math.log(2)); and 2.80xxxxx rounded got 3
the return of function has rounded up, example if solution 6,777 return has 7
you want know how many bits necessary represent int? then, there easier solutions, such as:
int bitsneededfor(int i) { int bits = 0; while (i > 0) { bits++; /= 2; } return bits; } on second thought, following faster , easier understand:
int bisneededfor(int i) { return 32 - integer.numberofleadingzeros(i); }
Comments
Post a Comment