Skip to content

Instantly share code, notes, and snippets.

@six519
Created July 9, 2013 15:53
Show Gist options
  • Save six519/5958511 to your computer and use it in GitHub Desktop.
Save six519/5958511 to your computer and use it in GitHub Desktop.
Reverse Binary (Spotify Puzzle in Java)
import java.util.*;
import java.lang.*;
class ReversedBinaryToDecimal {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int decimalNumber;
try {
decimalNumber = scanner.nextInt();
if(decimalNumber >= 1 && decimalNumber <= 1000000000) {
System.out.println(Integer.parseInt(new StringBuilder(Integer.toBinaryString(decimalNumber)).reverse().toString(),2));
} else {
throw new InputMismatchException();
}
}catch(InputMismatchException e) {
System.out.println("Invalid input");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment