Date: Thu, 8 Aug 2013 01:27:10 -0700 From: Peter Wemm <peter@wemm.org> To: s m <sam.gh1986@gmail.com> Cc: FreeBSD Net <freebsd-net@freebsd.org> Subject: Re: how calculate the number of ip addresses in a range? Message-ID: <CAGE5yCoBYS2%2BsHoQXZvWMNPTQeEEtDX7LAKSBHJAZ4_rA2-byQ@mail.gmail.com> In-Reply-To: <CAA_1SgEEeyCOk%2Bi9Zp725RfQ9s0tpELXL0SSBeiN%2B60z%2BxqYUg@mail.gmail.com> References: <CAA_1SgEEeyCOk%2Bi9Zp725RfQ9s0tpELXL0SSBeiN%2B60z%2BxqYUg@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Aug 8, 2013 at 12:04 AM, s m <sam.gh1986@gmail.com> wrote: > hello guys, > > i have a question about ip addresses. i know my question is not related to > freebsd but i googled a lot and found nothing useful and don't know where i > should ask my question. > > i want to know how can i calculate the number of ip addresses in a range? > for example if i have 192.0.0.1 192.100.255.254 with mask 8, how many ip > addresses are available in this range? is there any formula to calculate > the number of ip addresses for any range? > > i'm confusing about it. please help me to clear my mind. > thanks in advance, My immediate reaction is.. is this a homework / classwork / assignment? Anyway, you can think of it by converting your start and end addresses to an integer. Over simplified: $ cat homework.c main() { int start = (192 << 24) | (0 << 16) | (0 << 8) | 1; int end = (192 << 24) | (100 << 16) | (255 << 8) | 254; printf("start %d end %d range %d\n", start, end, (end - start) + 1); } $ ./homework start -1073741823 end -1067122690 range 6619134 The +1 is correcting for base zero. 192.0.0.1 - 192.0.0.2 is two usable addresses. I'm not sure what you want to do with the mask of 8. You can also do it with ntohl(inet_addr("address")) as well and a multitude of other ways. -- Peter Wemm - peter@wemm.org; peter@FreeBSD.org; peter@yahoo-inc.com; KI6FJV UTF-8: for when a ' just won\342\200\231t do. <brueffer> ZFS must be the bacon of file systems. "everything's better with ZFS"
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAGE5yCoBYS2%2BsHoQXZvWMNPTQeEEtDX7LAKSBHJAZ4_rA2-byQ>