----- Original Message -----
From: "Orlando Andico" <[EMAIL PROTECTED]>
To: "PLUG Mailing List" <[EMAIL PROTECTED]>
Sent: Thursday, February 07, 2002 12:17 PM
Subject: [plug] bitwise ops in perl


>
> ah forget it. me stupid.
>
> my $mask = (1 << ($masklen + 1)) - 1;

orly,

    that is still incorrect.. remember that you are dealing with *32 bit*
integer for ipv4... supposing that $masklen is equal to 24, the correct
answer in 32 bit in hex is FFFFFF00... if you are going to use your optimize
formula, the result in hex is 1FFFFFF.. even if you remove plus 1 in
($masklen + 1), still you got the wrong results $mask = (1 << $masklen) - 1
= FFFFFF != FFFFFF00.

    here is a simple trick to do your job:

    $ip = <ip address in network to host conversion>;
    $masklen = <between 0 to 32>;
    $mask = 32 - $masklen;
    $segment = ($ip >> $mask) << $mask;

    in case your network is ipv6, just replace 32 to 128

    but of course, there is an interesting bug in perl because all numbers
assigned to a variable treated as floating point... to find out that bug,
try to replace $masklen = 0 and see for yourself :->

fooler.

_
Philippine Linux Users Group. Web site and archives at http://plug.linux.org.ph
To leave: send "unsubscribe" in the body to [EMAIL PROTECTED]

To subscribe to the Linux Newbies' List: send "subscribe" in the body to 
[EMAIL PROTECTED]

Reply via email to