使用数字文字但不使用数字常量的移位运算符出错
Error in shift operator using numeric literals but not with numeric constant
我在 go 中执行移位操作时遇到错误 invalid operation: 1 << bucketCntBits (shift count type int, must be unsigned integer)
尝试在 main()
正文中声明文字时出错
失败的文字示例:https://play.golang.org/p/EqI-yag5yPp
func main() {
bucketCntBits := 3 // <---- This doesn't work
bucketCnt := 1 << bucketCntBits
fmt.Println("Hello, playground", bucketCnt)
}
当我将班次计数声明为常量时,班次运算符起作用了。
工作常数示例:https://play.golang.org/p/XRLL4FR8ZEl
const (
bucketCntBits = 3 // <---- This works
)
func main() {
bucketCnt := 1 << bucketCntBits
fmt.Println("Hello, playground", bucketCnt)
}
为什么常量有效而文字对移位运算符无效?
使用 math/bits
包你可以这样做:
package main
import (
"fmt"
"math/bits"
)
func main() {
bucketCntBits := 3
bucketCnt := bits.RotateLeft(1, bucketCntBits)
fmt.Println("Hello, playground", bucketCnt)
}
Go 1.13 Release Notes (September 2019)
Changes to the language
Per the signed shift counts proposal Go 1.13 removes the
restriction that a shift count must be unsigned. This change
eliminates the need for many artificial uint conversions, solely
introduced to satisfy this (now removed) restriction of the << and >>
operators.
invalid operation: 1 << bucketCntBits (shift count type int, must be unsigned integer)
这不再是 Go 1.13(2019 年 9 月)及更高版本的错误。
你的榜样,
package main
import "fmt"
func main() {
bucketCntBits := 3
bucketCnt := 1 << bucketCntBits
fmt.Println(bucketCnt)
}
输出:
$ go version
go version devel +66ff373911 Sat Aug 24 01:11:56 2019 +0000 linux/amd64
$ go run shift.go
8
或者,您可以将类型 uint8
、uint16
、uint32
或 uint64
转换为整数文字。例如,
func main() {
bucketCntBits := uint32(3)
bucketCnt := 1 << bucketCntBits
fmt.Println(bucketCnt)
}
输出:
8
我在 go 中执行移位操作时遇到错误 invalid operation: 1 << bucketCntBits (shift count type int, must be unsigned integer)
尝试在 main()
正文中声明文字时出错
失败的文字示例:https://play.golang.org/p/EqI-yag5yPp
func main() {
bucketCntBits := 3 // <---- This doesn't work
bucketCnt := 1 << bucketCntBits
fmt.Println("Hello, playground", bucketCnt)
}
当我将班次计数声明为常量时,班次运算符起作用了。 工作常数示例:https://play.golang.org/p/XRLL4FR8ZEl
const (
bucketCntBits = 3 // <---- This works
)
func main() {
bucketCnt := 1 << bucketCntBits
fmt.Println("Hello, playground", bucketCnt)
}
为什么常量有效而文字对移位运算符无效?
使用 math/bits
包你可以这样做:
package main
import (
"fmt"
"math/bits"
)
func main() {
bucketCntBits := 3
bucketCnt := bits.RotateLeft(1, bucketCntBits)
fmt.Println("Hello, playground", bucketCnt)
}
Go 1.13 Release Notes (September 2019)
Changes to the language
Per the signed shift counts proposal Go 1.13 removes the restriction that a shift count must be unsigned. This change eliminates the need for many artificial uint conversions, solely introduced to satisfy this (now removed) restriction of the << and >> operators.
invalid operation: 1 << bucketCntBits (shift count type int, must be unsigned integer)
这不再是 Go 1.13(2019 年 9 月)及更高版本的错误。
你的榜样,
package main
import "fmt"
func main() {
bucketCntBits := 3
bucketCnt := 1 << bucketCntBits
fmt.Println(bucketCnt)
}
输出:
$ go version
go version devel +66ff373911 Sat Aug 24 01:11:56 2019 +0000 linux/amd64
$ go run shift.go
8
或者,您可以将类型 uint8
、uint16
、uint32
或 uint64
转换为整数文字。例如,
func main() {
bucketCntBits := uint32(3)
bucketCnt := 1 << bucketCntBits
fmt.Println(bucketCnt)
}
输出:
8