如何指定unsigned整数类型可表示的最大值?
unsigned
我想知道如何min在下面的循环中初始化,以迭代地计算某些结构的最小和最大长度。
min
var minLen uint = ??? var maxLen uint = 0 for _, thing := range sliceOfThings { if minLen > thing.n { minLen = thing.n } if maxLen < thing.n { maxLen = thing.n } } if minLen > maxLen { // If there are no values, clamp min at 0 so that min <= max. minLen = 0 }
以便第一时间通过比较,minLen >= n。
minLen >= n
相关的部分:
由于整数类型使用二进制补码运算,可以推断最小/最大常数值int和uint。例如, golang const MaxUint = ^uint(0) const MinUint = 0 const MaxInt = int(MaxUint >> 1) const MinInt = -MaxInt - 1
由于整数类型使用二进制补码运算,可以推断最小/最大常数值int和uint。例如,
int
uint
golang const MaxUint = ^uint(0) const MinUint = 0 const MaxInt = int(MaxUint >> 1) const MinInt = -MaxInt - 1
根据@CarelZA 的评论:
uint8 : 0 to 255 uint16 : 0 to 65535 uint32 : 0 to 4294967295 uint64 : 0 to 18446744073709551615 int8 : -128 to 127 int16 : -32768 to 32767 int32 : -2147483648 to 2147483647 int64 : -9223372036854775808 to 9223372036854775807