Not directly an answer, but check out Net::Netmask which already handles this for you.
In reply to Re: behavior of bitwise shift by Fletch in thread behavior of bitwise shift by ngiordano