The maximum value for an int type in Go

前端 未结 10 1145
无人共我
无人共我 2021-01-29 19:09

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

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-29 19:44

    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
    

提交回复
热议问题