如何指定unsigned整数类型可表示的最大值?
我想知道如何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
}
Run Code Online (Sandbox Code Playgroud)
这样第一次通过比较,minLen >= n.
nmi*_*els 192
https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1
密切相关部分:
由于整数类型使用二进制补码运算,可以推断最小/最大常数值
int和uint.例如,Run Code Online (Sandbox Code Playgroud)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
Run Code Online (Sandbox Code Playgroud)
Del*_*ted 67
https://golang.org/ref/spec#Numeric_types用于物理类型限制.
最大值在数学包中定义,因此在您的情况下:math.MaxUint32
注意没有溢出 - 增加过去最大导致环绕.
Guj*_*ana 20
我会使用math包来获取最大值和最小值:
func printMinMaxValue() {
// integer max
fmt.Printf("max int64 = %+v\n", math.MaxInt64)
fmt.Printf("max int32 = %+v\n", math.MaxInt32)
fmt.Printf("max int16 = %+v\n", math.MaxInt16)
// integer min
fmt.Printf("min int64 = %+v\n", math.MinInt64)
fmt.Printf("min int32 = %+v\n", math.MinInt32)
fmt.Printf("max flloat64= %+v\n", math.MaxFloat64)
fmt.Printf("max float32= %+v\n", math.MaxFloat32)
// etc you can see more int the `math`package
}
Run Code Online (Sandbox Code Playgroud)
输出:
max int64 = 9223372036854775807
max int32 = 2147483647
max int16 = 32767
min int64 = -9223372036854775808
min int32 = -2147483648
max flloat64= 1.7976931348623157e+308
max float32= 3.4028234663852886e+38
Run Code Online (Sandbox Code Playgroud)
Wil*_*mer 13
快速总结:
import "math/bits"
const (
MaxUint uint = (1 << bits.UintSize) - 1
MaxInt int = (1 << bits.UintSize) / 2 - 1
MinInt int = (1 << bits.UintSize) / -2
)
Run Code Online (Sandbox Code Playgroud)
背景:
我想您知道,uint类型与uint32或 的大小相同uint64,具体取决于您所在的平台。通常,只有在没有接近最大值的风险时才会使用这些的非大小版本,因为没有大小规范的版本可以使用“本机”类型,具体取决于平台,这往往更快。
请注意,它往往“更快”,因为使用非本地类型有时需要处理器执行额外的数学运算和边界检查,以便模拟更大或更小的整数。考虑到这一点,请注意处理器(或编译器的优化代码)的性能几乎总是比添加您自己的边界检查代码要好,所以如果它有任何发挥作用的风险,它可能会使简单地使用固定大小的版本是有意义的,并让优化的仿真处理由此产生的任何后果。
话虽如此,在某些情况下,了解您正在使用的内容仍然很有用。
包“ math/bits ”包含uint, 以位为单位的大小。要确定最大值,请移动1那么多位,减去 1。即:(1 << bits.UintSize) - 1
请注意,在计算 的最大值时uint,您通常需要将其显式放入一个uint(或更大的)变量中,否则编译器可能会失败,因为它会默认尝试将该计算分配给一个有符号的int(其中,应该很明显,它不适合),所以:
const MaxUint uint = (1 << bits.UintSize) - 1
Run Code Online (Sandbox Code Playgroud)
这是您问题的直接答案,但还有一些您可能感兴趣的相关计算。
根据规范,uint并且int总是相同的大小。
uint32 位或 64 位
int大小一样uint
所以我们也可以使用这个常数来确定 的最大值int,方法是取相同的答案,2然后除以减去1。IE:(1 << bits.UintSize) / 2 - 1
以及 的最小值int,通过移位1那么多位并将结果除以-2。IE:(1 << bits.UintSize) / -2
总之:
最大单位: (1 << bits.UintSize) - 1
最大整数: (1 << bits.UintSize) / 2 - 1
分钟: (1 << bits.UintSize) / -2
完整示例(应与下面相同)
package main
import "fmt"
import "math"
import "math/bits"
func main() {
var mi32 int64 = math.MinInt32
var mi64 int64 = math.MinInt64
var i32 uint64 = math.MaxInt32
var ui32 uint64 = math.MaxUint32
var i64 uint64 = math.MaxInt64
var ui64 uint64 = math.MaxUint64
var ui uint64 = (1 << bits.UintSize) - 1
var i uint64 = (1 << bits.UintSize) / 2 - 1
var mi int64 = (1 << bits.UintSize) / -2
fmt.Printf(" MinInt32: %d\n", mi32)
fmt.Printf(" MaxInt32: %d\n", i32)
fmt.Printf("MaxUint32: %d\n", ui32)
fmt.Printf(" MinInt64: %d\n", mi64)
fmt.Printf(" MaxInt64: %d\n", i64)
fmt.Printf("MaxUint64: %d\n", ui64)
fmt.Printf(" MaxUint: %d\n", ui)
fmt.Printf(" MinInt: %d\n", mi)
fmt.Printf(" MaxInt: %d\n", i)
}
Run Code Online (Sandbox Code Playgroud)
cra*_*tok 11
我最初使用的是@nmichaels在他的回答中使用的讨论主题中的代码.我现在使用略有不同的计算.我已经包含了一些评论,以防其他人有与@Arijoon相同的查询
const (
MinUint uint = 0 // binary: all zeroes
// Perform a bitwise NOT to change every bit from 0 to 1
MaxUint = ^MinUint // binary: all ones
// Shift the binary number to the right (i.e. divide by two)
// to change the high bit to 0
MaxInt = int(MaxUint >> 1) // binary: all ones except high bit
// Perform another bitwise NOT to change the high bit to 1 and
// all other bits to 0
MinInt = ^MaxInt // binary: all zeroes except high bit
)
Run Code Online (Sandbox Code Playgroud)
最后两个步骤是有效的,因为在二进制补码算法中如何表示正数和负数.关于数字类型的Go语言规范部分将读者引用到相关的Wikipedia文章.我没有看过,但我确实从Charles Petzold的" Code "一书中学到了两个补充,这是一本非常容易理解的计算机和编码基础知识.
我把上面的代码(减去大部分注释)放到一个小整数数学包中.
oli*_*bre 11
MaxUint,MaxInt和MinInt常量。package main
import "fmt"
import "math"
const maxUint = uint(math.MaxUint)
func main() {
fmt.Println("Integer range on your system")
// .Println("MaxUint:", math.MaxUint) ERROR constant 18446744073709551615 overflows int
fmt.Println("MaxUint:", maxUint)
fmt.Println("MinInt:", math.MinInt)
fmt.Println("MaxInt:", math.MaxInt)
}
Run Code Online (Sandbox Code Playgroud)
来自 Go 1.17 发行说明:https ://golang.org/doc/go1.17#math
math 包现在又定义了三个常量:
MaxUint、MaxInt和MinInt。
对于 32 位系统,它们的值分别为2^32 - 1、2^31 - 1和-2^31。
对于 64 位系统,它们的值分别为2^64 - 1、2^63 - 1和-2^63。
const (
MaxInt = 1<<(intSize-1) - 1 // New
MinInt = -1 << (intSize - 1) // New
MaxInt8 = 1<<7 - 1
MinInt8 = -1 << 7
MaxInt16 = 1<<15 - 1
MinInt16 = -1 << 15
MaxInt32 = 1<<31 - 1
MinInt32 = -1 << 31
MaxInt64 = 1<<63 - 1
MinInt64 = -1 << 63
MaxUint = 1<<intSize - 1 // New
MaxUint8 = 1<<8 - 1
MaxUint16 = 1<<16 - 1
MaxUint32 = 1<<32 - 1
MaxUint64 = 1<<64 - 1
)
Run Code Online (Sandbox Code Playgroud)
另请参阅 Go 源代码:https://github.com/golang/go/blob/master/src/math/const.go#L39
来自数学库:https : //github.com/golang/go/blob/master/src/math/const.go#L39
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("max int64: %d\n", math.MaxInt64)
}
Run Code Online (Sandbox Code Playgroud)
使用math 包中定义的常量:
const (
MaxInt8 = 1<<7 - 1
MinInt8 = -1 << 7
MaxInt16 = 1<<15 - 1
MinInt16 = -1 << 15
MaxInt32 = 1<<31 - 1
MinInt32 = -1 << 31
MaxInt64 = 1<<63 - 1
MinInt64 = -1 << 63
MaxUint8 = 1<<8 - 1
MaxUint16 = 1<<16 - 1
MaxUint32 = 1<<32 - 1
MaxUint64 = 1<<64 - 1
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
125182 次 |
| 最近记录: |