Go语言中int类型的最大值

huangapple go评论99阅读模式
英文:

The maximum value for an int type in Go

问题

如何指定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 {
  // 如果没有值,则将min限制为0,以使min <= max。
  minLen = 0
}

这样,第一次比较时,minLen >= n

英文:

How does one specify the maximum value representable for an unsigned integer type?

I would like to know how to initialize min in the loop below that iteratively computes min and max lengths from some structs.

var minLen uint = ???
var maxLen uint = 0
for _, thing := range sliceOfThings {
  if minLen &gt; thing.n { minLen = thing.n }
  if maxLen &lt; thing.n { maxLen = thing.n }
}
if minLen &gt; maxLen {
  // If there are no values, clamp min at 0 so that min &lt;= max.
  minLen = 0
}

so that the first time through the comparison, minLen &gt;= n.

答案1

得分: 322

德国部分:

> 由于整数类型使用二进制补码算术,您可以推断出intuint的最小/最大常量值。例如,

> const MaxUint = ^uint(0)
> const MinUint = 0
> const MaxInt = int(MaxUint >> 1)
> const MinInt = -MaxInt - 1

根据@CarelZA的评论:

uint8  : 0 到 255 
uint16 : 0 到 65535 
uint32 : 0 到 4294967295 
uint64 : 0 到 18446744073709551615 
int8   : -128 到 127 
int16  : -32768 到 32767 
int32  : -2147483648 到 2147483647 
int64  : -9223372036854775808 到 9223372036854775807
英文:

https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1

The germane part:

> Since integer types use two's complement arithmetic, you can infer the
> min/max constant values for int and uint. For example,
>
> const MaxUint = ^uint(0)
> const MinUint = 0
> const MaxInt = int(MaxUint >> 1)
> const MinInt = -MaxInt - 1

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

答案2

得分: 91

https://golang.org/ref/spec#Numeric_types 用于物理类型限制。

最大值在math包中定义,所以在你的情况下是:math.MaxUint32

要注意的是,没有溢出-超过最大值会导致循环。

英文:

https://golang.org/ref/spec#Numeric_types for physical type limits.

The max values are defined in the math package so in your case: math.MaxUint32

Watch out as there is no overflow - incrementing past max causes wraparound.

答案3

得分: 75

我会使用math包来获取整数的最大值和最小值:

package main

import (
    "fmt"
    "math"
)

func main() {
	// 整数最大值
	fmt.Printf("max int64   = %+v\n", math.MaxInt64)
	fmt.Printf("max int32   = %+v\n", math.MaxInt32)
	fmt.Printf("max int16   = %+v\n", math.MaxInt16)

	// 整数最小值
	fmt.Printf("min int64   = %+v\n", math.MinInt64)
	fmt.Printf("min int32   = %+v\n", math.MinInt32)

	fmt.Printf("max float64 = %+v\n", math.MaxFloat64)
	fmt.Printf("max float32 = %+v\n", math.MaxFloat32)

	// 等等,你可以在`math`包中看到更多
}

输出:

max int64   = 9223372036854775807
max int32   = 2147483647
max int16   = 32767
min int64   = -9223372036854775808
min int32   = -2147483648
max float64 = 1.7976931348623157e+308
max float32 = 3.4028234663852886e+38
英文:

I would use math package for getting the maximal and minimal values for integers:

package main

import (
    &quot;fmt&quot;
    &quot;math&quot;
)

func main() {
	// integer max
	fmt.Printf(&quot;max int64   = %+v\n&quot;, math.MaxInt64)
	fmt.Printf(&quot;max int32   = %+v\n&quot;, math.MaxInt32)
	fmt.Printf(&quot;max int16   = %+v\n&quot;, math.MaxInt16)

	// integer min
	fmt.Printf(&quot;min int64   = %+v\n&quot;, math.MinInt64)
	fmt.Printf(&quot;min int32   = %+v\n&quot;, math.MinInt32)

	fmt.Printf(&quot;max float64 = %+v\n&quot;, math.MaxFloat64)
	fmt.Printf(&quot;max float32 = %+v\n&quot;, math.MaxFloat32)

	// etc you can see more int the `math`package
}

Output:

max int64   = 9223372036854775807
max int32   = 2147483647
max int16   = 32767
min int64   = -9223372036854775808
min int32   = -2147483648
max float64 = 1.7976931348623157e+308
max float32 = 3.4028234663852886e+38

答案4

得分: 24

注意:此答案已被go 1.17取代,其中包括e8eb1d8;即:math包现在包含了math.MaxUintmath.MaxIntmath.MinInt的常量。

快速总结:

import "math/bits"
const (
    MaxUint uint = (1 << bits.UintSize) - 1
    MaxInt int = (1 << bits.UintSize) / 2 - 1
    MinInt int = (1 << bits.UintSize) / -2
)

背景:

我假设你知道,uint类型的大小与平台上的uint32uint64相同。通常,只有在没有接近最大值的风险时,才会使用未指定大小的版本,因为未指定大小的版本可以使用“本机”类型,具体取决于平台,这往往更快。

需要注意的是,它往往更“快”,因为使用非本机类型有时需要处理器执行额外的数学运算和边界检查,以模拟较大或较小的整数。在这种情况下,请注意处理器的性能(或编译器优化的代码)几乎总是优于添加自己的边界检查代码,因此,如果有任何风险可能发生,直接使用固定大小的版本并让优化的模拟处理任何后果可能是有意义的。

尽管如此,仍然有一些情况下了解你正在处理的内容是有用的。

包“math/bits”包含了uint的位数。要确定最大值,将1左移那么多位,然后减去1。即:(1 << bits.UintSize) - 1

需要注意的是,在计算uint的最大值时,通常需要将其明确放入uint(或更大)变量中,否则编译器可能会失败,因为它将默认尝试将该计算分配给有符号的int(显然,它不适合),所以:

const MaxUint uint = (1 << bits.UintSize) - 1

这是对你问题的直接回答,但还有一些相关的计算可能会对你有用。

根据规范uintint的大小始终相同。

> uint 要么是32位或64位
>
> intuint大小相同

因此,我们还可以使用此常量来确定int的最大值,方法是将相同的答案除以2,然后减去1。即:(1 << bits.UintSize) / 2 - 1

以及int的最小值,方法是将1左移那么多位,并将结果除以-2。即:(1 << bits.UintSize) / -2

总结:

MaxUint:(1 << bits.UintSize) - 1

MaxInt:(1 << bits.UintSize) / 2 - 1

MinInt:(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)
}
英文:

note: this answer is superseded as of go 1.17, which included e8eb1d8; ie: the math package now includes constants for math.MaxUint, math.MaxInt, and math.MinInt.

Quick summary:

import &quot;math/bits&quot;
const (
    MaxUint uint = (1 &lt;&lt; bits.UintSize) - 1
    MaxInt int = (1 &lt;&lt; bits.UintSize) / 2 - 1
    MinInt int = (1 &lt;&lt; bits.UintSize) / -2
)

Background:

As I presume you know, the uint type is the same size as either uint32 or uint64, depending on the platform you're on. Usually, one would use the unsized version of these only when there is no risk of coming close to the maximum value, as the version without a size specification can use the "native" type, depending on platform, which tends to be faster.

Note that it tends to be "faster" because using a non-native type sometimes requires additional math and bounds-checking to be performed by the processor, in order to emulate the larger or smaller integer. With that in mind, be aware that the performance of the processor (or compiler's optimised code) is almost always going to be better than adding your own bounds-checking code, so if there is any risk of it coming into play, it may make sense to simply use the fixed-size version, and let the optimised emulation handle any fallout from that.

With that having been said, there are still some situations where it is useful to know what you're working with.

The package "math/bits" contains the size of uint, in bits. To determine the maximum value, shift 1 by that many bits, minus 1. ie: (1 &lt;&lt; bits.UintSize) - 1

Note that when calculating the maximum value of uint, you'll generally need to put it explicitly into a uint (or larger) variable, otherwise the compiler may fail, as it will default to attempting to assign that calculation into a signed int (where, as should be obvious, it would not fit), so:

const MaxUint uint = (1 &lt;&lt; bits.UintSize) - 1

That's the direct answer to your question, but there are also a couple of related calculations you may be interested in.

According to the spec, uint and int are always the same size.

> uint either 32 or 64 bits
>
> int same size as uint

So we can also use this constant to determine the maximum value of int, by taking that same answer and dividing by 2 then subtracting 1. ie: (1 &lt;&lt; bits.UintSize) / 2 - 1

And the minimum value of int, by shifting 1 by that many bits and dividing the result by -2. ie: (1 &lt;&lt; bits.UintSize) / -2

In summary:

MaxUint: (1 &lt;&lt; bits.UintSize) - 1

MaxInt: (1 &lt;&lt; bits.UintSize) / 2 - 1

MinInt: (1 &lt;&lt; bits.UintSize) / -2

full example (should be the same as below)

package main

import &quot;fmt&quot;
import &quot;math&quot;
import &quot;math/bits&quot;

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 &lt;&lt; bits.UintSize) - 1
	var i uint64 = (1 &lt;&lt; bits.UintSize) / 2 - 1
	var mi int64 = (1 &lt;&lt; bits.UintSize) / -2
	
	fmt.Printf(&quot; MinInt32: %d\n&quot;, mi32)
	fmt.Printf(&quot; MaxInt32:  %d\n&quot;, i32)
	fmt.Printf(&quot;MaxUint32:  %d\n&quot;, ui32)
	fmt.Printf(&quot; MinInt64: %d\n&quot;, mi64)
	fmt.Printf(&quot; MaxInt64:  %d\n&quot;, i64)
	fmt.Printf(&quot;MaxUint64:  %d\n&quot;, ui64)
	fmt.Printf(&quot;  MaxUint:  %d\n&quot;, ui)
	fmt.Printf(&quot;   MinInt: %d\n&quot;, mi)
	fmt.Printf(&quot;   MaxInt:  %d\n&quot;, i)
}

答案5

得分: 14

我最初使用了@nmichaels在他的答案中使用的代码。现在我使用了稍微不同的计算方法。我已经包含了一些注释,以防其他人有与@Arijoon相同的疑问。

const (
    MinUint uint = 0                 // 二进制:全为零

    // 执行按位取反操作,将每个位从0变为1
    MaxUint      = ^MinUint          // 二进制:全为一

    // 将二进制数向右移位(即除以2),将高位变为0
    MaxInt       = int(MaxUint &gt;&gt; 1) // 二进制:除高位外全为一

    // 再次执行按位取反操作,将高位变为1,其他位变为0
    MinInt       = ^MaxInt           // 二进制:除高位外全为零
)

最后两个步骤之所以有效,是因为正数和负数在二进制补码算术中的表示方式。Go语言规范中的数字类型部分将读者引用到相关的Wikipedia文章。我没有阅读过那篇文章,但我从Code by Charles Petzold这本书中学到了关于二进制补码的知识,这是一本非常易懂的计算机和编码基础入门书籍。

我将上面的代码(除了大部分注释)放入了一个小的整数数学包中。

英文:

I originally used the code taken from the discussion thread that @nmichaels used in his answer. I now use a slightly different calculation. I've included some comments in case anyone else has the same query as @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 &gt;&gt; 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
)

The last two steps work because of how positive and negative numbers are represented in two's complement arithmetic. The Go language specification section on Numeric types refers the reader to the relevant Wikipedia article. I haven't read that, but I did learn about two's complement from the book Code by Charles Petzold, which is a very accessible intro to the fundamentals of computers and coding.

I put the code above (minus most of the comments) in to a little integer math package.

答案6

得分: 9

从数学库: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)
}
英文:

From math lib: https://github.com/golang/go/blob/master/src/math/const.go#L39

package main

import (
	&quot;fmt&quot;
	&quot;math&quot;
)

func main() {
	fmt.Printf(&quot;max int64: %d\n&quot;, math.MaxInt64)
}

答案7

得分: 9

Go-1.17现在在math包中定义了MaxUintMaxIntMinInt常量。

package main

import "fmt"
import "math"

const maxUint = uint(math.MaxUint)

func main() {
	fmt.Println("你的系统上的整数范围")

	// .Println("MaxUint:", math.MaxUint)  ERROR constant 18446744073709551615 overflows int
	fmt.Println("MaxUint:", maxUint)

	fmt.Println("MinInt:", math.MinInt)
	fmt.Println("MaxInt:", math.MaxInt)
}
  • 测试上述代码:https://play.golang.org/p/5R2iPasn6OZ

  • 来自Go 1.17发布说明:https://golang.org/doc/go1.17#math

math包现在定义了三个更多的常量:MaxUintMaxIntMinInt
对于32位系统,它们的值分别为2^32 - 12^31 - 1-2^31
对于64位系统,它们的值分别为2^64 - 12^63 - 1-2^63

  • 提交记录:https://github.com/golang/go/commit/e8eb1d82
  • 文档:https://pkg.go.dev/math#pkg-constants
const (
	MaxInt    = 1<<(intSize-1) - 1   // 新增
	MinInt    = -1 << (intSize - 1)  // 新增
	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       // 新增
	MaxUint8  = 1<<8 - 1
	MaxUint16 = 1<<16 - 1
	MaxUint32 = 1<<32 - 1
	MaxUint64 = 1<<64 - 1
)

另请参阅Go源代码:https://github.com/golang/go/blob/master/src/math/const.go#L39

英文:

Go-1.17 now defines MaxUint, MaxInt and MinInt constants in the math package.

package main

import &quot;fmt&quot;
import &quot;math&quot;

const maxUint = uint(math.MaxUint)

func main() {
	fmt.Println(&quot;Integer range on your system&quot;)

	// .Println(&quot;MaxUint:&quot;, math.MaxUint)  ERROR constant 18446744073709551615 overflows int
	fmt.Println(&quot;MaxUint:&quot;, maxUint)

	fmt.Println(&quot;MinInt:&quot;, math.MinInt)
	fmt.Println(&quot;MaxInt:&quot;, math.MaxInt)
}

> The math package now defines three more constants: MaxUint, MaxInt and MinInt.
> For 32-bit systems their values are 2^32 - 1, 2^31 - 1 and -2^31, respectively.
> For 64-bit systems their values are 2^64 - 1, 2^63 - 1 and -2^63, respectively.

const (
	MaxInt    = 1&lt;&lt;(intSize-1) - 1   // New
	MinInt    = -1 &lt;&lt; (intSize - 1)  // New
	MaxInt8   = 1&lt;&lt;7 - 1
	MinInt8   = -1 &lt;&lt; 7
	MaxInt16  = 1&lt;&lt;15 - 1
	MinInt16  = -1 &lt;&lt; 15
	MaxInt32  = 1&lt;&lt;31 - 1
	MinInt32  = -1 &lt;&lt; 31
	MaxInt64  = 1&lt;&lt;63 - 1
	MinInt64  = -1 &lt;&lt; 63
	MaxUint   = 1&lt;&lt;intSize - 1       // New
	MaxUint8  = 1&lt;&lt;8 - 1
	MaxUint16 = 1&lt;&lt;16 - 1
	MaxUint32 = 1&lt;&lt;32 - 1
	MaxUint64 = 1&lt;&lt;64 - 1
)

See also the Go source code: https://github.com/golang/go/blob/master/src/math/const.go#L39

答案8

得分: 7

使用在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
)
英文:

Use the constants defined in the math package:

const (
    MaxInt8   = 1&lt;&lt;7 - 1
    MinInt8   = -1 &lt;&lt; 7
    MaxInt16  = 1&lt;&lt;15 - 1
    MinInt16  = -1 &lt;&lt; 15
    MaxInt32  = 1&lt;&lt;31 - 1
    MinInt32  = -1 &lt;&lt; 31
    MaxInt64  = 1&lt;&lt;63 - 1
    MinInt64  = -1 &lt;&lt; 63
    MaxUint8  = 1&lt;&lt;8 - 1
    MaxUint16 = 1&lt;&lt;16 - 1
    MaxUint32 = 1&lt;&lt;32 - 1
    MaxUint64 = 1&lt;&lt;64 - 1
)

答案9

得分: 3

一种解决这个问题的方法是从值本身获取起始点:

var minLen, maxLen uint
if len(sliceOfThings) > 0 {
  minLen = sliceOfThings[0].minLen
  maxLen = sliceOfThings[0].maxLen
  for _, thing := range sliceOfThings[1:] {
    if minLen > thing.minLen { minLen = thing.minLen }
    if maxLen < thing.maxLen { maxLen = thing.maxLen }
  }
}
英文:

One way to solve this problem is to get the starting points from the values themselves:

var minLen, maxLen uint
if len(sliceOfThings) &gt; 0 {
  minLen = sliceOfThings[0].minLen
  maxLen = sliceOfThings[0].maxLen
  for _, thing := range sliceOfThings[1:] {
    if minLen &gt; thing.minLen { minLen = thing.minLen }
    if maxLen &lt; thing.maxLen { maxLen = thing.maxLen }
  }
}

答案10

得分: 2

Go 1.17(2021年第四季度)可能会有所帮助,根据Go101的说明,通过提交e8eb1d8

> 在Go 1.17之前,我们可以使用以下技巧来定义MaxInt
>
> const MaxInt = int(^uint(0) >> 1)
>
> 自Go 1.17以后,我们可以直接使用math.MaxInt

这修复了由Silentd00m报告的问题28538,并经过CL 247058的审查。

> 由于我们有int8int64min maxuint8uint64max常量,我们可能也应该有一些用于字长类型的常量。

Tests演示了这是如何工作的:

	if v := int(MaxInt); v+1 != MinInt {
		t.Errorf(&quot;MaxInt should wrap around to MinInt: %d&quot;, v+1)
	}
	if v := int8(MaxInt8); v+1 != MinInt8 {
		t.Errorf(&quot;MaxInt8 should wrap around to MinInt8: %d&quot;, v+1)
	}
	if v := int16(MaxInt16); v+1 != MinInt16 {
		t.Errorf(&quot;MaxInt16 should wrap around to MinInt16: %d&quot;, v+1)
	}
	if v := int32(MaxInt32); v+1 != MinInt32 {
		t.Errorf(&quot;MaxInt32 should wrap around to MinInt32: %d&quot;, v+1)
	}
	if v := int64(MaxInt64); v+1 != MinInt64 {
		t.Errorf(&quot;MaxInt64 should wrap around to MinInt64: %d&quot;, v+1)
	}
英文:

Go 1.17 (Q4 2021) might help, with commit e8eb1d8, as noted by Go101:

> Before Go 1.17, we can use the following trick to define MaxInt:
>
> const MaxInt = int(^uint(0) >> 1)
>
> Since Go 1.17, we can directly use math.MaxInt instead

That fixes issue 28538 reported by Silentd00m, reviewed with CL 247058.

> Since we have int8 to int64 min max and uint8 to uint64 max constants, we should probably have some for the word size types too.

Tests are illustrating how this works:

	if v := int(MaxInt); v+1 != MinInt {
		t.Errorf(&quot;MaxInt should wrap around to MinInt: %d&quot;, v+1)
	}
	if v := int8(MaxInt8); v+1 != MinInt8 {
		t.Errorf(&quot;MaxInt8 should wrap around to MinInt8: %d&quot;, v+1)
	}
	if v := int16(MaxInt16); v+1 != MinInt16 {
		t.Errorf(&quot;MaxInt16 should wrap around to MinInt16: %d&quot;, v+1)
	}
	if v := int32(MaxInt32); v+1 != MinInt32 {
		t.Errorf(&quot;MaxInt32 should wrap around to MinInt32: %d&quot;, v+1)
	}
	if v := int64(MaxInt64); v+1 != MinInt64 {
		t.Errorf(&quot;MaxInt64 should wrap around to MinInt64: %d&quot;, v+1)
	}

答案11

得分: 0

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

英文:
MaxInt8   = 1&lt;&lt;7 - 1
MinInt8   = -1 &lt;&lt; 7
MaxInt16  = 1&lt;&lt;15 - 1
MinInt16  = -1 &lt;&lt; 15
MaxInt32  = 1&lt;&lt;31 - 1
MinInt32  = -1 &lt;&lt; 31
MaxInt64  = 1&lt;&lt;63 - 1
MinInt64  = -1 &lt;&lt; 63
MaxUint8  = 1&lt;&lt;8 - 1
MaxUint16 = 1&lt;&lt;16 - 1
MaxUint32 = 1&lt;&lt;32 - 1
MaxUint64 = 1&lt;&lt;64 - 1

答案12

得分: 0

我总是记得的方式是,你取出位数(int8是8位,int是32位),除以8得到字节数(int8是一个字节,int是四个字节)。

每个字节都是0xFF(除了有符号整数,此时最高有效字节为0x7F)。以下是结果:

package main

func main() {
   {
      var n int8 = 0x7F
      println(n) // 127
   }
   {
      var n uint8 = 0xFF
      println(n) // 255
   }
   {
      var n int = 0x7FFF_FFFF
      println(n) // 2147483647
   }
   {
      var n uint = 0xFFFF_FFFF
      println(n) // 4294967295
   }
}
英文:

The way I always remember it, is you take the bits (int8 is 8 bits, int is
32 bits), divide by 8 and you get the bytes (int8 would be one byte, int
would be four bytes).

Each byte is 0xFF (except for signed integer, in which case most significant
byte will be 0x7F). Here is result:

package main

func main() {
   {
      var n int8 = 0x7F
      println(n) // 127
   }
   {
      var n uint8 = 0xFF
      println(n) // 255
   }
   {
      var n int = 0x7FFF_FFFF
      println(n) // 2147483647
   }
   {
      var n uint = 0xFFFF_FFFF
      println(n) // 4294967295
   }
}

答案13

得分: -1

import (
    "fmt"
    "<完整的URL>/go-imath/ix"
    "<完整的URL>/go-imath/ux"
)
...
fmt.Println(ix.Minimal) // 输出: -2147483648 (32位) 或 -9223372036854775808 (64位)
fmt.Println(ix.Maximal) // 输出: 2147483647 或 9223372036854775807
fmt.Println(ux.Minimal) // 输出: 0
fmt.Println(ux.Maximal) // 输出: 4294967295 或 18446744073709551615
英文:

A lightweight package contains them (as well as other int types limits and some widely used integer functions):

import (
    &quot;fmt&quot;
    &quot;&lt;Full URL&gt;/go-imath/ix&quot;
    &quot;&lt;Full URL&gt;/go-imath/ux&quot;
)
...
fmt.Println(ix.Minimal) // Output: -2147483648 (32-bit) or -9223372036854775808 (64-bit)
fmt.Println(ix.Maximal) // Output: 2147483647 or 9223372036854775807
fmt.Println(ux.Minimal) // Output: 0
fmt.Println(ux.Maximal) // Output: 4294967295 or 18446744073709551615

huangapple
  • 本文由 发表于 2011年7月30日 04:21:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/6878590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定