[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Bitwise not operator.



ajf@afco.demon.co.uk (Alan Fry) writes:
} >I've just been trying to do netmask manipulation in MacPerl (5.03r1m)
} >There appear to be problems with the bitwise not of certain numbers,
} >Specifically, accorind to macperl :-
} >
} >~ffffff00 is 7fffffff
} >and
} >~7fffffff is 80000000 (which is correct.)
} >
} >Is there something strange going on here to do with the handling
} >of signed quantities and coversions? It would be nice if Perl
} >behaved as expected :-)
} 
} I think the problem is that MacPerl can only count up to 2147483647. For
} instance:

MacPerl, Unix perl, and any other program that uses 32 bit signed
integers, of course.

} 
}         if( $num < 0 ) { print "lt\n" } else { print "gt\n" }
} returns:
}         'gt' when $num == 2147483647    (expected)
}         'lt' when $num == 2147483648    (unexpected)

Expected.  Add one and it wraps from the largest positive integer to the
smallest negative one.

} 
} Similarly if:
}         $num = hex("7FFFFFFF");
}         sprintf("%lx", -$num) returns 80000001 (correct)
} but if:
}         $num = hex("80000000")
}         sprintf("%lx", -$num) returns 80000000 (H'mmm)
} 
} In the last case it should return FF80000000, but can't manage the first
} "FF" I believe.

This also happens with Perl 5.003 on a Sun workstation.   I'll have to
track down documentation on integer representations some time (I think
it's first bit on is negative the complement - 1, but I'm not sure).  It
may just be because -$num is one more than the largest positive integer
the machine can represent, so it can't do what you're asking.

} 
} I should add this is with MacPerl 68K.

And with anything else you try that's doing 32 bit signed integer arithmetic.

} 
} Alan Fry
} 
} 
} 
} 
---
--------
Paul J. Schinder
NASA Goddard Space Flight Center,
Code 693, Greenbelt, MD 20771 USA
schinder@pjstoaster.pg.md.us