How to calculate if a given host is in the same subnet as another
















Variables: 
IPA 
Address of Host A 

TMP 
temporary variable 








NMA 
Netmask of Host A 

CGU 
Result 









IPB 
Address of Host B 


If zero: 
Hosts are in same subnet 






NMB 
Netmask of Host B 


If not zero: 
Hosts are not in same subnet 




















Operations: 














Host A 






Host B 







TMP 
= IPA xor IPB 




TMP 
= IPA xor IPB 





CGU 
= TMP and NMA 




CGU 
= TMP and NMB 




















"xor" and "and" are binary operations that mask one binary number with another 







Xor: 
Sets all positions to "1" that are different in the two numbers and all positions to "0" that are equal. 






Example: 0101 xor 0011 = 0110 


























And: 
Sets all positions to "0" in the two numbers except for those where both are "1" 







Example: 0101 and 0011 = 0001 

























Examples 






























Host A 






Host B 





















IPA 
192.168.1.1 (=10000000.10101000.00000001.00000001) 

IPB 
192.168.1.2 (=10000000.10101000.00000001.00000010) 
NMA 
255.255.255.0 (=11111111.11111111.11111111.00000000) 

NMB 
255.255.255.0 (=11111111.11111111.11111111.00000000) 

TMP 
= IPA xor IPB 













= 192.168.1.1 xor 192.168.1.2 = 0.0.0.3 (=00000000.00000000.00000000.00000011) 






CGU 
= TMP and NMA = 0.0.0.3 and 255.255.255.0 = 0.0.0.0 










The hosts are in the same subnet 

























IPA 
192.168.1.1 





IPB 
192.168.5.2 




NMA 
255.255.255.0 





NMB 
255.255.255.0 





TMP 
= IPA xor IPB 













= 192.168.1.1 xor 192.168.5.2 = 0.0.4.3 










CGU 
= TMP and NMA = 0.0.4.3 and 255.255.255.0 = 0.0.4.0 










The hosts are not in the same subnet 
























IPA 
192.168.1.1 





IPB 
192.168.5.2 




NMA 
255.255.0.0 





NMB 
255.255.255.0 





TMP 
= IPA xor IPB 





TMP 
= IPA xor IPB 





= 192.168.1.1 xor 192.168.5.2 = 0.0.4.3 




= 192.168.1.1 xor 192.168.5.2 = 0.0.4.3 


CGU 
= TMP and NMA = 0.0.4.3 and 255.255.0.0 = 0.0.0.0 


CGU 
= TMP and NMB = 0.0.4.3 and 255.255.255.0 = 0.0.4.0 


For Host A, the hosts are in the same subnet 



For Host B, the hosts are not in the same subnet 