fork(4) 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. static List<Integer> bits(int num) {
  11. List<Integer> setBits = new ArrayList<>();
  12. for (int i = 1; num != 0; ++i, num >>>= 1) {
  13. if ((num & 1) != 0) setBits.add(i);
  14. }
  15. return setBits;
  16. }
  17.  
  18. public static void main (String[] args) throws java.lang.Exception
  19. {
  20. System.out.println(6 + " " + bits(6));
  21. System.out.println(7 + " " + bits(7));
  22. System.out.println(8 + " " + bits(8));
  23. System.out.println(-1 + " " + bits(-1));
  24. }
  25. }
Success #stdin #stdout 0.1s 35848KB
stdin
Standard input is empty
stdout
6 [2, 3]
7 [1, 2, 3]
8 [4]
-1 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]