Programming challenge description:
A network block is defined by an IP address and a subnet mask. The mask value separates the network address from the internal address based on how many bits are hidden by the mask.
For example, an 8-bit subnet mask is expressed as 255.0.0.0. An IP address of 10.21.8.15 with that mask applied reveals a network address of 10.0.0.0.
Your challenge is to inspect an IP address and mask number, then decide if another IP address is on that local network.
Considering the IP block 10.0.0.0 and a subnet mask of 255.0.0.0, we can represent this like so: 10.0.0.0/8 (this is called CIDR notation).
As an example, the address 10.21.8.15 would be on the local network. The address 188.8.131.52 would not.
Explanation: CIDR notation 10.0.0.0/8 means that 8 most significant bit are fixed and rest of 24 bits are available to use for IP addresses so here 10 is fixed as it lies on first 8 bits but other 3 places (which lie on rest of 24 bits) in doted notation can have any value between 0-255 so any address like 10.100.10.13 is on this network but address like 184.108.40.206 is not valid on this network because we cannot have 12 at first place only 10 is allowed.
As another example if we have 172.16.0.0/12, it means 12 most significant bits are fixed and rest of 20 bits are free for change so here 172 is fixed because it is on first 8 fixed bits but from next 8 bits only first 4 bits are fixed so at second place we can have any value from 16-255, on 3rd and 4th place nothing is fixed so we can use any value between 0-255.
Three lines of input as follows:
IPv4 Network block in standard CIDR notation
IPv4 Address to Check
If the IP address is in the subnet defined by the CIDR, print “True”. Otherwise, print “False”.