sub bin { return unpack('N', pack('B32', substr('0'x32 . $_[0], -32))); } $mask = bin('1'x$bits . '0'x(32-$bits));