Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "math/base/special/fast/uint32-log2/docs/types/index.d"

Index

Functions

Functions

Export assignment log2

  • log2(x: number): number
  • Computes an integer binary logarithm (base two).

    Method

    1. Note that the largest unsigned 32-bit integer is 4294967295, which is 2^{32}-1. Hence, the integer binary logarithm cannot exceed 31 (i.e., 16 + 8 + 4 + 2 + 1), which corresponds to the bit sequence

      00000000000000000000000000011111
    2. Initialize a return variable with the value zero.

    3. If at least one of the first sixteen most significant bits of the input 32-bit integer x is turned on, we know that the power to which the number 2 must be raised to obtain x is at least 16 (i.e., x > 65536). Hence, activate the corresponding bit of the return variable. Mutate x by shifting sixteen bits to the right, discarding the bits shifted off.

    4. Carry out the following steps with B in [ 8, 4, 2, 1 ]:

      • If at least one of the next B most significant bits of the current x is turned on, we know that the power to which the number 2 must be raised to obtain x has to be increased by B.
      • Activate the bit of the return variable that corresponds to B.
      • Mutate x by shifting B bits to the right, discarding the bits shifted off.
    5. The final value of the return variable is the integer binary logarithm of x.

    Parameters

    • x: number

      unsigned 32-bit integer

    Returns number

    integer binary logarithm

    Example

    var v = log2( 4 >>> 0 );
    // returns 2

    Example

    var v = log2( 8 >>> 0 );
    // returns 3

    Example

    var v = log2( 9 >>> 0 );
    // returns 3