前缀长度变掩码
(每道题开头都有同一段:to_int 把点分十进制变成 32 位整数,to_str 变回去,mask(前缀长度) 给出掩码的整数——高位连续的 1,低位 0。)
五种前缀长度各变成点分十进制的掩码:
def to_int(ip):
a, b, c, d = [int(x) for x in ip.split(".")]
return (a << 24) | (b << 16) | (c << 8) | d
def to_str(n):
return ".".join(str((n >> s) & 255) for s in (24, 16, 8, 0))
def mask(prefix):
return (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF
print(",".join(to_str(mask(p)) for p in (8, 16, 24, 26, 30)))
全部评论