Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Exercises: Returns the largest integer but not larger than the base-2 logarithm of a specified integer

Java Basic: Exercise-156 with Solution

Write a Java program that returns the largest integer but not larger than the base-2 logarithm of a given integer.

Original Number: 2350
Result: 11

Sample Solution:

Java Code:

import java.util.Scanner;
public class Solution {
	     
    public static void main(String[] args) {
        int n = 2350;
		System.out.printf("Original Number: %d\n", n);
		int shift_right_count = 0;
        do {
            n >>= 1;
            shift_right_count++;
        } while (n != 0);
         shift_right_count--;
        System.out.printf("Result: %s\r\n", shift_right_count);
    }
}

Sample Output:

Original Number: 2350
Result: 11

Flowchart:

Flowchart: Java exercises: Returns the largest integer but not larger than the base-2 logarithm of a specified integer.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to print an array after changing the rows and columns of a given two-dimensional array.
Next: Write a Java program to prove that Euclid’s algorithm computes the greatest common divisor of two positive given integers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe