如何为无符号整数类型指定可表示的最大值?
我想知道如何在下面的循环中初始化min,迭代计算一些结构的min和max长度。
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。
https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1
相关部分:
由于整数类型使用二补运算,因此可以推断
int和uint的最小/最大常量值。例如,
const maxint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxInt >> 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