what's wrong with golang constant overflows uint64

后端 未结 2 783
-上瘾入骨i
-上瘾入骨i 2020-12-20 02:04
userid := 12345
did := (userid & ^(0xFFFF << 48))

when compiling this code, I got:

./xxxx.go:511: constant -

2条回答
  •  时光说笑
    2020-12-20 02:50

    The Go Programming Language Specification

    Constants

    Numeric constants represent values of arbitrary precision and do not overflow.

    Constants may be typed or untyped.

    A constant may be given a type explicitly by a constant declaration or conversion, or implicitly when used in a variable declaration or an assignment or as an operand in an expression. It is an error if the constant value cannot be represented as a value of the respective type.

    An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as i := 0 where there is no explicit type. The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.

    Numeric types

    int is an implementation-specific size, either 32 or 64 bits.

    userid is of type int. For example,

    package main
    
    import "fmt"
    
    func main() {
        userid := 12345
        did := uint64(userid) & ^uint64(0xFFFF<<48)
        fmt.Println(userid, did)
    }
    

    Output:

    12345 12345
    

提交回复
热议问题