Tuesday, 15 May 2012

int - Fail to Understand Java Two's Complement -



int - Fail to Understand Java Two's Complement -

i have code:

package com.company; import java.net.inetaddress; import java.net.unknownhostexception; public class main { private final static int broadcast = 0xffffffff; //4294967295, or 255.255.255.255 private final static int firstclasse = 0xf0000000; //4026531840, or 240.0.0.0 public static int getintinetaddress(inetaddress toconvert) { final byte[] addr = toconvert.getaddress(); final int ipaddr = ((addr[0] & 0xff) << (3 * 8)) + ((addr[1] & 0xff) << (2 * 8)) + ((addr[2] & 0xff) << (1 * 8)) + (addr[3] & 0xff); homecoming ipaddr; } public static boolean isclasseaddress(inetaddress address) { int curaddr = getintinetaddress(address); boolean test1 = curaddr >= firstclasse; boolean test2 = curaddr < broadcast; system.out.println(string.format("\ncuraddr: %s, firstclasse: 240.0.0.0, broadcast: 255.255.255.255", address.gethostaddress())); system.out.println(string.format("curaddr: %d, firstclasse: %d, broadcast: %d, curaddr >= firstclasse: %s, curaddr < broadcast: %s", curaddr, firstclasse, broadcast, test1 ? "true" : "false", test2 ? "true" : "false")); homecoming (test1 && test2) ? true : false; } public static void main(string[] args) throws unknownhostexception { if (isclasseaddress(inetaddress.getbyname("1.0.0.0"))) { // raise flag system.out.println("class e ip address detected."); } if (isclasseaddress(inetaddress.getbyname("250.0.0.0"))) { // raise flag system.out.println("class e ip address detected."); } if (isclasseaddress(inetaddress.getbyname("239.255.255.255"))) { // raise flag system.out.println("class e ip address detected."); } if (isclasseaddress(inetaddress.getbyname("240.0.0.0"))) { // raise flag system.out.println("class e ip address detected."); } if (isclasseaddress(inetaddress.getbyname("240.0.0.1"))) { // raise flag system.out.println("class e ip address detected."); } if (isclasseaddress(inetaddress.getbyname("255.255.255.255"))) { // raise flag system.out.println("class e ip address detected."); } } }

which produces next output:

curaddr: 1.0.0.0, firstclasse: 240.0.0.0, broadcast: 255.255.255.255 curaddr: 16777216, firstclasse: -268435456, broadcast: -1, curaddr >= firstclasse: true, curaddr < broadcast: false curaddr: 250.0.0.0, firstclasse: 240.0.0.0, broadcast: 255.255.255.255 curaddr: -100663296, firstclasse: -268435456, broadcast: -1, curaddr >= firstclasse: true, curaddr < broadcast: true class e ip address detected. curaddr: 239.255.255.255, firstclasse: 240.0.0.0, broadcast: 255.255.255.255 curaddr: -268435457, firstclasse: -268435456, broadcast: -1, curaddr >= firstclasse: false, curaddr < broadcast: true curaddr: 240.0.0.0, firstclasse: 240.0.0.0, broadcast: 255.255.255.255 curaddr: -268435456, firstclasse: -268435456, broadcast: -1, curaddr >= firstclasse: true, curaddr < broadcast: true class e ip address detected. curaddr: 240.0.0.1, firstclasse: 240.0.0.0, broadcast: 255.255.255.255 curaddr: -268435455, firstclasse: -268435456, broadcast: -1, curaddr >= firstclasse: true, curaddr < broadcast: true class e ip address detected. curaddr: 255.255.255.255, firstclasse: 240.0.0.0, broadcast: 255.255.255.255 curaddr: -1, firstclasse: -268435456, broadcast: -1, curaddr >= firstclasse: true, curaddr < broadcast: false

what not understanding why numbers , comparisons not expect them be, yet code produces results want. gather it's whole two's complement thing, don't "get" reason. mechanically, know (two's complement) flipping bits , adding 1 don't is, why comparisons working if of numbers inverted?

for example, in first check ip 1.0.0.0, int value 16777216 checked see if it's smaller int value 255.255.255.255, -1. result false, broadcast ip, when converted int, larger, not smaller, ip 1.0.0.0. likewise, check 1.0.0.0 beingness @ least, or higher than, 240.0.0.0 returns true, when know isn't case.

i've checked boundary cases , working... don't understand why (and wrote code, go figure!). if there more clear method of determining if ip fals within range, explore way must not making sense, despite working (or it?)

in intellij, there's unusual illustration of behaviour. when examine address, inspector showing both proper value , negative value, i've highlighted reddish arrows in pic below. using windows calc, set in -84 , converted hex , received fff...fac. when set in 172, received ac... why same hex number, preceeded 1 in sig position?

update:

thanks patient give-and-take , great answers! think mechanics of thing, still grappling subtleties of usage. :) cheers!

to summarize, want isclasseaddress(inetaddress address) homecoming true if address between 240.0.0.0 , 255.255.255.254 inclusive. takes 2 lines of code:

public static boolean isclasseaddress(inetaddress address) { int curaddr = getintinetaddress(address); homecoming ((curaddr & 0xf0000000) == 0xf0000000) && (curaddr != 0xffffffff); }

java int twos-complement

No comments:

Post a Comment