fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. System.out.println(getRequiredNumberOfBits(2)) ;
  14. }
  15.  
  16. public static int getRequiredNumberOfBits(int N) {
  17. /*
  18.   Please implement this method to
  19.   return the number of bits which is just enough to store any integer from 0 to N-1 inclusively
  20.   for any int N > 0
  21.   Example: to store 5 integers from 0 to 4 you need 3 bits: 000, 001, 010, 011, 100 and 101
  22.   */
  23.  
  24. int number = 2 ;
  25. int count = 1 ;
  26. while(N > number){
  27. number = number*2 ;
  28. count++ ;
  29.  
  30. }
  31.  
  32. return count ;
  33.  
  34. }
  35.  
  36. }
Success #stdin #stdout 0.09s 46932KB
stdin
10
stdout
1