Converting Decimal and Binary Values
Converting Between Decimal and Binary for IP Addresses
Introduction
- Converting between decimal and binary is crucial for understanding IP addresses and networking concepts.
- This skill is essential for working with IP ranges, prefixes, subnet masks, and routing.
Why Understand Decimal to Binary Conversion?
- Human-readable IP address (Dotted Decimal Notation):
- Example: 133.330.330.7
- Four decimal numbers (0-255) separated by periods.
- Computer-readable IP address (Binary):
- A 32-bit binary number.
- Four sets of eight bits (an octet or byte).
- Each byte corresponds to the decimal number in the dotted decimal notation.
Converting Decimal to Binary
- More complex than binary to decimal.
- Tackle each decimal number individually.
Process
Introduce the Binary Math Table:
Position 1 2 3 4 5 6 7 8 Value 128 64 32 16 8 4 2 1 - Each position corresponds to a value in decimal.
- A binary "1" in that position has the associated decimal value.
- Example: A "1" in position 1 is 128 in decimal; a "1" in position 4 is 16 in decimal.
Rules for Conversion:
- Move from left to right (position 1 to position 8).
- Rule 1: If the decimal number is smaller than the corresponding binary position value, write "0" in that position and move to the next.
- Rule 2: If the decimal number is greater than or equal to the binary position value:
- Subtract the binary position value from the decimal number.
- Write "1" in that position.
- Move to the next position.
Example: Converting 133 to Binary:
- Start at position 1 (128).
- 133 is greater than 128, so apply Rule 2.
- Write "1" in position 1.
- Move to position 2 (64).
- 5 is smaller than 64, so apply Rule 1.
- Write "0" in position 2.
- Repeat for positions 3, 4, and 5 (32, 16, and 8).
- 5 is smaller than all, so write "0" in each.
- Move to position 6 (4).
- 5 is greater than 4, so apply Rule 2.
- Write "1" in position 6.
- Move to position 7 (2).
- 1 is smaller than 2, so apply Rule 1.
- Write "0" in position 7.
- Move to position 8 (1).
- 1 is equal to 1, so apply Rule 2.
- Write "1" in position 8.
- Result: 133 in decimal is 10000101 in binary.
Example: Converting 33 to Binary:
- Start at position 1 (128).
- 33 is less than 128. Rule 1: Write "0", move to position 2.
- Position 2 (64):
- 33 is less than 64. Rule 1: Write "0", move to position 3.
- Position 3 (32):
- 33 is not less than 32. Rule 2: 33 - 32 = 1. Write "1", move to position 4.
- Positions 4, 5, 6, 7 (16, 8, 4, 2):
- 1 is less than all. Rule 1: Write "0" in each.
- Position 8 (1):
- 1 is not less than 1. Rule 2: 1 - 1 = 0. Write "1".
- Result: 33 in decimal is 00100001 in binary.
- Start at position 1 (128).
Example: Converting 7 to Binary:
- Result: 7 in decimal is 00000111 in binary.
Converting Binary to Decimal
- Much easier than decimal to binary.
Process
Break the IP Address into Octets:
- Each eight-bit binary value corresponds to a decimal number.
- Example:
- Binary: 10000101.00100001.00100001.00000111
- Decimal: 133.33.33.7
Use the Binary Math Table:
Position 1 2 3 4 5 6 7 8 Value 128 64 32 16 8 4 2 1 Iterate Through Each Octet:
- If there's a "1", add the corresponding decimal value from the table.
- If there's a "0", add zero.
Example:
- Binary: 10000101
- Calculation:
Repeat for All Octets:
- Binary: 00100001
- Calculation:
Final Result:
- Combine the decimal values to get the dotted decimal notation: 133.33.33.7
Practice
- Pick random IP addresses and convert them back and forth to solidify your understanding.