Advertisement
knyazer

Untitled

Apr 21st, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6. public static boolean haveBit(int x, int n) {
  7. if (n != 0) return (x & (int) (2 << (n - 1))) != 0;
  8. return x % 2 != 0;
  9. }
  10.  
  11. public static int log2(int a) {
  12. return 1 + (int) (Math.log(a) / Math.log(2));
  13. }
  14.  
  15. public static void main(String[] args) {
  16. Scanner in = new Scanner(System.in);
  17.  
  18. int a = in.nextInt();
  19.  
  20. int l = log2(a);
  21.  
  22. int b = 0;
  23.  
  24. for (int i = 0; i < l; i++)
  25. {
  26. if (haveBit(a, l - i - 1)) {
  27. if (i == 0) b += 1;
  28. else b += 2 << (i - 1);
  29. }
  30. }
  31.  
  32. System.out.println(b);
  33.  
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement