Pretend you are a router. If you are at a certain IP and you have a certain destination IP to handle, do you send it to a router gateway, or can your computer talk directly with the destination?
Answer: XOR the destination with your IP and AND the result with the gateway mask, a zero result means you can talk to it directly.
Examples below assume I’m at 10.0.0.113 looking to handle various IP addresses with a netmask of 255.255.255.0.
send to myself:
10.0.0.113 XOR IP> 0.0.0.0 AND MASK> 0.0.0.0 > available without router
send to a neighbor on my subnet:
10.0.0.114 XOR IP > 0.0.0.1 AND MASK > 0.0.0.0 > available without router is correct
send to someone out of my subnet:
10.0.1.113 XOR IP> 0.0.1.0 AND MASK > 0.0.1.0 > send to gateway is correct
notice the order is not reversible (yields a bad result)
Someone in my subnet:
10.0.0.114 AND MASK > 10.0.0.0 XOR IP> 0.0.0.1 > send to gateway is the ~wrong~ answer.
In fact, ~all traffic will go to the gateway (wrong answer)
10.0.1.X AND MASK> 10.0.1.0 > XOR IP > 0.0.1.113 > send to gateway is the ~wrong~ answer.
Notice this logic can also be used to determine ~which~ gateway or route the packet is suppose to go to by ignoring your own IP address and comparing instead to possible destination gateways. Evan Anderson does a fantastic and excruciatingly detailed writing on the topic of subnetting. Download it to a pdf for your library. However, it’s almost too detailed. Summarizing his example 1/3 of the way down the page:
Destination is 192.168.10.2. Options for routing include router #1 at 192.168.10.1 and router #2 at 192.168.20.1.
192.168.10.2 XOR ROUTER 1 > 0.0.0.3 AND MASK > 0.0.0.0 – zero, yes use this router
192.168.10.2 XOR ROUTER 2 > 0.0.30.3 AND MASK > 0.0.30.0 – nonzero, do not use this router