## DEV Community is a community of 894,881 amazing developers

We're a place where coders share, stay up-to-date and grow their careers. # Quick Overview of Binary Math for Calculating Subnets

I’ve been a technologist for nearly a decade, and for whatever reason, a weak point of mine has always been networking; I’m fine if I have values to plugin configuring and architecting a network space and components, but when it comes to subnetting, I’m completely helpless, even post-certification.

That said, it’s something that, once I committed some basics to memory, things that transcend networking into areas I understand much better than I do subnetting, I can usually intuit my way through various problems in production.

If you find yourself struggling with these concepts, this might give you a helpful approach to learning them.
Binary Math

There’s certain pieces I am going to gloss over, because the use-case for this knowledge is pretty specific, so a unit like a bit is probably one you’re familiar with, if only conceptually, and since we’re talking about binary math in the context of IP addressing, we’ll use 8 bits as our example, but these principles apply to any space, following the same patterns.

To sum up what is relevant about a bit (and its abstractions; a byte, or in the context of an IP address octet):

byte = eight bits
bit = a zero or one

With this standard, in an IP address, an octet is equivalent to 1 byte (so an IP address, 192.168.0.1, for example, contains 4 octets of 8 bits each, and therefore is a 32 bit address).

So, let’s take these two piece of knowledge and look at our rubric for converting a 32-bit IP address into binary, or binary into a 32-bit IP address:

``````Conversion Chart
----------------
128 64 32 16 8 4 2 1
0  0  0  0 0 0 0 0
``````

So, for each bit in your 8-bit octet, the value will increment (i.e. multiply by 2 in this case) when you go to evaluate the values going in and out of this conversion rubric.

What this chart is meant to tell you (for the sake of demonstration) is that you can represent values between 0 and 255 in 8-bit: In binary, a bit is either a 1 or a 0 (2 values), so in an 8-bit space, it allows for values between 0–255 (i.e. 2 x 128, per the above chart) per 8-bit octet (so the output from the conversion cannot exceed a result of 255).

In conversion, for every 1 in the binary number, the bit place is added to the total. Let’s take the example of

Converting Binary value 10000010 to decimal:

``````    128 64 32 16 8 4 2 1
+  1  0  0  0 0 0 1 0
---------------------
128  0  0  0 0 0 1 0 = 130 in decimal
``````

So, you see the process, but how do you decide a bit value is a 1 or a 0? If converting binary to decimal, you simply add the place value to the output sum. In this case, you only have 1 as a bit in the first, and seventh place, which mean your decimal value is 128 + 2 .

If the input binary were 11111111 , you would be, then, adding 128 + 64 + 32 and so on until your place values reach 1 to make 255 (the upper-limit of the possible range of output values in an 8-bit space! — all of your place values are set to 1).

One more example of Binary to Decimal conversion:

``````11101000 to Decimal:

128 + 64 + 32 + 0 + 8 ->
232
``````

To sum up this portion, let’s clarify: If you have a 2-bit space, what does your conversion rubric look like? Well, with two bits:

``````2 1
``````

your sum can only top out at 3, so possible binary values to be input are 00 , 01 , 10 , 11 (i.e. 0, 1, 2, 3).

## 2 bits

If you only have 2 bits to work with, the upper limit is 3:

``````    00 = 0
01 = 1
10 = 2
11 = 3

3 bits

----------
000 = 0
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
110 = 6
111 = 7
``````

Converting a Decimal value back into Binary is a little more involved, but you rely on this same understanding of the rubric, and how a bit and byte interact.

Let’s say you have a decimal value of 154, and this is one octet in your IP address:

``````128 64 32 16 8 4 2 1
?  ?  ?  ? ? ? ? ?
-------------------------
``````

Where ? represents the bit value (1 or 0). In decimal-to-binary conversion, what determines if a bit is a 0 or a 1? If the decimal value is equivalent or less than the bit-space value (plus any preceding 1 value bit spaces), it is a 1 , so:

Is 128 less than, or equal to, 154? Yes, so:

``````128 64 32 16 8 4 2 1
1  ?  ?  ? ? ? ? ?
-------------------------
128+...              = 154
``````

Is 128 + 64 less than 54? It sums up to 192, so, no, it doesn’t, and thus the next bit place is a 0 :

``````128 64 32 16 8 4 2 1
1  0  ?  ? ? ? ? ?
-------------------------
128+ 0+...           = 154
``````

If a place value returns a 0 then, you do not need to consider that value in the sum. So, in this case, we get:

``````128 64 32 16 8 4 2 1
+   1  0  0  1 1 0 1 0
--------------------
128+ 0+ 0+16+8+0+2+0           = 154
``````

Where only the the combinations of the above spaces were less than or equal to the decimal value we hoped to convert to binary.

Here are a couple of more exercises (with the processing and solutions):

``````Binary -> Decimal
11000101
->
128 + 64 + 0 + 0 + 0 +4 + 0 + 1 = 197

Decimal -> Binary
212
=
11010011

>>> 128 <= 212
True
>>> 128 + 64 <= 212
True
>>> 128 + 64 + 32 <= 212
False
>>> 128 + 64 + 16<= 212
True
>>> 128 + 64 + 16 + 8 <= 212
False
>>> 128 + 64 + 16 + 4 <= 212
False
>>> 128 + 64 + 16 + 2 <= 212
True
>>> 128 + 64 + 16 + 2 + 1 <= 212
True
>>> If `False`, do not add that bit-place value to the sum.
``````

Try selecting binary values of 8-bits, and random decimal values and see if this process works for you!
Applying Binary Math to Understand Subnets

The hard part of subnetting for me was understanding what my usable range is when handed an IP address from somewhere in the range of usable IPs, and then a subnet mask; how do I figure out the network address and broadcast address?

Let’s use this example:

``````192.168.1.165
255.255.255.0
``````

What are the relevant values for the network this IP is on, given the netmask 255.255.255.0 ?

Well, the solutions is called a bit-wise AND: Whenever there is a 1 in the same place in, both, the address and netmask address, a one is carried over when we use the rubric below, for these binary representations (so you’ve performed the binary to decimal conversion for each octet — a decimal value-):

``````11000000.10101000.00000001.10100101
11111111.11111111.11111111.00000000
``````

Using the rule of carrying down a 1 for every time a 1 appears in both rows, you get the result (appearing in both row):

``````11000000.10101000.00000001.00000000
``````

So, using binary math, you need to convert these octets back into decimals to get a usable IP address to be your network address, and when you do the conversion you get:

``````192     .168     .1       .0
``````

``````192.168.1.1 - 192.168.1.254
``````

Figuring out the number of usable hosts (available addresses in a subnet), as I popped out above, you take the same approach, using a binary-to-decimal conversion, and then a conversion back into decimals to find the range’s end:

``````255.255.255.0   11111111.11111111.11111111.00000000
255.255.255.128 11111111.11111111.11111111.10000000
255.255.255.192 11111111.11111111.11111111.11000000
255.255.255.224 ...
255.255.255.240 ...
255.255.255.248 ...
255.255.255.252 ...
255.255.255.254 ...
``````

Given an address in a given subnet, using the bitwise AND procedure against whatever subnet mask you are given, you can determine the network and broadcast addresses like we did above (using whatever netmask address is provided).
Conclusion

There’s a lot of other examples of subnet math that you may run into administrating a large network, with multiple subnets, VLANs, etc. but my hope is that, if you struggled with this like I did, it can give you basis to start with when you encounter an unexpected issue creating networks.

A comprehensive resource I found on Github a while back was this chart, courtesy of Mariko Kosaka:
kosamari/DecimalHexBinary

which also includes hexadecimal values! But is an awesome resource for quick reference on binary-to-decimal values.