This might be something obvious but i'am missing it. Why do i need to perform AND with 0xff to obtain ip address? The way i see it is exactly the same thing, perform AND with 0xff should leave the bits the same, so why doesn't it work if i don't do the AND operation?
package com.inet.ex1;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ShowIP {
public static void main(String[] args) {
InetAddress host;
try {
host = InetAddress.getLocalHost();
byte[] ip = host.getAddress();
for (int i = 0; i < ip.length; i++) {
if (i == 0) System.out.print(ip[i] & 0xff);
else System.out.print("." + (ip[i] & 0xff));
}
} catch (UnknownHostException e) {
System.out.println(e);
}
}
}