Created
August 21, 2016 23:30
-
-
Save nithesh1992/0d6e7d61d0ecd51e6a2ca667da654547 to your computer and use it in GitHub Desktop.
Decimal Compliment (Binary)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
/** | |
* Created by Nithesh on 8/9/2016. | |
* Get an Integer Compliment. | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
ArrayList<Integer> binList = new ArrayList<>(); | |
int result = 0; | |
int n = sc.nextInt(); | |
String unique = Integer.toBinaryString(n); | |
for(int i=0; i < unique.length(); i++) | |
{ | |
if(unique.charAt(i) == '0') | |
{ | |
binList.add(1); | |
} | |
else | |
{ | |
binList.add(0); | |
} | |
} | |
int fresult= Integer.parseInt(binList.toString().replaceAll("[^01]", ""), 2); | |
System.out.println(unique); | |
System.out.println(fresult); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment