Skip to content

Instantly share code, notes, and snippets.

@Keda87
Created June 13, 2014 07:03
Show Gist options
  • Save Keda87/dd32230d8e17725b058a to your computer and use it in GitHub Desktop.
Save Keda87/dd32230d8e17725b058a to your computer and use it in GitHub Desktop.
Veritrans apply CV test
package other.veritrans;
import java.util.Scanner;
import java.util.logging.Logger;
/**
* @author : Adiyat Mubarak
* @email : adiyatmubarak@gmail.com
* @blog : www.keda87.wordpress.com
*/
public class BinaryComplement {
private static final Logger LOG = Logger.getLogger(BinaryComplement.class.getName());
private Integer getIntegerComplement(Integer n) {
String binN = Integer.toBinaryString(n);
LOG.info(binN);
String complement = "";
for (int i = 0; i < binN.length(); i++) {
complement += binN.charAt(i) == '1' ? '0' : '1';
}
LOG.info(complement);
int[] pos = {128, 64, 32, 16, 8, 4, 2, 1};
int hasil = 0;
int tmpN = complement.length() - 1;
int tmpPos = pos.length - 1;
for (int i = 0; i < complement.length(); i++) {
if (complement.charAt(tmpN) == '1') {
hasil += pos[tmpPos];
}
tmpN--;
tmpPos--;
}
return hasil;
}
public static void main(String[] args) {
BinaryComplement veritrans = new BinaryComplement();
Scanner sc = new Scanner(System.in);
System.out.print("input : ");
int hasil = veritrans.getIntegerComplement(sc.nextInt());
System.out.println("output : " + hasil);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment