log2 function of Python math module

The log2 function of the Python math module computes and returns the binary logarithm of a given number.

Math Updated 21 Jun 2022 1 min read

Overview

  • The function log2(num), computes the binary logarithm of a given number. The Base-2 logarithm of a given number “n” is the power to which, number 2 must be raised to obtain the number “n”.

Example

python
# Example Python program that computes

            # the base-2 logarithm of a number

            # using the function log2(x)

import math

# Compute binary logrithm for 10

            num = 10

            base2log = math.log2(num)

            print("Base-2 logarithm of %d:%d"%(num, base2log))

# Compute binary logrithm for 128

            num = 128

            base2log = math.log2(num)

            print("Base-2 logarithm of %d:%d"%(num, base2log))

Output

Output
Base-2 logarithm of 10:3

Base-2 logarithm of 128:7