This function basically asks "how many 2's do you need to multiply to get n?". A common example of use in programming would be to calculate the number of bits that are needed to represent a number. To do this we calculate the

log2(x)of the number, round it down and add 1 - for examplelog2(100)returns 6.643856, which rounded down is 6, to which we add one and it gives us 7. So we need 7 bits to represent 100. It can also be used for describing exponential growth or decay, and is often called abinary logarithm.

log2(n);

参数 | 描述 |
---|---|

n | The input value. |

实数

colourbits = floor(log2(colour)) - 1;

The above code stores the number of bits needed to represent the value stored in the variable "colour".