Network ID Calculation Process with Subnet Mask and Bitwise AND

An IPv4 address is 32 bits long and is written as 4 octets of 8 bits each. Each octet takes a value between 0 and 255. IPv6 uses a 128-bit address space. An IPv4 address alone does not tell you which network it belongs to. That information becomes clear only when the address is read together with the subnet mask, which shows how many bits of the address belong to the network portion and how many to the host portion. For example, the subnet mask 255.255.255.0 states that the first 3 octets are the network portion and the last octet is the host portion.

Subnetting divides a large IP block into smaller subnets. In classic multi-access IPv4 subnets such as the /28 in this article, the first address is reserved as the network ID and the last address is reserved for broadcast. The network ID identifies the subnet and is not assigned to a device. The broadcast address is used to send to all devices in that subnet. The addresses in between are assigned to hosts. There are 2 exceptions: on /31 point-to-point links both addresses can be assigned to the endpoints, and a /32 specifies a single address and does not contain a separate broadcast address. When subnets are separated from each other by a Layer 3 device, each one becomes a separate broadcast domain, and access rules and routing policies can also be applied per subnet.

The subnet that an IP address belongs to cannot always be identified by looking at the address alone. 192.168.10.10 and 192.168.10.20 appear at first to be in the same network, but with a /28 mask they are in different subnets. The operation that makes the distinction is the bitwise AND calculation. The IP address and the subnet mask are ANDed bit by bit in binary, and the result gives the network ID. The operating system uses the same operation in kernel mode when it decides whether a packet is delivered directly or sent to a router. A destination in another subnet does not mean that the packet goes to the WAN. The destination can also be another VLAN in the same building.

In network designs that contain more than 1 subnet, manual ANDing gives the exact network ID that an address belongs to. You need to check separately the Layer 2 and VLAN configuration to see which broadcast domain the address is in, the relevant policy to see which access rule applies to it, and the routing table of the source system to see which route it follows. I perform the operation by hand first, and then move on to where and with which data Windows performs the same calculation.

Manual Bitwise AND Calculation

I take the 192.168.10.0 /28 network as the example. /28 means that the subnet mask is 255.255.255.240. In the last octet, 4 bits remain for the host portion, and each subnet forms a block of 16 addresses. The network ID of the first subnet is 192.168.10.0, the usable host range is 192.168.10.1 - 192.168.10.14, and the broadcast address is 192.168.10.15. The next subnet starts at 192.168.10.16.

Because ANDing works at the binary level, I first need to convert the IP address and the subnet mask from the decimal system to the binary system. An octet is 8 bits, and each bit carries a weight from 27 down to 20.

Bit Position 7 6 5 4 3 2 1 0
Value (2n) 27 = 128 26 = 64 25 = 32 24 = 16 23 = 8 22 = 4 21 = 2 20 = 1

In the conversion I start from the highest weight. If the weight fits into the remaining number, that bit is 1 and the weight is subtracted from the number. If it does not fit, the bit is 0. For the number 10, the steps are as follows.

Bit Weight Comparison Bit Value Remainder
128 128 does not fit into 10 0 10
64 64 does not fit into 10 0 10
32 32 does not fit into 10 0 10
16 16 does not fit into 10 0 10
8 8 fits into 10 1 10 - 8 = 2
4 4 does not fit into 2 0 2
2 2 fits into 2 1 2 - 2 = 0
1 1 does not fit into 0 0 0

Reading the bit values from left to right gives 00001010 as the binary equivalent of 10. With the same method I get 192 = 11000000, 168 = 10101000, 20 = 00010100 and 240 = 11110000.

๐Ÿ‘‰ AND Operation:

Bitwise AND is an operation based on Boolean logic. If both bits are 1, the result is 1. In all other combinations it is 0. In digital circuits this operation is implemented with an AND gate and is executed at hardware level in the processor. In Boolean algebra it is shown with the ยท (dot) symbol.

A B A AND B
1 1 1
1 0 0
0 1 0
0 0 0

Bits that are 1 in the subnet mask pass the corresponding bit of the IP address through unchanged. Bits that are 0 set the result to 0. As a result, the AND operation leaves the network portion of the IP address, and the host portion is set to 0.

Network ID of the 192.168.10.10 /28 Address

First, I show that the 192.168.10.10 /28 address belongs to the 192.168.10.0 /28 network ID, which is the 1st network. I place the binary values of the IP address and the subnet mask one under the other and AND them.

Row 1st Octet 2nd Octet 3rd Octet 4th Octet
IP Address (Decimal) 192 168 10 10
IP Address (Binary) 11000000 10101000 00001010 00001010
Subnet Mask (Decimal) 255 255 255 240
Subnet Mask (Binary) 11111111 11111111 11111111 11110000
Result (Binary) 11000000 10101000 00001010 00000000
Network ID (Decimal) 192 168 10 0

In the first 3 octets, all bits of the subnet mask are 1, so the IP address passes through unchanged. In the last octet, when 00001010 is ANDed with 11110000, the result is 00000000 because the 1 bits of the IP address align with the part of the mask that is 0. The resulting 192.168.10.0 /28 network ID confirms that the 192.168.10.10 /28 address belongs to the 1st network.

Network ID of the 192.168.10.20 /28 Address

This time I take the 192.168.10.20 /28 address, which is not in the 192.168.10.1 /28 - 192.168.10.14 /28 range. When I AND it with the same subnet mask, the result should be a different network ID.

๐Ÿ‘‰ AND Operation:

Row 1st Octet 2nd Octet 3rd Octet 4th Octet
IP Address (Decimal) 192 168 10 20
IP Address (Binary) 11000000 10101000 00001010 00010100
Subnet Mask (Decimal) 255 255 255 240
Subnet Mask (Binary) 11111111 11111111 11111111 11110000
Result (Binary) 11000000 10101000 00001010 00010000
Network ID (Decimal) 192 168 10 16

In the last octet, the bit with weight 16 in 00010100 aligns with the part of the mask that is 1, so it passes to the result and 00010000 remains. The resulting 192.168.10.16 /28 network ID shows that the 192.168.10.20 /28 address does not belong to the 1st network and is in the next subnet, the 192.168.10.16 /28 network. The 2 addresses have the same first 3 octets, but they are in different subnets because of the 4 mask bits in the last octet.

The operating system also performs the calculation that I did by hand. In the next section I cover, in order, where the IP information is kept in Windows, how it is loaded into memory and at which point of the routing decision the AND operation is applied.

The Bitwise AND Calculation Process in Windows

Before an IP packet leaves the Windows TCP/IP stack, the kernel must answer this question: is the destination directly reachable, or must the packet be passed to a router? In the teaching explanation, this decision is shown as ANDing the client's own IP address and the destination IP address with the same subnet mask and comparing the 2 network IDs. The result is correct, but the path that Windows follows is slightly different. The stack determines the suitable route for the destination from the routing table and applies the bitwise AND operation with the netmask value of each route in the table. The local subnet is also present in this table as a route.

The comparison of the source and destination network IDs is a teaching model. Windows reaches the same result by ANDing the destination IP address with the netmask values of the routes in the routing table.

The data that the decision is based on passes through 3 stages. The persistent configuration is stored in the registry. tcpip.sys keeps this configuration in memory, and the routing table is built from this information. When a packet is to be sent to a destination and a usable destination cache entry exists for that destination, the next hop and interface in the entry are used. Otherwise the suitable route is determined from the routing table in memory. The NIC carries the result of the decision.

1. IP and Subnet Information Is Stored Persistently in the Registry

When an IP address, subnet mask and default gateway are defined persistently on a NIC, or when this information comes from DHCP, the related values are located in the registry under a separate key for each network interface.


HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{GUID}

According to the Microsoft document that describes this key in detail, manually entered values are stored under the names IPAddress, SubnetMask and DefaultGateway, and values obtained from DHCP are stored under the names DhcpIPAddress, DhcpSubnetMask and DhcpDefaultGateway. If a valid manually entered value exists, it takes precedence over the value from DHCP. The document that describes these 6 values together was written for Windows XP, and the key path is the same in current versions.

The AND process in Windows

Not every IP setting has to be persistent. For example, an address applied with store=active in netsh interface ipv4 commands is valid only in that session and is lost at restart. The default option, store=persistent, makes the setting persistent.

The registry acts as the persistent store here. The registry is not read for the routing decision during packet transmission. The decision is made from the running configuration in memory.

Do not change the values under this key manually with Registry Editor. The change may not be reflected in the running stack without a restart. For IP settings, use the adapter properties, netsh or PowerShell.

To see the running configuration, I use the following PowerShell command. The first part of the command shows the IP, default gateway and DNS summary of the interface. The second part lets me find the {GUID} key of the same interface in the registry. Get-NetIPConfiguration is part of the NetTCPIP module that came with Windows Server 2012 and Windows 8. You need to replace the "Wi-Fi" value with the interface alias in your own environment.


Get-NetIPConfiguration -InterfaceAlias "Wi-Fi"; (Get-NetAdapter -InterfaceAlias "Wi-Fi").InterfaceGuid

The AND process in Windows

This summary does not give the subnet mask as a separate field. To see the IPv4 address together with the mask length, I use the Get-NetIPAddress command. The PrefixLength field shows the number of 1 bits in the mask. For /28 this value is 28.


Get-NetIPAddress -InterfaceAlias "Wi-Fi" -AddressFamily IPv4 | Select-Object IPAddress, PrefixLength

2. tcpip.sys Loads When the Computer Starts

The component that implements the TCP/IP protocols in Windows is the tcpip.sys driver. In my example environment it is a file on disk at the path C:\Windows\System32\drivers\tcpip.sys. It is loaded into memory during system startup and operates as a protocol driver that runs in kernel mode.

The AND process in Windows

When a network interface is active, the IP and subnet information of that interface is present in the running configuration of the TCP/IP stack in memory. Routing decisions are made with this current information in memory.

3. How Is This Information Kept and Used in RAM?

The TCP/IP stack keeps information such as the IP address, subnet mask and interface index in memory for each interface. The routing table is also built automatically from this configuration. When the address 10.10.10.10 is assigned to an interface with the mask 255.255.255.0, an on-link route for the 10.10.10.0 network is added to the table. When a default gateway is defined, the 0.0.0.0/0 route is created.

The calculation is performed on the CPU, on these values in memory. Logically, the destination address is ANDed with the netmask value of the route, and the result is compared with the network address of the route. The disk and the registry are not accessed during this operation.

4. The Routing Decision and the Bitwise AND Operation

When an application wants to send data, the TCP segment or UDP datagram that comes from the transport layer is turned into an IP packet at the network layer. At this stage the stack must determine the next-hop address and the outgoing interface for the destination. My example values are as follows:

1. Source IP address: 10.10.10.10
2. Destination IP address: 10.10.20.5
3. Subnet mask: 255.255.255.0

In the teaching model, 2 AND operations are performed. 10.10.10.10 AND 255.255.255.0 gives 10.10.10.0, and 10.10.20.5 AND 255.255.255.0 gives 10.10.20.0. Because the 2 network IDs are different, the destination is in another subnet.

Windows reaches the same result from the routing table. The destination IP address is ANDed with the netmask value of each route in the table, and if the result equals the network address of that route, the route is considered a match. In the table below, I show the 2 routes that are relevant to the example. A real routing table also contains loopback, broadcast and multicast routes.

Route Netmask 10.10.20.5 AND Netmask Result
10.10.10.0 (on-link) 255.255.255.0 10.10.20.0 No match
0.0.0.0 (default route) 0.0.0.0 0.0.0.0 Match

For the destination 10.10.20.5, the only matching route is the default route, so the next hop is the default gateway address. If the destination were 10.10.10.25, both routes would match. In that case the route with the most 1 bits in its netmask is selected (longest match), so the on-link route is selected and the next hop is the destination itself. If there is more than 1 route of equal length, the one with the lowest metric value is used.

The default gateway is not used in every case. If a more specific route is defined for the destination, the packet goes to the next-hop address of that route. This address can be a router other than the default gateway. The 0.0.0.0/0 route takes effect when no other route matches.

A default gateway also does not have to be defined. If it is not defined and no route matches the destination, the stack produces a routing error and the packet is not sent.

The lookup is not repeated from the beginning for every packet. The stack keeps the next-hop and interface information that it found for a destination in the destination cache, and it uses that entry for later packets to the same destination if a usable entry exists. You can see the entries with the netsh interface ipv4 show destinationcache command.

After the next hop is determined, the Layer 2 address is needed. The stack first checks the neighbor cache. In IPv4 this is the table known as the ARP cache. For an on-link destination, the MAC address of the destination itself is used. For transmission through a router, the MAC address that belongs to the next-hop IP address of the selected route is used. If the default route is selected, this IP is the default gateway address. If the neighbor cache has no usable MAC mapping for the next hop, the ARP process starts. The presence of an entry in the cache is not sufficient by itself. For an entry whose state is Incomplete, address resolution is still in progress and the MAC address has not been learned yet.


The application sends the data
        |
        v
Transport Layer (TCP Segment / UDP Datagram)
        |
        v
Network Layer (IP Packet is created)
        |
        v
Is there a usable entry for the destination in the Destination Cache?
        |-- Yes --> The stored next-hop and Interface are used
        |-- No  --> Routing Table lookup
        |           (Destination IP AND Netmask, longest match, Metric)
        v
Is the selected route on-link?
        |-- Yes --> Next-hop = Destination IP address
        |-- No  --> Next-hop = Gateway address of the route
        v
Is there a usable MAC address for the next-hop in the Neighbor Cache?
        |-- Yes --> The stored MAC address is used
        |-- No  --> An ARP Request is sent
        v
The Ethernet Frame is prepared and passed to the NIC

This flow is carried out by the route lookup and next-hop determination steps of the TCP/IP stack in kernel mode. The result of the same lookup can be obtained from user mode with IP Helper APIs such as GetBestRoute2 and GetBestInterface. GetBestRoute2 returns the best route entry and the source IP address to be used for the given destination IP address.

To observe the decision from PowerShell, I use 3 commands. The first lists the IPv4 routing table in memory. The second shows the route and the source IP address that the stack selects for the destination I give. The third returns the IP and MAC mappings in the neighbor cache together with their state information. The commands belong to the NetTCPIP module.


Get-NetRoute -AddressFamily IPv4

Find-NetRoute -RemoteIPAddress 10.10.20.5

Get-NetNeighbor -AddressFamily IPv4

5. The NIC Is Not Where the Routing Decision Is Made

When the operating system passes a frame to the NIC, the decision has already been made. The route has been selected, the next hop has been determined, the MAC address of the next hop has been resolved and the Ethernet frame has been prepared with this information. The task of the NIC is to transmit this frame, which contains the destination MAC address, to the physical medium.

๐Ÿ‘‰ Division of work between the network interface card (NIC) and the TCP/IP stack:

Operation Performed By
ANDing the destination IP address with the routes and selecting the route TCP/IP stack (tcpip.sys)
Determining the next-hop address and the outgoing interface TCP/IP stack (tcpip.sys)
Resolving the next-hop MAC address (neighbor cache, ARP) TCP/IP stack (tcpip.sys)
Transmitting the Ethernet frame to the physical medium NIC
Checksum calculation and segmentation of large TCP packets (if offload is enabled) NIC

The last row of the table clarifies a detail. Current NICs are not completely unaware of the IP and TCP headers. When Checksum Offload and Large Send Offload (LSO) are enabled, checksum calculation and the segmentation of large TCP packets are transferred from the TCP/IP stack to the NIC hardware. These features concern how the packet is prepared and do not transfer route selection to the NIC. In the scenario of this article, that is, a Windows host that uses a standard NIC, the TCP/IP stack selects the outgoing route.

SmartNIC and DPU designs that run a separate forwarding layer on the device itself are outside this generalisation. This hardware can use the route table that is programmed into it for forwarding. That is a separate decision from the host's own route selection.

This article covered how I find by hand which subnet an IP address belongs to with the bitwise AND calculation, using /28 examples. It then walked through how Windows uses the same operation in the flow that runs from the persistent configuration in the registry to the running configuration in memory, and from the routing table lookup to the NIC.

I hope you find it useful...

No comments on this article yet. Be the first to comment!

750 characters available.
Security code
Comments are published once approved.
Your email is only used to let you know when your comment is approved. It is never published.