fork(18) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. private static int smallerPowerOf2(int v) {
  7. v--;
  8. v |= v >> 1;
  9. v |= v >> 2;
  10. v |= v >> 4;
  11. v |= v >> 8;
  12. v |= v >> 16;
  13. v++;
  14. return v >> 1;
  15. }
  16. public static void main (String[] args) throws java.lang.Exception
  17. {
  18. int[] data = new int[] {9, 100, 1000, 64};
  19. for (int n : data) {
  20. System.out.println(n + " - " + smallerPowerOf2(n));
  21. }
  22. }
  23. }
Success #stdin #stdout 0.06s 380160KB
stdin
Standard input is empty
stdout
9 - 8
100 - 64
1000 - 512
64 - 32