public static boolean sameNetwork ( int prefixLength , InetAddress address1 , InetAddress address2 ) { if ( logger . isDebugEnabled ( ) ) { logger . debug ( "Comparing address " + address1 . getHostAddress ( ) + " with " + address2 . getHostAddress ( ) + ", prefixLength=" + prefixLength ) ; } long prefixMask = 0xffffffff L & ( - 1 << ( 32 - prefixLength ) ) ; return ( addressToLong ( address1 ) & prefixMask ) == ( addressToLong ( address2 ) & prefixMask ) ; }
Checks whether two internet addresses are on the same subnet .