Amazon Interview Question
Software Engineer / DevelopersCountry: United States
A network mask helps you to determine which portion determines the network id and which portion determines the node id.
class A-255.0.0.0 class B-255.255.0.0 class C-255.255.255.0
Subnetting allows you to have multiple networks from among the class of networks which are A,B,C instead of having only one network from among the classes A,B,C. Here for class C the subnet mask is 255.255.255.224 here as the first 3 bits are set you can have 8 subnets and 32 host addresses.
For class B you can have more subnets and more host addresses as 255.255.248.0 allows you to have 32 subnets and 2^11 host addresses. It is also denoted as /27 as 27 bits are set in the network.
(Boolean) AND each address against the subnet mask then compare (SUB-tract).
If the two are equal then the address is within the network. AND in this case *masks off* the same number of lower bits from the network address and the address being tested. Thus the name "subnet *MASK*.
(In assembly language SUB-traction and comparison are effectively the same operation --- if different op-codes are present for both on a given processor it's likely that they only differ in which flags are set by the operation. A "zero" result from the subtraction only occurs if the two operand were exactly equal. Otherwise the result is either a positive or negative number; the precise representation of that number in the processor will depend on the architecture).
Logic is bit-wise and operation between networkaddress and IPaddress gives networkaddress.
1. Convert decimal dotted quad string to long integer
2. Convert a network address to a long integer
3. verify ip & net == net
Python code:
returns true if it matches.
- pirate July 30, 2013