Calculate broadcast by IP and mask
Instead of optimizing the Python code, use the ipaddress
module to do the work.
https://docs.python.org/3/library/ipaddress.html
import ipaddress
IP = '192.168.32.16'
MASK = '255.255.0.0'
host = ipaddress.IPv4Address(IP)
net = ipaddress.IPv4Network(IP + '/' + MASK, False)
print('IP:', IP)
print('Mask:', MASK)
print('Subnet:', ipaddress.IPv4Address(int(host) & int(net.netmask)))
print('Host:', ipaddress.IPv4Address(int(host) & int(net.hostmask)))
print('Broadcast:', net.broadcast_address)
OUTPUT:
IP: 192.168.32.16
Mask: 255.255.0.0
Subnet: 192.168.0.0
Host: 0.0.32.16
Broadcast: 192.168.255.255