MarzleyTech Learn

Home / Learn / Networking / Binary and hexadecimal for networking: conversions by hand, mask values, powers of two and hex in MAC/IPv6

Binary and hexadecimal for networking: conversions by hand, mask values, powers of two and hex in MAC/IPv6

Computers store everything as bits: 0 or 1. An IPv4 address like 192.168.1.10 is really 32 bits, and a subnet mask like 255.255.255.0 is 32 bits too. Subnetting, which is the skill every network engineer needs (and every networking exam tests), is just counting and switching bits. Once binary feels natural, subnetting becomes easy.

Hexadecimal (hex) is a shorter way to write binary. You'll see it in MAC addresses (3C:52:82:1A:0F:9B), IPv6 addresses (2001:db8::1), colour codes in web design (#FF6600) and error codes.

Why binary?

Electronics are reliable at telling two states apart: voltage on/off, light on/off, magnetised one way or the other. So computers count in base 2 (two digits: 0 and 1) instead of base 10.

TermSize
BitOne 0 or 1
Nibble4 bits (one hex digit)
Byte / octet8 bits
IPv4 address32 bits = 4 octets
MAC address48 bits = 6 bytes
IPv6 address128 bits

Networking says octet for 8 bits because historically "byte" wasn't always 8 bits.

Place values in one octet

In decimal, each position is worth 10× the one to its right (1, 10, 100...). In binary, each position is worth 2×:

1286432168421
2⁷2⁶2⁵2⁴2³2²2¹2⁰

All eight bits on = 128+64+32+16+8+4+2+1 = 255, which is why each octet of an IP address goes from 0 to 255.

Binary to decimal

Add the place values wherever there's a 1.

BinaryWorkingDecimal
11000000128 + 64192
10101000128 + 32 + 8168
000010108 + 210
11111111all255
0111111164+32+16+8+4+2+1127

So 11000000.10101000.00000001.00001010 = 192.168.1.10.

Decimal to binary (subtraction method)

Go through the place values from 128 down. If the value fits into what's left, write 1 and subtract; otherwise write 0.

Convert 200:

PlaceFits in remaining?BitRemaining
128128 ≤ 200 yes172
6464 ≤ 72 yes18
32no08
16no08
88 ≤ 8 yes10
4no00
2no00
1no00

200 = 11001000.

Check yourself with Python:

Python · runs live in the interactive lesson
def to_binary(n):
    bits = ""
    for place in [128, 64, 32, 16, 8, 4, 2, 1]:
        if place <= n:
            bits += "1"
            n -= place
        else:
            bits += "0"
    return bits

for n in [200, 172, 10, 255, 99]:
    print(n, "=", to_binary(n), "| check:", format(n, "08b"))

ip = "192.168.1.10"
print(".".join(to_binary(int(o)) for o in ip.split(".")))

Numbers to memorise: mask values

Subnet masks are made of 1s followed by 0s. These are the only values a mask octet can take:

BinaryDecimalBits onBlock size (256 − value)
0000000000256
100000001281128
11000000192264
11100000224332
11110000240416
1111100024858
1111110025264
1111111025472
1111111125581

Trick: each value is the previous one plus half the remaining gap: 128, +64 = 192, +32 = 224, +16 = 240, +8 = 248, +4 = 252, +2 = 254, +1 = 255.

Powers of two

n2ⁿn2ⁿ
129512
24101,024
38112,048
416124,096
532138,192
6641416,384
71281532,768
82561665,536

Why they matter:

  • Host bits h give 2ʰ addresses, of which 2ʰ − 2 are usable hosts (one for the network, one for broadcast). 8 host bits → 256 addresses → 254 hosts.
  • Borrowed bits b give 2ᵇ subnets. Borrow 3 bits → 8 subnets.

Hexadecimal

Hex is base 16: digits 0–9, then A=10, B=11, C=12, D=13, E=14, F=15. One hex digit is exactly 4 bits, so one byte is two hex digits.

HexBinaryDecimalHexBinaryDecimal
000000810008
100011910019
200102A101010
300113B101111
401004C110012
501015D110113
601106E111014
701117F111115

Converting

  • Hex → binary: replace each digit with its 4 bits. C0 → 1100 0000.
  • Binary → hex: split into groups of 4 from the right. 1010 1000 → A8.
  • Hex → decimal: first digit × 16 + second digit. A8 = 10×16 + 8 = 168. FF = 15×16 + 15 = 255.
  • Decimal → hex: divide by 16; the quotient is the first digit, the remainder the second. 192 ÷ 16 = 12 remainder 0 → C0.
Python · runs live in the interactive lesson
for n in [192, 168, 10, 255, 172]:
    print(n, "-> hex", format(n, "02X"), "-> binary", format(n, "08b"))
print("Hex A8 is", int("A8", 16), "in decimal")

Where hex appears in networking

MAC addresses: 48 bits written as 6 pairs of hex digits: 3C:52:82:1A:0F:9B (Windows shows 3C-52-82-1A-0F-9B; Cisco shows 3c52.821a.0f9b). The first 3 bytes (the OUI) identify the manufacturer.

IPv6 addresses: 128 bits written as 8 groups of 4 hex digits: 2001:0db8:0000:0000:0000:0000:0000:0001, shortened to 2001:db8::1 (the IPv6 lesson explains the rules).

Try the converter

Type in any box; the others update. The row shows which bits are on.

This part of the lesson has an interactive tool. Open the live lesson to use it.

Practice

Convert without a calculator, then check with the converter:

  1. 11110000 → decimal
  2. 172 → binary
  3. 100 → binary
  4. 0x3F → decimal
  5. 224 → hex
  6. 10111111 → decimal
Think about it: Answers to the practice questions above?Show answer

1) 240. 2) 10101100. 3) 01100100. 4) 3×16 + 15 = 63. 5) E0. 6) 128+32+16+8+4+2+1 = 191.

Summary

  • Computers use binary because two states are reliable; an octet is 8 bits with place values 128, 64, 32, 16, 8, 4, 2, 1.
  • Binary → decimal: add the places with 1s; decimal → binary: subtract from 128 down.
  • Memorise mask values 128, 192, 224, 240, 248, 252, 254, 255 and powers of two.
  • h host bits give 2ʰ − 2 usable hosts; b borrowed bits give 2ᵇ subnets.
  • Hex digits are 4 bits each (A=10 ... F=15); hex appears in MAC and IPv6 addresses.

Check yourself

  1. What is binary 11100000 in decimal?

    Show answer

    224

  2. What is binary 00001010 in decimal?

    Show answer

    10

  3. Write 172 in binary (8 bits).

    Show answer

    10101100 · 128 + 32 + 8 + 4

  4. Write 255 in hexadecimal.

    Show answer

    FF

  5. How many bits are turned on in the mask octet 252?

    Show answer

    6

  6. What is 2 to the power of 10?

    Show answer

    1024

  7. How many usable hosts do 5 host bits give?

    Show answer

    30 · 2^5 - 2

Learn more: Khan Academy: binary numbers · Cisco binary game

Lesson 4 of 24 in Networking · Printable course notes