``````12.23.34.22
``````

I need to find the network prefix for this IP Address. For that I need to find the subnet mask and then count the number of contiguous 1s to get the network prefix.

How can we calculate the subnet mask `(255.x.x.x)` for this given IP ?

• The network mask is not calculated. It is assigned. Whoever gave you the IP address can tell you what it is. Oct 21, 2016 at 5:17
• Did any answer help you? If so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. Alternatively, you could provide and accept your own answer. Aug 15, 2017 at 4:31

How can we calculate the subnet mask `(255.x.x.x)` for this given IP ?

You can't. To find the prefix, you need the address and mask, or the address and prefix length.

See the answers to this question on how to calculate everything.

• So basically only the subnet's router and the nodes that comprise the subnet know the netmask? Or more precisely, a router doesn't exactly know the netmask. It just has rules with the subnet's netmask, that tell it to send packets into the subnet, if an IP address matches a rule (that has a mask and a subnet number). And also nodes know their netmask (`ip a`). To determine if they're to accept or ignore a packet. That is, a netmask is not needed to send a packet. Correct me if I'm wrong. Aug 1, 2019 at 17:47
• ...And say if I want to know if a site is behind Cloudflare. I've got to do `ipcalc IP/PREFIX_LENGTH` for each IP range, and check if the resulting subnet number matches the one from the IP range. Or find an IP range that looks similar and check the IP with the range's prefix length. Aug 1, 2019 at 17:58
• A routing table in a router has prefixes (network and mask). IP packets only have addresses. The destination address on a packet coming into a router is compared to the entries in a routing table (masking the address) to see if it matches. If there is a match, the packet is is sent out the interface for the matching prefix, otherwise the packet is dropped. You really have no way to know the path a packet takes to its destination, and that is how IP was designed. You do not care how it gets there, and the path could change at any time; you only care that it does get to the destination. Aug 1, 2019 at 18:18

A subnet mask is a 32 bit value that allows the device that's receiving ip packets to distinguish the network ID portion of the IP address from the host ID portion of the IP address, so without a subnet mask that can be represented in form of a 32 value like (`255.0.0.0`) or slash-notation (`/8`) it is impossible to identify the network prefix of an IP address.

I think you need to know if it's classful or not. if it's classful then you can use the default subnet mask of class A which is `255.0.0.0` and so the network address will be `12.0.0.0`.