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 Integer[] Test(Integer n)
  11. {
  12. //int n = 24; // Argument funkcji
  13. int nlen = 0; // Liczba bitów w binarnej reprezentacji n
  14. List<Integer> pos = new LinkedList<>();
  15. for (int i = 1; n >> i != 0; i++)
  16. nlen = i + 1;
  17. for (int i = 0; i < nlen; i++)
  18. if ((n & (1 << i)) != 0) // "1 << i" to kolejno 1, 2, 4, 8...
  19. pos.add(i);
  20. return pos.toArray(new Integer[pos.size()]);
  21. }
  22.  
  23. public static void main (String[] args) throws java.lang.Exception
  24. {
  25. System.out.println((Arrays.toString(Test(24))));
  26. }
  27. }
Success #stdin #stdout 0.06s 380224KB
stdin
Standard input is empty
stdout
[3, 4]