
IPv6 subnet calculator
Calculate the network prefix, address range and address count for an IPv6 address and CIDR prefix.
The endpoints describe the mathematical extent of the prefix; they do not mean every address is assignable. IPv6 has no broadcast address.
Method and assumptions
Method version 1.0.0 · Updated
Calculate an IPv6 prefix
Formula: Network address = IPv6 address AND prefix mask; address count = 2^(128 − prefix).
The result gives the network prefix, its mathematical endpoints and total address count. IPv6 has no broadcast address; these endpoints do not imply local assignment rules.
- address
- IPv6 address
- prefix
- CIDR prefix length
Hexadecimal IPv6 addresses with :: compression and CIDR prefixes from /0 to /128. Zone identifiers and embedded IPv4 forms are not accepted.
Calculate with code
These short examples use the same inputs and core calculation as this tool. Adapt validation and formatting to your application.
JavaScript
// npm install ipaddr.js
import ipaddr from 'ipaddr.js';
const [address, prefix] = ipaddr.parseCIDR('2001:db8:1234::1/64');
const bytes = address.toByteArray();
for (let i = 0; i < bytes.length; i++) {
const bits = Math.max(0, Math.min(8, prefix - i * 8));
bytes[i] &= (0xFF << (8 - bits)) & 0xFF;
}
console.log(ipaddr.fromByteArray(bytes).toString()); // 2001:db8:1234::TypeScript
// npm install ipaddr.js
import ipaddr from 'ipaddr.js';
const [address, prefix] = ipaddr.parseCIDR('2001:db8:1234::1/64');
const bytes = address.toByteArray();
for (let i = 0; i < bytes.length; i++) {
const bits = Math.max(0, Math.min(8, prefix - i * 8));
bytes[i] = bytes[i]! & ((0xFF << (8 - bits)) & 0xFF);
}
console.log(ipaddr.fromByteArray(bytes).toString()); // 2001:db8:1234::Python
from ipaddress import ip_network
network = ip_network('2001:db8:1234::1/64', strict=False)
print(network.network_address) # 2001:db8:1234::Java
import java.math.BigInteger;
import java.net.InetAddress;
var address = InetAddress.getByName("2001:db8:1234::1").getAddress();
var value = new BigInteger(1, address);
var mask = BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE.shiftLeft(64));
var network = value.and(mask).toByteArray();
var sixteenBytes = new byte[16];
System.arraycopy(network, Math.max(0, network.length - 16), sixteenBytes,
Math.max(0, 16 - network.length), Math.min(16, network.length));
System.out.println(InetAddress.getByAddress(sixteenBytes).getHostAddress()); // 2001:db8:1234:0:0:0:0:0C#
var bytes = System.Net.IPAddress.Parse("2001:db8:1234::1").GetAddressBytes();
for (var i = 0; i < bytes.Length; i++) {
var bits = Math.Max(0, Math.Min(8, 64 - i * 8));
bytes[i] &= (byte)((0xFF << (8 - bits)) & 0xFF);
}
Console.WriteLine(new System.Net.IPAddress(bytes)); // 2001:db8:1234::More languages
C
#include <arpa/inet.h>
#include <stdio.h>
struct in6_addr ip; char out[INET6_ADDRSTRLEN];
inet_pton(AF_INET6, "2001:db8:1234::1", &ip);
for (int i = 8; i < 16; i++) ip.s6_addr[i] = 0; // /64
inet_ntop(AF_INET6, &ip, out, sizeof out);
puts(out); // 2001:db8:1234::C++
#include <arpa/inet.h>
#include <iostream>
struct in6_addr ip{}; char out[INET6_ADDRSTRLEN];
inet_pton(AF_INET6, "2001:db8:1234::1", &ip);
for (int i = 8; i < 16; ++i) ip.s6_addr[i] = 0; // /64
inet_ntop(AF_INET6, &ip, out, sizeof out); std::cout << out; // 2001:db8:1234::PHP
$bytes = inet_pton('2001:db8:1234::1');
$bytes = substr($bytes, 0, 8) . str_repeat(chr(0), 8); // /64
echo inet_ntop($bytes); // 2001:db8:1234::PowerShell
$bytes = [Net.IPAddress]::Parse('2001:db8:1234::1').GetAddressBytes()
for ($i = 8; $i -lt 16; $i++) { $bytes[$i] = 0 } # /64
[Net.IPAddress]::new($bytes) # 2001:db8:1234::