Search code examples
cnetwork-programmingipipv4subnet

Compare two ip subnet


How to decide if two ip are on the same subnet or not? The only input is the ip address and the subnet mask! What is the optimal way, using C/C++, to compute if two ip have the same subnet?


Solution

  • bool checkForSubnetEquality(in_addr_t ipA, in_addr_t ipB, uint32_t subnetMask) {
       return (ipA & subnetMask) == (ipB & subnetMask);
    }