forked from mbobesic/algorithms-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversebinary.java
More file actions
34 lines (23 loc) · 724 Bytes
/
Reversebinary.java
File metadata and controls
34 lines (23 loc) · 724 Bytes
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
33
34
// link: https://open.kattis.com/problems/reversebinary
// name: Reverse Binary
package kattis;
import java.util.Scanner;
/**
* Created by mislav on 11/17/14.
*/
public class Reversebinary {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int originalNumber = input.nextInt();
int numBitLength = 32 - Integer.numberOfLeadingZeros(originalNumber);
int result = 0;
for (int i = 0; i < numBitLength; i++) {
result = result << 1;
int bit = originalNumber & 1;
result += bit;
originalNumber = originalNumber >> 1;
}
System.out.println(result);
input.close();
}
}