Go語言提供了對位的內置支持,以借助bits包對預聲明的無符號整數類型實現位計數和操作函數。該程序包提供RotateLeft16()函數,該函數用於向左旋轉(k mod 16)位,向右旋轉k位,您需要調用RotateLeft16(a,-k)。要訪問RotateLeft16()函數,您需要借助import關鍵字在程序中添加math /bits包。
用法:
func RotateLeft16(a uint16, k int) uint16
參數:此函數采用兩個參數,即uint16類型的a和int類型的k。
返回值:此函數返回uint16類型的旋轉(左或右)值。
範例1:
// Golang program to illustrate bits.RotateLeft16() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Using RotateLeft16() function
var a uint16 = 6
b:= bits.RotateLeft16(a, 5)
fmt.Printf("Original:%b", a)
fmt.Printf("\nAfter Rotation(Left):%b", b)
}
輸出:
Original:110 After Rotation(Left):11000000
範例2:
// Golang program to illustrate bits.RotateLeft16() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Using RotateLeft16() function
var a uint16 = 5
b:= bits.RotateLeft16(a, -1)
fmt.Printf("Original:%b", a)
fmt.Printf("\nAfter Rotation(Right):%b", b)
}
輸出:
Original:101 After Rotation(Right):1000000000000010
相關用法
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
- Golang time.Round()用法及代碼示例
- Golang reflect.AppendSlice()用法及代碼示例
- Golang reflect.ChanOf()用法及代碼示例
- Golang flag.Bool()用法及代碼示例
- Golang time.Sleep()用法及代碼示例
- Golang time.Time.Year()用法及代碼示例
- Golang reflect.DeepEqual()用法及代碼示例
- Golang reflect.Indirect()用法及代碼示例
- Golang reflect.CanAddr()用法及代碼示例
- Golang reflect.CanInterface()用法及代碼示例
- Golang reflect.CanSet()用法及代碼示例
- Golang reflect.Cap()用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 bits.RotateLeft16() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。