# Any way to somehow reverse (-x)-1

Here’s a programming challenge I have been baffled on:

public class CrackTheCode {

public static void main(String[] args) {
// Test the masher
String testString = "One Team, One Goal, One Web.";
if (Masher.unmash(Masher.mash(testString)).equals(testString)) {
System.out.println("OK");
} else {
System.out.println("Error");
}
}

static class Masher {

static String mash(String s) {
byte[] bytes = s.getBytes();
byte[] mashed = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
mashed[i] = (byte) ~bytes[i];
}
return new String(mashed);
}

static String unmash(String s) {
//TODO
return null;
}
}

}

Take another 1’s complement?

-((-x) - 1) - 1 = x + 1 - 1 = x

Or did I not understand your question?

static class Masher {

static  String mash(String s) {
byte[] bytes = s.getBytes();
byte[] mashed = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
mashed[i] = (byte) ~bytes[i];
}
char[] chars=new char[mashed.length];
for (int i = 0; i < mashed.length; i++) {
chars[i] = (char)mashed[i];
}
return new String(chars);
}

static String unmash(String s) {
char[] chars = s.toCharArray();
byte[] unmashed=new byte[chars.length];
for (int i = 0; i < chars.length; i++) {
unmashed[i] =(byte)~(byte)(chars[i]);
}
return new String(unmashed);
}

}

the string typecasting was messing up with negative values in bytes…however worked fine this way