log2

Returns the log base 2 of n, the number 2 must be powered by to equal n.

Syntax:

GML Code Example

log2(n);


Argument Description
n The input value.


Returns: Real


Description

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 example log2(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 a binary logarithm.


Example:

GML Code Example

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

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





No Examples Submitted

Does this page need better examples? Be the first to Submit

No Edits Suggested

Is this page unclear or not descriptive enough? Suggest an edit to the page and with enough upvotes your changes will be made.

No Questions Have Been Ask

Do you have a question about this page? Ask it Here