Calculate Log2n without using built-in function

Objective: Given a number n, write a program to calculate Log2n without using built-in function.


N = 32
Log232 = 5

N = 64
Log264 = 6


  • Initialize result = 0.
  • Keep dividing the given number by 2 till number is greater than 0 and add one to the result if n is greater than equal to 1.

Java Code:


Log64 value: 6

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.