protected static boolean isMaskCompatibleWithRange ( long value , long upperValue , long maskValue , long maxValue ) { if ( value == upperValue || maskValue == maxValue || maskValue == 0 ) { return true ; } //algorithm: //here we find the highest bit that is part of the range, highestDifferingBitInRange (ie changes from lower to upper) //then we find the highest bit in the mask that is 1 that is the same or below highestDifferingBitInRange (if such a bit exists) //this gives us the highest bit that is part of the masked range (ie changes from lower to upper after applying the mask) //if this latter bit exists, then any bit below it in the mask must be 1 to include the entire range. long differing = value ^ upperValue ; boolean foundDiffering = ( differing != 0 ) ; boolean differingIsLowestBit = ( differing == 1 ) ; if ( foundDiffering && ! differingIsLowestBit ) { int highestDifferingBitInRange = Long . numberOfLeadingZeros ( differing ) ; long maskMask = ~ 0L >>> highestDifferingBitInRange ; long differingMasked = maskValue & maskMask ; foundDiffering = ( differingMasked != 0 ) ; differingIsLowestBit = ( differingMasked == 1 ) ; if ( foundDiffering && ! differingIsLowestBit ) { //anything below highestDifferingBitMasked in the mask must be ones //Also, if we have masked out any 1 bit in the original, then anything that we do not mask out that follows must be all 1s int highestDifferingBitMasked = Long . numberOfLeadingZeros ( differingMasked ) ; long hostMask = ~ 0L >>> ( highestDifferingBitMasked + 1 ) ; //for the first mask bit that is 1, all bits that follow must also be 1 if ( ( maskValue & hostMask ) != hostMask ) { //check if all ones below return false ; } if ( highestDifferingBitMasked > highestDifferingBitInRange ) { //We have masked out a 1 bit, so we need to check that all bits in upper value that we do not mask out are also 1 bits, otherwise we end up missing values in the masked range //This check is unnecessary for prefix-length subnets, only non-standard ranges might fail this check. //For instance, if we have range 0000 to 1010 //and we mask upper and lower with 0111 //we get 0000 to 0010, but 0111 was in original range, and the mask of that value retains that value //so that value needs to be in final range, and it's not. //What went wrong is that we masked out the top bit, and any other bit that is not masked out must be 1. //To work, our original range needed to be 0000 to 1111, with the three 1s following the first masked-out 1 long hostMaskUpper = ~ 0L >>> highestDifferingBitMasked ; if ( ( upperValue & hostMaskUpper ) != hostMaskUpper ) { return false ; } } } } return true ; }
when divisionPrefixLen is null isAutoSubnets has no effect