Increment subnet based on how subnets are to be divided e.g. - Java Network

Java examples for Network:Network Address

Description

Increment subnet based on how subnets are to be divided e.g.

Demo Code

/** // ww  w.java  2s.c o m
 * Copyright 2012 InCNTRE, This file is released under Apache 2.0 license except for component libraries under different licenses
http://www.apache.org/licenses/LICENSE-2.0
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int ip = 2;
        int subnet = 2;
        System.out.println(incrementSubnet(ip, subnet));
    }

    /**
     * Increment subnet based on how subnets are to be divided
     * e.g. 192.168.0.0/24 will incremented will be 
     * 192.168.1.0/24  
     * @param ip
     * @param subnet
     * @return the incremented subnet in integer form 
     */
    public static int incrementSubnet(int ip, int subnet) {

        int newIP = 0;

        int raiseTo = (int) Math.pow(2, 32 - subnet);

        newIP = ip + raiseTo;

        return newIP;

    }
}

Related Tutorials