I am new to java and am doing a project using Java.
I have an IP( for ex: 192.168.0.10
) and a subnet mask (let us say, 255.255.255.0
). I need a list of all the ip addresses in this sub-net. What I can think of best is the naive solution(in which I have to deal with bits and all). Does anyone know a better/e.legant way of doing this.
If we consider the example we will have list of ip's as 192.168.0.1. 192.168.0.2, 192.168.0.3, ..... , 192.168.0.254
Any help appreciated.
[Edit] Naive approach I have thought of:
Construct a integer by considering the ip address you have and the subnet. In our case it would be this in binary representation 11000000.10101000.00000000.00000000
(dots are added for readability). Increment this number by 1 till the 24 bits from left toggles. The first tile it toggles we have generated all numbers which can be possible ip in this subnet. Now the task is to generate ip from a 32 bit number which can be done by writing a function which extracts 8 bits at a time.