package main import ( `fmt` `log` "strconv" ) const ( /* This is a nonsensical example. */ vtfBits string = "01100000000000000000000000000000" // [0110 0000 0000 0000 0000 0000 0000 0000], or 1610612736 (0x60000000) ) var ( vtfVOffset uint32 = 0x0000001c // 28 vtfVFlagMask uint32 = 0x0000000f // Mask to 4 bits vtfTOffset uint32 = 0x00000014 // 20 vtfTFlagMask uint32 = 0x000000ff // mask to 8 bits vtfFlowMask uint32 = 0x0000ffff ) func ToVTF(ver, tc, flow uint32) (vtf uint32) { vtf = (ver << vtfVOffset) | (tc << vtfTOffset) | (flow & vtfFlowMask) return } func FromVTF(vtf uint32) (ver, tc, flow uint32) { ver = (vtf >> vtfVOffset) & vtfVFlagMask tc = (vtf >> vtfTOffset) & vtfTFlagMask flow = vtf & vtfFlowMask return } func main() { var err error var u64 uint64 var vtf uint32 var ver uint32 var tc uint32 var flow uint32 // Given a fragmentation configuration of vtfBits (see const at top)... if u64, err = strconv.ParseUint(vtfBits, 2, 32); err != nil { log.Panicln(err) } vtf = uint32(u64) // Prints: /* VTF is: 1610612736 (0x60000000) */ fmt.Printf("VTF is:\t\t\t%d (%#04x)\n", vtf, vtf) ver, tc, flow = FromVTF(vtf) // Prints: /* VTF is: 1610612736 (0x60000000) Version: 6 (0x0006) Traffic Class: 0 (0x0000) Flow Label: 0 (0x0000) */ fmt.Printf( "Version:\t\t%d (%#04x)\n"+ "Traffic Class:\t\t%d (%#04x)\n"+ "Flow Label:\t\t%d (%#04x)\n", ver, ver, tc, tc, flow, flow, ) vtf = ToVTF(ver, tc, flow) // Prints: /* Confirmed V/T/F: 1610612736 (0x60000000) */ fmt.Printf("Confirmed V/T/F:\t%d (%#02x)\n", vtf, vtf) }