I try to invert the binary in golang to find broadcast ip address from the input data.
10000010.00101101.00100000.00000000 (network adress)
OR
00000000.00000000.00001111.11111111 (inverted subnet mask)
=
10000010.00101101.00101111.11111111 = 130.45.47.255 (broadcast address)
Assume user input
network address 130.45.32.0
subnetmask 255.255.240.0 11111111.11111111.11110000.00000000
// expect invert subnetmask 00000000.00000000.00001111.11111111
So if I got subnet mask from user input now I need to invert it
// subIp from https://golang.org/pkg/net/#IP.Mask
subIp:=subnetmask.Mask(network.Mask)
// 255. 255. 240. 0
// then I loop to subIp[0], subIp[1], subIp[2], subIp[3]
// I Assume subIp[3] = 0
subnet := int64(subIp[3])
subnetString := strconv.FormatInt(subnet, 2)
// add 0 before number
subnetString = leftPad2Len(subnetString, "0", 8)
// loop to string and invert it
invertSubnetString := ""
for i := 0; i<8; i++ {
if subnetString[i] == 48 {
invertSubnetString += "1"
} else {
invertSubnetString += "0"
}
}
// now I got invert data. So I can find broadcast number
// convert it to int
invertSubnet, _ := strconv.ParseInt(invertSubnetString, 2, 64)
// then OR
fmt.Println(networkIp | invertSubnet)
Are there any easy way to do the thing like I do ?