Skip to content

Instantly share code, notes, and snippets.

@nithesh1992
Created August 21, 2016 23:30
Show Gist options
  • Save nithesh1992/0d6e7d61d0ecd51e6a2ca667da654547 to your computer and use it in GitHub Desktop.
Save nithesh1992/0d6e7d61d0ecd51e6a2ca667da654547 to your computer and use it in GitHub Desktop.
Decimal Compliment (Binary)
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