Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static boolean haveBit(int x, int n) {
- if (n != 0) return (x & (int) (2 << (n - 1))) != 0;
- return x % 2 != 0;
- }
- public static int log2(int a) {
- return 1 + (int) (Math.log(a) / Math.log(2));
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int a = in.nextInt();
- int l = log2(a);
- int b = 0;
- for (int i = 0; i < l; i++)
- {
- if (haveBit(a, l - i - 1)) {
- if (i == 0) b += 1;
- else b += 2 << (i - 1);
- }
- }
- System.out.println(b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement