IPv4 subnet calculator

Calculate network and broadcast addresses, masks, ranges and address counts for an IPv4 prefix.

Enter an IPv4 address and CIDR prefix. The result includes subnet boundaries and masks. For /31, both addresses are counted as usable on a point-to-point link; /32 represents one address.

The calculation uses classless CIDR prefixes and makes no A/B/C class assumptions.

Method and assumptions

Method version 1.0.0 · Updated

Calculate an IPv4 subnet

Formula: Network address = IPv4 address AND subnet mask; broadcast address = network OR wildcard mask.

Enter an IPv4 address and CIDR prefix. The result includes subnet boundaries and masks. For /31, both addresses are counted as usable on a point-to-point link; /32 represents one address.

address
IPv4 address
prefix
CIDR prefix length

IPv4 only: valid dotted-decimal address and CIDR prefix from /0 to /32. Conventional usable-host counts exclude network and broadcast for /0 to /30; /31 uses the point-to-point case and /32 denotes one address.

Calculate with code

These short examples use the same inputs and core calculation as this tool. Adapt validation and formatting to your application.

JavaScript
const octets = '192.168.1.130'.split('.').map(Number);
const ip = octets.reduce((n, octet) => (n << 8n) | BigInt(octet), 0n);
const mask = (0xFFFFFFFFn << 8n) & 0xFFFFFFFFn; // /24
const network = ip & mask;
const format = n => [24n, 16n, 8n, 0n].map(s => Number((n >> s) & 255n)).join('.');
console.log(format(network)); // 192.168.1.0
TypeScript
const octets = '192.168.1.130'.split('.').map(Number);
const ip = octets.reduce<bigint>((n, octet) => (n << 8n) | BigInt(octet), 0n);
const mask = (0xFFFFFFFFn << 8n) & 0xFFFFFFFFn; // /24
const network = ip & mask;
const format = (n: bigint) => [24n, 16n, 8n, 0n].map(s => Number((n >> s) & 255n)).join('.');
console.log(format(network)); // 192.168.1.0
Python
from ipaddress import ip_network

network = ip_network('192.168.1.130/24', strict=False)
print(network.network_address)  # 192.168.1.0
print(network.broadcast_address)  # 192.168.1.255
Java
long ip = 0;
for (String octet : "192.168.1.130".split("\\.")) {
    ip = (ip << 8) | Integer.parseInt(octet);
}
long mask = (0xFFFFFFFFL << 8) & 0xFFFFFFFFL; // /24
long network = ip & mask;
System.out.printf("%d.%d.%d.%d%n", (network >>> 24) & 255, (network >>> 16) & 255,
    (network >>> 8) & 255, network & 255); // 192.168.1.0
C#
var bytes = System.Net.IPAddress.Parse("192.168.1.130").GetAddressBytes();
for (var i = 0; i < bytes.Length; i++) bytes[i] &= (i < 3 ? (byte)255 : (byte)0); // /24
Console.WriteLine(new System.Net.IPAddress(bytes)); // 192.168.1.0
More languages
C
#include <arpa/inet.h>
#include <stdio.h>
#include <stdint.h>

struct in_addr ip; char out[INET_ADDRSTRLEN];
inet_pton(AF_INET, "192.168.1.130", &ip);
uint32_t network = ntohl(ip.s_addr) & 0xFFFFFF00u; // /24
ip.s_addr = htonl(network); inet_ntop(AF_INET, &ip, out, sizeof out);
puts(out); // 192.168.1.0
C++
#include <arpa/inet.h>
#include <cstdint>
#include <iostream>

struct in_addr ip{}; char out[INET_ADDRSTRLEN];
inet_pton(AF_INET, "192.168.1.130", &ip);
ip.s_addr = htonl(ntohl(ip.s_addr) & 0xFFFFFF00u); // /24
inet_ntop(AF_INET, &ip, out, sizeof out); std::cout << out; // 192.168.1.0
PHP
$bytes = inet_pton('192.168.1.130');
$ip = unpack('N', $bytes)[1] & 0xFFFFFF00; // /24
echo inet_ntop(pack('N', $ip)); // 192.168.1.0
PowerShell
$bytes = [Net.IPAddress]::Parse('192.168.1.130').GetAddressBytes()
$bytes[3] = 0 # /24: clear host octet
[Net.IPAddress]::new($bytes) # 192.168.1.0

Learn about IP protocols