# Is this explanation correct - how to determine if an IP address is on the same subnet [duplicate]

1. Is this explanation of how to determine, mathematically, if a destination IP address is in the same subnet as the sender, correct or not?

2. Is this what the computer does or does it do the math differently?

How do you calculate the prefix, network, subnet, and host numbers? is a great entry and covers a lot of ground, but to answer my question, I have to extrapolate and so I am left with the question as to whether or not my extrapolation is correct.

To determine if a packet is to be handed off to the switch to deal with, or forwarded to the router (gateway), we need to determine if the destination address is within the same network or not.

To do that, we can simply apply the netmask to the destination address so as to extract the network prefix. If we get the same value as the prefix for the computer, then it is on the same network.

For this example, let's assume the user is doing a `ping` to a computer with the IP address of `192.168.22.45`. Let's apply the netmask of our computer against this IP address:

``` 11000000.10101000.00010110.00101101 (192.168.22.45 - IP address) AND 11111111.11111111.11111111.00000000 (255.255.255.0 - Netmask) ----------------------------------- = 11000000.10101000.00010110.00000000 (192.168.22.0 - Prefix) ```

Finally, let's compare the prefix gotten to the prefix we got with the IP address of our computer earlier: `192.168.22.0` is not equal to `192.168.1.0`.

Or in binary with Boolean logic, we compare the two prefix using the Exclusive-OR operator. If we get all 0s, then the two prefix are equal and therefore, are in the same network.

``` 11000000.10101000.00010110.00000000 (Destination prefix - 192.168.22.0) XOR 11000000.10101000.00000001.00000000 (Local computer prefix - 192.168.1.0) ----------------------------------- = 00000000.00000000.00010111.00000000 ```

• @RonMaupin - I definitely read this post, but it does not explain how to determine if an IP address is part of a subnet or not, mathematically. I can derive an answer but I am trying to confirm that the above is correct.
– ETL
Apr 12 '16 at 23:43
• It explains how to get the network of an address if you have the mask. If you have two addresses, you mask them, and and the results are equal, what does that tell you? Apr 12 '16 at 23:45
• Slight correction. Discovering whether the target is on your network or not will determine whether ARP will try to resolve the target's MAC address or the Default Gateway's MAC address. Either way, the packet will probably go through the switch. Apr 13 '16 at 7:49
• ...let's try keeping this Q around. It is, basically a dupe, but maybe the students will find it and stop re-asking this same question. :/ Apr 13 '16 at 13:12
• @CraigConstantine - I guess this type of question comes up often! I can see how it's almost a duplicate but I edited it to explain why I felt I didn't get an answer from the other question.
– ETL
Apr 13 '16 at 13:54

Yes, there is a mathematical way (and that is how the computer does it unless I'm much mistaken).