An IP Subnet is defined with two parts, a network and a prefix-length or mask.
For example 192.168.0.0/16 (or, 192.168.0.0/255.255.0.0).
An IP address like 192.168.1.1 is said to match this subnet because,
(192.168.1.1 & 255.255.0.0) == 192.168.0.0
I am interested in what might be called the inverse of a subnet
which is described like this,
For a given SubnetA (say, NetworkA / MaskA),
The inverse of SubnetA is the list of k subnets, such that,If an IP address A, matches SubnetA,
A will not match any of these k subnets, and
Every IP address B that does not match SubnetA,
will match exactly 1 of these k subnets.
Code is not necessary, I am interested in a correct and optimal method.
I have the optimized answer noted for reference below so it does not distract people trying this as a problem. Have retained acceptance of Rafał’s answer since he also got it right first.
One subnet for every unmasked bit
binA, matching all the previous bits inA, differing onb, masking all the following bits. This way each addressinot inAwill match only one of the above networks, namely the one responsible for the first bit ofithat does not matchA.