Go中int类型的最大值 [英] The maximum value for an int type in Go
本文介绍了Go中int类型的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何指定可用于无符号
整数类型的最大值?
How does one specify the maximum value representable for an unsigned
integer type?
我想知道如何在下面的循环中初始化 min
,它可以从一些结构中传递计算最小和最大长度。
I would like to know how to initialize min
in the loop below that transitively computes min and max lengths from some structs.
var minLen uint = ???
var maxLen uint = 0
for _, thing := range sliceOfThings {
if minLen > thing.minLen { minLen = n }
if maxLen < thing.maxLen { maxLen = n }
}
if minLen > maxLen {
// If there are no values, clamp min at 0 so that min <= max.
minLen = 0
}
让第一次通过比较, minLen> = n
。
so that the first time through the comparison, minLen >= n
.
推荐答案
https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli = 1
$ b
The germane part:
因为整数类型使用二进制补码算术,可以推断
int
和uint
的
min / max常量值。例如,
Since integer types use two's complement arithmetic, you can infer the min/max constant values for
int
anduint
. For example,
const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
根据@ CarelZA的评论:
As per @CarelZA's comment:
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
这篇关于Go中int类型的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文