q/src/sizeof/Unsigned.go

20 lines
313 B
Go

package sizeof
import "math"
// Unsigned tells you how many bytes are needed to encode this unsigned number.
func Unsigned(number uint64) int {
switch {
case number <= math.MaxUint8:
return 1
case number <= math.MaxUint16:
return 2
case number <= math.MaxUint32:
return 4
default:
return 8
}
}