Using Python script to calculate Network ID

Let’s look at how to write a simple Python script to calculate the Net-ID given an IP address and a subnet mask.
We will work with

This is the script I will use.

Now, let’s go through it step by step to see what all the lines on it do. But first Quick reminder. Net-ID is derived by ANDing the binary equivalents of both the IP Address and the Subnet Mask.


ANDing in python is part of what are known as BITWISE operators. This are operators used on binary values. There are 6 primary bitwise operators.

Let’s examine the effect of the operators on a binary variable A and B

The NOT operator ~ is used to get the binary bit opposite. For any given variable, at any point we have 1 we replace that with 0 and where we have 0 we replace that with 1.

The shift operators are used to shift the binary bit values a number of stated times either to the left << or to the right >>. Here is an example.

>>>print(0b00110010 >>2)

So what exactly is happening here? The binary value 00110010 translates to decimal 50. If we shift the binary bits 2 times to the right the binary bits we get are 00001100. Hence an output of 12. Try the same with the << left shift

Now, Lets go back to our script.

To carry out the ANDing operation we need to convert our decimal numbers into binary octets.
Python has an inbuilt function bin( ) which we can use to convert an integer into its binary bit equivalent.
IP Address
SN Mask

I have converted a few below as an example:


Once your conversions are done we build a List each with the binary equivalents for the IP address and SN mask. We will use this lists for our next section.

The next part requires that we run a recursive AND on each of the elements of both our list. For this we will need to invoke a loop. In this case we will use a for loop.
Before we do this however, lets create another empty list which we will use to hold the outcomes of our operation.

We will run the loop 4 times as it is set up to run from iteration 0 to 4-1(3).
Within each loop it references iplist[element space] & masklist[element space] . The outcome of this operation is assigned to a variable netid.
Let’s take one example iplist[0] is 0b1010 and masklist[0] is 0b11111111. AND the two and the value received is 0b1010.

Let’s take a closer look at this line.

List are mutable data structures within Python. Meaning that the elements within them can be adjusted, added or removed. The append( ) function is used to add elements to the end of an existing List. We already created an empty list networkidlist[ ]. Now we need to populate it with elements as we receive them from the loop iterations.

Once the iterations have run our list networkidlist will contain the below details
[0b1010, 0b100, 0b1010, 0b1000000]
However when we print the contents of the list what we will see as output is [10, 10, 10, 64].

The final print statement references the elements of the networkidlist one by one using their index numbers. Converts them from integers to string to enable them be output  with the statement.

The final statement that will be displayed on the screen will be


Networks Engineering, Networks Infrastructure Security, Networks Automation

Leave a Reply

Your email address will not be published. Required fields are marked *