[olug] Subnet mask
Jay Hannah
jay at jays.net
Wed Dec 17 04:16:28 UTC 2003
On Tuesday, December 16, 2003, at 01:10 PM, Justin Hopkins wrote:
> On Tue, Dec 16, 2003 at 12:35:35PM -0600, Daniel Linder wrote:
>> Jay wrote:
>>> Subnets for that range:
>>> http://jays.net/cgi-bin/ipr.pl?ip1=205.202.101.64&ip2=205.202.101.93
>>>
>>> Source code:
>>> http://jays.net/ipr.pl.txt
>>
>> Handy tool Jay, but could you have leveraged "divide by 2" and/or
>> bit-shifting in Perl (never done that myself) rather than using
>> "X/log(2)"
>> mathmatics? (My Calc and binary math are rusty right now, can't
>> remember
>> the relationships between those functions.)
>>
>> Dan
>
> He probably could have. I remember when Jay wrote that thing a few
> years
> ago, and the subject of integers and bit-shift operations came up.
> I think its no mistake that an IPv4 address can be tightly represented
> as a 32-bit integer (just like an IPv6 can be tightly represented as a
> 128-bit integer). I assume all of those highly optimized network
> devices
> do nothing but bitwise and/or/shift operations on their IP addresses
> to make
> their network operations lightning fast.
>
> For some reason, I came up with the log_base2() thing. Change-of-base
> logarithm
> rule from Calc:
>
> log_a(b) = log_x(b) / log_x(a)
>
> because the perl log function is base 10.
>
> log_2(b) = log_10(b) / log_10(2)
>
> I now wait for some genius of bit operations to thoroughly blow us
> away with
> an elegant solution.
Note that to_bin() in the code does use bit-shifting, which Justin
explained to me at the time, and I promptly forgot (years ago -grin-).
I don't remember why I performed all the subnet finding operations in
decimal. I guess that's the only way writing it made sense to me at the
time. Therefore, log_base2() wasn't Justin's fault -- I specifically
asked him how to do that w/o telling him why.
Look, bit-shifting in the code:
sub to_bin {
$_ = $_[0];
my @bs;
unshift @bs, ($_ & 1);
for (my $j = 0; $j < 31; $j++) {
unshift @bs, (($_ >>= 1) & 1); # More of Justin's mojo:
bit-shifting.
}
return (join "", @bs);
}
j
Omaha Perl Mongers: http://omaha.pm.org
More information about the OLUG
mailing list