I have to find the twos Complement of a decimal number. The input is in Byte and the result should be in String. I have already written a program that converts decimal to binary but i don't know how to convert binary String to ones Complement aka replacing 0 with 1 and 1 with 0.
This is what I have written so far:
String toTwoComp(byte n) {
String s = "";
byte num = n;
String res = "";
while (n > -128 && n < 127) {
s = (num % 2) + s;
num = (byte) (num / 2);
if (num == 0) {
break;
}
}
res = "00000000".substring(s.length()) + s;
return res;
}