Java: IPv4 addresses, masks, and CIDR blocks
Compute the network address, broadcast address, and host count of a subnet with bitwise operations.
class Main {
static int toInt(String ip) {
int value = 0;
for (String part : ip.split("\\.")) {
value = (value << 8) | Integer.parseInt(part);
}
return value;
}
static String toText(int value) {
return ((value >>> 24) & 255) + "." + ((value >>> 16) & 255) + "."
+ ((value >>> 8) & 255) + "." + (value & 255);
}
public static void main(String[] args) {
int ip = toInt("192.168.10.77");
int prefix = 26;
int mask = prefix == 0 ? 0 : -1 << (32 - prefix);
int network = ip & mask;
int broadcast = network | ~mask;
System.out.println("network " + toText(network));
System.out.println("broadcast " + toText(broadcast));
System.out.println("hosts " + ((1L << (32 - prefix)) - 2));
}
}network 192.168.10.64 broadcast 192.168.10.127 hosts 62
Explanation
An IPv4 address is a 32-bit number, usually written as four decimal bytes. CIDR notation such as /26 says that the first 26 bits identify the network and the remaining 6 bits identify the host. The subnet mask has ones in the network bits and zeros in the host bits.
Bitwise AND of the address and the mask gives the network address. Setting all host bits gives the broadcast address, and a block with h host bits has 2^h − 2 usable hosts, because the network and broadcast addresses are reserved. Java's int is signed, so the program uses the unsigned shift >>> when printing each byte.
Explore the dry run
class Main { static int toInt(String ip) { int value = 0; for (String part : ip.split("\\.")) { value = (value << 8) | Integer.parseInt(part); } return value; } static String toText(int value) { return ((value >>> 24) & 255) + "." + ((value >>> 16) & 255) + "." + ((value >>> 8) & 255) + "." + (value & 255); } public static void main(String[] args) { int ip = toInt("192.168.10.77"); int prefix = 26; int mask = prefix == 0 ? 0 : -1 << (32 - prefix); int network = ip & mask; int broadcast = network | ~mask; System.out.println("network " + toText(network)); System.out.println("broadcast " + toText(broadcast)); System.out.println("hosts " + ((1L << (32 - prefix)) - 2)); }}Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| Pack the four bytes into one 32-bit integer. | ip = 0xC0A80A4D | — | |
| 26 ones followed by 6 zeros. | mask = 255.255.255.192 | — | |
| Clearing the host bits leaves the block start: 77 falls in the 64–127 block. | network = 192.168.10.64 | — | |
| 64 addresses in the block, minus the network and broadcast addresses. | 2^6 - 2 = 62 | network 192.168.10.64 broadcast 192.168.10.127 hosts 62 |
Cost and common mistakes
O(1): a constant number of bit operations.
- Shifting an int by 32 or more bits does not produce zero; Java uses only the low five bits of the shift count, which is why prefix 0 is handled separately.
- Use >>> for unsigned right shift. >> keeps the sign bit and would print wrong bytes for addresses above 127.x.x.x.
- A /31 and /32 follow special rules, so the '− 2' formula does not apply to them.
Try it yourself
What are the network address and host count for 10.1.2.200/24?
Reveal answer and reasoning
The network address is 10.1.2.0, the broadcast address is 10.1.2.255, and there are 254 usable hosts.