Here's my thought of how your method compareBits should look like:
public static String compareBits(String oct, String oct2) {
String comparison = "";
for(int i=0; i<=oct.length()-1; i++) {
if(oct.charAt(i) == oct2.charAt(i) && oct.charAt(i) != '0') {
comparison = comparison+"1";
} else {
comparison = comparison+"0";
}
}
return comparison;
}
Since you are checking the charAt(), you get a character, but you compare with 0, and not with the char literal '0'. That should do the trick.
On the other hand, I think this task could be archieved with less code, for instance:
long ipL = IpConverter.ipToLong(ip);
long maskL = IpConverter.ipToLong(mask);
System.out.println(IpConverter.longToIp(ipL & maskL));
Regards