Getty Images/iStockphoto

Tip

How to convert binary to decimal

It's helpful for network admins to know how to convert binary to decimal, and vice versa, for IPv4 addressing, subnet masks, default gateways and network IDs.

Part of the knowledge set required for network administrators and architects is understanding how network devices use IPv4 addressing and how computer systems use binary.

Humans use decimal-based addressing, but computer networks and devices rely on binary-based addressing. As such, network practitioners must know how to convert IP addresses, subnet masks and default gateway information from dotted decimal notation into binary. These tasks also require the skill to convert from binary to decimal.

What is binary?

Binary is a numbering system that uses only the digits 1 and 0. Each digit represents a value: 1 is "on" or "true," while 0 is "off" or "false."

Just as humans use the decimal numeral system -- with the digits 0 through 9 -- for all counting, money and financial transactions, computers use binary for data storage, data transmissions and numerical calculations. Because human administrators use decimal, computing systems must convert decimal to binary for their use.

One way to understand binary and decimal conversion is to think of them as two differing languages. Someone who speaks both English and Spanish, for example, is considered bilingual because they understand two languages. Similarly, a computer understands two numbering systems: binary and decimal.

Binary-to-decimal conversion

Depending on the task, humans may need to convert the binary numbers computers use back to decimal. In this article, we look at standard conversion and then explore how binary and decimal conversion applies to networking.

Converting from binary to decimal isn't difficult. The numbers 0 and 1 in binary convert to 0 and 1 in decimal. After that, it gets a little more complicated. It's helpful to remember the following factors when converting binary into decimal:

  • always start on the right-hand side of the binary number; and
  • understand that each decimal digit in the binary conversion chart doubles in size as it goes left.

The first binary digit is converted to decimal as either 0 or 1. The digit to the left of this first digit doubles from 1 to 2. The digit after that doubles from 2 to 4 and so on. As the chart below shows, binary uses the power of two as each digit moves further to the left.

Power of two

27

26

25

24

23

22

21

20

Decimal space

128

64

32

16

8

4

2

1

Binary bits

The next step is to add up the numeric value of each bit that is "on," represented by the number 1. If the bit has a 0 digit, the binary number can be ignored. For example, the binary conversion of 1001 means the 1 and the 8 bits are activated, but not the 2 or 4 bits. Thus, the binary-to-decimal conversion for 1001 is 8 + 1 = 9.

These numbers are calculated using the following table:

Power of two

27

26

25

24

23

22

21

20

Decimal space

128

64

32

16

8

4

2

1

= 9

Binary bits

0

0

0

0

1

0

0

1

= 1001

Here is an example of counting from zero to 10 using binary:

Decimal Binary

0

0

1

1

2

10

3

11

4

100

5

101

6

110

7

111

8

1000

9

1001

10

1010

Decimal-to-binary conversion

Converting decimal numbers into binary uses the reverse steps. For example, the decimal number 3 in binary format is calculated by having a 1 in the 20 place and a 1 in the 21 place. If you add the 1 from the 20 place and the 1 from the 21 place, you get 3. In other words, 1 + 2 = 3. The binary format translates to 11.

Let's look at a more challenging example. The number 120 in decimal is represented as 1111000 in binary. The following table demonstrates why this is true:

Power of two

27

26

25

24

23

22

21

20

Decimal space

128

64

32

16

8

4

2

1

Binary bits

0

1

1

1

1

0

0

0

In the diagram, 20 to 27 translate to decimal digits from 0 to 128. When converting decimal to binary, work from left to right, and use the first binary number that is equal to or smaller than the number -- in this case, 120.

Because 128 is larger than the target number of 120, the first binary digit that's on is the 64-bit digit. Next, subtract 64 from 120 to determine the remainder that still needs to be converted -- 56. Move to the next space to the right of 64, which is 32. Because 56 is larger than 32, flip that bit on, and subtract 32 from 56, which results in 24.

Again, move right one space to the 16 bit. The number 24 is larger than 16. So, flip that bit on, and subtract 16 from 24, which results in 8. Move right once more to the 8 bit. The decimal number 8 fits perfectly with the 8 bit left in binary. Flip that bit on, and turn all remaining bits -- 4, 2 and 1 -- off.

IPv4 addressing and binary

Like other computing systems, a network sends all data in a binary format. The network devices that transfer this data -- i.e., computer, server, router or switch -- require three critical pieces of information:

  1. IP address
  2. subnet mask
  3. default gateway

A Dynamic Host Configuration Protocol server could send that information to the devices automatically, or a network administrator may provide it manually. In that case, the administrator needs to use both binary and decimal.

Let's look at a few networking examples, using IPv4 addresses. It's important to note that IPv4 addresses are represented by four equal numbers from 0 to 255 that are separated by a dot (.). This format is often referred to as dotted decimal.

Once a network administrator enters the addresses as decimals, the computing device immediately converts the number octets into binary. Depending on how the network is subnetted, some of these numbers may not be available for device addressing, as they may be allocated for broadcasting purposes.

In our example, an IPv4 network device has received the following network configuration information:

  • IPv4 address: 1.1.1.1
  • subnet mask: 255.255.255.0
  • default gateway: 1.1.1.254

The computer converts this information from decimal to binary and then calculates the network ID.

Convert IPv4 dotted decimal to binary

Let's first convert the 1.1.1.1 IP address to binary. Take the octets -- the numbers between the decimal points -- one at a time, like this:

  • 1 decimal = 00000001 binary
  • 1 decimal = 00000001 binary
  • 1 decimal = 00000001 binary
  • 1 decimal = 00000001 binary

An IPv4 address is 32 bits in binary, so each octet is 8 bits in length. Because of this, we need to pad the leading bits as 0s, as shown above. So, the IPv4 address in binary is 00000001 00000001 00000001 00000001.

Convert subnet mask to binary

To convert the subnet mask of 255.255.255.0, take each octet one at a time, using the following table:

Power of two

27

26

25

24

23

22

21

20

Decimal space

128

64

32

16

8

4

2

1

Start with 27, which equals 128, and calculate how many 128s are in 255. The answer is 1, so we then subtract 128 from 255, resulting in 127.

Next, determine how many 64s -- the next binary space to the right -- are in 127. The answer is 1, so subtract 64 from 127 to get 63 as the remainder. Continue the process by moving to the right of the table, like this:

  • 63 - 32 = 31
  • 31 - 16 = 15
  • 15 - 8 = 7
  • 7 - 4 = 3
  • 3 - 2 = 1
  • 1 - 1 = 0
A helpful fact to remember is eight 1s in binary equals 255 in decimal.

The result of converting 255 into binary is 11111111. Notice how all eight binary spaces are flipped on. A helpful fact to remember is eight 1s in binary equals 255 in decimal.

The last octet of the 255.255.255.0 subnet mask is 0. While the decimal 0 is also 0 in binary, write 00000000 to fill all 8 bits of the octet.

The 255.255.255.0 subnet mask in binary is 11111111 11111111 11111111 00000000.

Calculate the network ID and default gateway

The network device on which the IPv4 addressing information is configured must know what the network ID is. The network ID tells the device to which local network it belongs.

If the device's destination IPv4 address with which the device is trying to communicate isn't on its local network, that traffic is sent to a default gateway. A default gateway is used only if the destination for the traffic a device is sending isn't on the local network.

Calculate the network ID

To calculate the network ID, start with the IPv4 address and subnet mask in binary, from above:

  • IP address: 00000001 00000001 00000001 00000001
  • subnet mask: 11111111 11111111 11111111 00000000

Perform a logical AND function on these by looking at each corresponding number in the octets and calculating an answer based on the following rules:

  • 0 and 0 = 0
  • 0 and 1 = 0
  • 1 and 1 = 1

Using the AND rules, look at the first 0 in the IPv4 address and the first 1 in the subnet mask. According to the rules, a 0 and a 1 equal 0. This results in a 0 for the first bit of the network ID. To continue, go from left to right for each number, and calculate using the AND rules.

The calculations result in the following:

  • IP address: 00000001 00000001 00000001 00000001
  • subnet mask: 11111111 11111111 11111111 00000000
  • network ID: 00000001 00000001 00000001 00000000

Notice that the only difference between the IPv4 address and network ID is in the last number of the last octet.

To get the network ID in decimal, use the standard binary-to-decimal conversion from above. The 00000001 in binary is converted to 1 in decimal. The last octet, 00000000, is converted to 0. This makes 1.1.1.0 the network ID.

When the network device from above wants to communicate with a host that has the IPv4 address of 1.1.1.200, it compares this with its network ID. In this example, it discovers this host is on the local network. The network device can then communicate directly with it because it's on the 1.1.1.0 network.

If the network device wants to communicate with host 1.1.2.1, however, it finds this isn't on its local 1.1.1.0 network and sends the traffic to the default gateway.

Calculate default gateway

To calculate the example default gateway written in the decimal IPv4 format of 1.1.1.254, perform the decimal-to-binary conversion, using the binary conversion chart. When the conversion is completed, the default gateway is 00000001 00000001 00000001 11111110.

Conversion summary

Here is an overview of what we've discussed:

  • Binary is a numbering system using only 1 and 0.
  • Use the binary conversion chart to convert from binary to decimal and back.
  • Convert binary to decimal by starting on the right-hand side. Use a 1 to represent bits that are flipped on and a 0 for bits that are turned off.
  • Convert decimal to binary by starting on the left-hand side. Subtract the largest decimal number possible in the binary spaces until no more decimal numbers remain.
  • A network device or computer calculates the network ID using the IPv4 address and subnet mask it is provided.
  • To calculate the network ID, the computer performs a math calculation called a logical AND function.
  • A default gateway isn't required for a device that won't communicate outside its local network.

This article was originally written by David Davis and expanded by Andrew Froehlich.

Next Steps

What's the difference between a MAC address and IP address?

Dig Deeper on Network infrastructure