Skip to content

Commit 103cfd6

Browse files
committed
Replace more unnecessary bitwise ops with encoding/binary
1 parent 93e30fa commit 103cfd6

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

conversions.go

+7-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"bytes"
54
"encoding/binary"
65
"net"
76
)
@@ -11,28 +10,15 @@ func ip2long(ip net.IP) uint32 {
1110
return binary.BigEndian.Uint32(ip.To4()[0:4])
1211
}
1312

14-
func long2ip(ipIntLong uint32) net.IP {
15-
ipInt := int64(ipIntLong)
16-
b0 := byte(ipInt >> 24 & 0xff)
17-
b1 := byte(ipInt >> 16 & 0xff)
18-
b2 := byte(ipInt >> 8 & 0xff)
19-
b3 := byte(ipInt & 0xff)
20-
return net.IP{b0, b1, b2, b3}
13+
func long2ip(data uint32) net.IP {
14+
b := long2bytes(data)
15+
return net.IP{b[0], b[1], b[2], b[3]}
2116
}
2217

23-
// Quickly ripped from https://gist.github.com/chiro-hiro/2674626cebbcb5a676355b7aaac4972d
24-
func long2bytes(ip uint32) []byte {
25-
r := make([]byte, 4)
26-
for i := uint32(0); i < 4; i++ {
27-
r[3-i] = byte((ip >> (8 * i)) & 0xff)
28-
}
29-
return r
30-
}
31-
32-
func bytes2long(ip []byte) uint32 {
33-
var long uint32
34-
binary.Read(bytes.NewBuffer(ip), binary.LittleEndian, &long)
35-
return long
18+
func long2bytes(data uint32) []byte {
19+
b := make([]byte, 4)
20+
binary.BigEndian.PutUint32(b, data)
21+
return b
3622
}
3723

3824
func calcBroadcast(network, netmask net.IP) net.IP {

0 commit comments

Comments
 (0)