Complement number
For given integer number change it into binary representation and return integer, which represents binary complement number!
private static int getIntegerComplement(int n) { String binaryBase = Integer.toBinaryString(n); System.out.println("Binary = " + binaryBase); //reversing binaryBase = binaryBase.replaceAll("0", "2"); binaryBase = binaryBase.replaceAll("1", "0"); binaryBase = binaryBase.replaceAll("2", "1"); System.out.println("Reversed: " + binaryBase); //building int from binary int reversed = Integer.parseInt(binaryBase, 2); System.out.println("After reversing = " + reversed); return reversed; }