Python Encyclopedia for Academics
  • Course Outline
  • Artificial Intelligence
    • Data Science Foundation
      • Python Programming
        • Introduction and Basics
          • Variables
          • Print Function
          • Input From User
          • Data Types
          • Type Conversion
        • Operators
          • Arithmetic Operators
          • Relational Operators
          • Bitwise Operators
          • Logical Operators
          • Assignment Operators
          • Compound Operators
          • Membership Operators
          • Identity Operators
      • Numpy
        • Vectors, Matrix
        • Operations on Matrix
        • Mean, Variance, and Standard Deviation
        • Reshaping Arrays
        • Transpose and Determinant of Matrix
      • Pandas
        • Series and DataFrames
        • Slicing, Rows, and Columns
        • Operations on DataFrames
        • Different wayes to creat DataFrame
        • Read, Write Operations with CSV files
      • Matplotlib
        • Graph Basics
        • Format Strings in Plots
        • Label Parameters, Legend
        • Bar Chart, Pie Chart, Histogram, and Scatter Plot
  • Machine Learning Algorithms
    • Regression Analysis In ML
      • Regression Analysis in Machine Learning
      • Proof of Linear Regression Formulas
      • Simple Linear Regression Implementation
      • Multiple Linear Regression
      • Advertising Dataset Example
      • Bike Sharing Dataset
      • Wine Quality Dataset
      • Auto MPG Dataset
    • Classification Algorithms in ML
      • Proof of Logistic Regression
      • Simplified Mathematical Proof of SVM
      • Iris Dataset
  • Machine Learning Laboratory
    • Lab 1: Titanic Dataset
      • Predicting Survival on the Titanic with Machine Learning
    • Lab 2: Dow Jones Index Dataset
      • Dow Jones Index Predictions Using Machine Learning
    • Lab 3: Diabetes Dataset
      • Numpy
      • Pandas
      • Matplotlib
      • Simple Linear Regression
      • Simple Non-linear Regression
      • Performance Matrix
      • Preprocessing
      • Naive Bayes Classification
      • K-Nearest Neighbors (KNN) Classification
      • Decision Tree & Random Forest
      • SVM Classifier
      • Logistic Regression
      • Artificial Neural Network
      • K means Clustering
    • Lab 4: MAGIC Gamma Telescope Dataset
      • Classification in ML-MAGIC Gamma Telescope Dataset
    • Lab 5: Seoul Bike Sharing Demand Dataset
      • Regression in ML-Seoul Bike Sharing Demand Dataset
    • Lab 6: Medical Cost Personal Datasets
      • Predict Insurance Costs with Linear Regression in Python
    • Lab 6: Predict The S&P 500 Index With Machine Learning And Python
      • Predict The S&P 500 Index With Machine Learning And Python
  • Artificial Neural Networks
    • Biological Inspiration vs. Artificial Neurons
    • Review linear algebra and calculus essentials for ANNs
    • Activation Function
  • Mathematics
    • Pre-Calculus
      • Factorials
      • Roots of Polynomials
      • Complex Numbers
      • Polar Coordinates
      • Graph of a Function
    • Calculus 1
      • Limit of a Function
      • Derivative of Function
      • Critical Points
      • Indefinite Integrals
  • Calculus 2
    • 3D Coordinates and Vectors
    • Vectors and Vector Operations
    • Lines and Planes in Space (3D)
    • Partial Derivatives
    • Optimization Problems (Maxima/Minima) in Multivariable Functions
    • Gradient Vectors
  • Engineering Mathematics
    • Laplace Transform
  • Electrical & electronics Eng
    • Resistor
      • Series Resistors
      • Parallel Resistors
    • Nodal Analysis
      • Example 1
      • Example 2
    • Transient State
      • RC Circuit Equations in the s-Domain
      • RL Circuit Equations in the s-Domain
      • LC Circuit Equations in the s-Domain
      • Series RLC Circuit with DC Source
  • Computer Networking
    • Fundamental
      • IPv4 Addressing
      • Network Diagnostics
  • Cybersecurity
    • Classical Ciphers
      • Caesar Cipher
      • Affine Cipher
      • Atbash Cipher
      • Vigenère Cipher
      • Gronsfeld Cipher
      • Alberti Cipher
      • Hill Cipher
Powered by GitBook
On this page
  • What is IPv4?
  • Step 1: Understand IP Classes (Classful Addressing)
  • Step 2: Practical Example
  • Step 3: Convert Prefix /28 to Subnet Mask
  • Step 4: Calculate the Network Address
  • Step 5: Calculate Broadcast Address
  • Step 6: Find First and Last Host
  • Step 7: Total Number of Hosts
  • Summary
  • Step 8: Python Code for an IP Calculator
  • Keywords
  1. Computer Networking
  2. Fundamental

IPv4 Addressing

Nerd Cafe

What is IPv4?

IPv4 (Internet Protocol version 4) is the fourth version of the Internet Protocol (IP). It uses a 32-bit address format, typically represented as four decimal numbers separated by dots (e.g., 192.168.1.1).

Each IP address has two main parts:

  • Network portion

  • Host portion

Step 1: Understand IP Classes (Classful Addressing)

Class
Starting IP
Ending IP
Default Subnet Mask
Usage

A

0.0.0.0

127.255.255.255

255.0.0.0

Large networks

B

128.0.0.0

191.255.255.255

255.255.0.0

Medium networks

C

192.0.0.0

223.255.255.255

255.255.255.0

Small networks

D

224.0.0.0

239.255.255.255

N/A (Multicast)

Multicast

E

240.0.0.0

255.255.255.255

N/A (Reserved)

Reserved

Step 2: Practical Example

Let’s take this IP address as an example: 192.168.10.17/28

Find the Class:

  • Starts with 192 ⇒ Class C

  • Default mask of Class C = 255.255.255.0

Classful or Classless?

  • /28 is a custom prefix ⇒ This is classless addressing (CIDR - Classless Inter-Domain Routing)

Step 3: Convert Prefix /28 to Subnet Mask

  • /28 means 28 bits are for the network ⇒ Subnet mask is:

11111111.11111111.11111111.11110000

In decimal ⇒ 255.255.255.240

Step 4: Calculate the Network Address

  1. Convert IP and subnet mask to binary:

    • IP: 192.168.10.17 → 11000000.10101000.00001010.00010001

    • Subnet mask: 255.255.255.240 → 11111111.11111111.11111111.11110000

  2. Perform bitwise AND:

11000000.10101000.00001010.00010001
AND
11111111.11111111.11111111.11110000
=
11000000.10101000.00001010.00010000
⇒ 192.168.10.16

Network Address = 192.168.10.16

Step 5: Calculate Broadcast Address

  • Invert the subnet mask:

00000000.00000000.00000000.00001111 → 0.0.0.15

Add this to the network address:

192.168.10.16 + 0.0.0.15 = 192.168.10.31

Broadcast Address = 192.168.10.31

Step 6: Find First and Last Host

  • First Host = Network Address + 1 = 192.168.10.17

  • Last Host = Broadcast Address - 1 = 192.168.10.30

Step 7: Total Number of Hosts

  • Formula: 2^(32 - subnet_prefix) - 2

  • ⇒ 2^(32 - 28) - 2 = 2^4 - 2 = 14

Total usable hosts = 14

Summary

Item
Value

IP Address

192.168.10.17/28

Class

C

Subnet Mask

255.255.255.240

Network Address

192.168.10.16

Broadcast Address

192.168.10.31

First Host

192.168.10.17

Last Host

192.168.10.30

Total Hosts

14

Classful?

❌ Classless

Step 8: Python Code for an IP Calculator

Here’s a simple Python tool using the ipaddress module:

import ipaddress

def ip_calculator():
    try:
        # Get IP address and subnet mask from user
        ip = input("Enter IP address (e.g. 192.168.10.17): ").strip()
        subnet_mask = input("Enter Subnet Mask (e.g. 255.255.255.240): ").strip()

        # Create network using netmask
        network = ipaddress.IPv4Network(f"{ip}/{subnet_mask}", strict=False)

        print("\n--- IP Address Information ---")
        print(f"IP Address: {ip}")
        print(f"Subnet Mask: {network.netmask}")
        print(f"Prefix Length: /{network.prefixlen}")
        print(f"Network Address: {network.network_address}")
        print(f"Broadcast Address: {network.broadcast_address}")
        print(f"Total Hosts: {network.num_addresses - 2}")
        print(f"First Host: {list(network.hosts())[0]}")
        print(f"Last Host: {list(network.hosts())[-1]}")

        # Determine IP class
        first_octet = int(ip.split('.')[0])
        if 1 <= first_octet <= 126:
            ip_class = "A"
            default_prefix = 8
        elif 128 <= first_octet <= 191:
            ip_class = "B"
            default_prefix = 16
        elif 192 <= first_octet <= 223:
            ip_class = "C"
            default_prefix = 24
        elif 224 <= first_octet <= 239:
            ip_class = "D (Multicast)"
            default_prefix = None
        else:
            ip_class = "E (Reserved)"
            default_prefix = None

        print(f"Class: {ip_class}")
        if default_prefix:
            is_classless = network.prefixlen != default_prefix
        else:
            is_classless = True

        print(f"Classless Addressing: {'Yes' if is_classless else 'No'}")

    except Exception as e:
        print(f"Error: {e}")

# Run the calculator
ip_calculator()

Output

Enter IP address (e.g. 192.168.10.17):  172.16.11.24
Enter Subnet Mask (e.g. 255.255.255.240):  255.240.0.0

--- IP Address Information ---
IP Address: 172.16.11.24
Subnet Mask: 255.240.0.0
Prefix Length: /12
Network Address: 172.16.0.0
Broadcast Address: 172.31.255.255
Total Hosts: 1048574
First Host: 172.16.0.1
Last Host: 172.31.255.254
Class: B
Classless Addressing: Yes

Keywords

IPv4 addressing, subnet mask, IP calculator, network address, broadcast address, IP class, classful addressing, classless addressing, CIDR, prefix length, total hosts, first host, last host, IP range, IPv4 classes, netmask, private IP, public IP, IP subnetting, IP analysis, nerd cafe

PreviousFundamentalNextNetwork Diagnostics

Last updated 1 month ago