Golang bits.Div32() 函数及示例
Go 语言提供了 bits 包,通过这个包可以实现预定义无符号整数类型的位计数和位操作功能。bits 包提供了 Div32() 函数, 用于查找 (a, b) 除以 c 的商和余数,即,q = (a, b)/c, r = (a, b)%c,并将被除数位的上半部分存储在参数 a 中,下半部分存储在参数 b 中。如果 c == 0(除以零)或 c <= a(商溢出),则该函数会抛出 panic 异常。要访问 Div32() 函数,您需要在程序中使用 import 关键字添加一个 math / bits 包。
语法:
func Div32(a, b, c uint32) (q, r uint32)
参数: 此函数需要三个 uint32 类型的参数,即 a、b 和 c。
返回值: 该函数返回两个 uint32 类型的值,即 q 和 r。其中 q 是商,r 是余数。
示例1:
// Golang program to illustrate bits.Div32() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Finding quotient and remainder
// Using Div32() function
q, r := bits.Div32(10, 12, 11)
fmt.Println("Quotient:", q)
fmt.Println("Remainder:", r)
}
输出:
商: 3904515724
余数: 8
示例2:
// Golang program to illustrate bits.Div32() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Finding quotient and remainder
// Using Div32() function
var a, b, c uint32 = 1, 13, 5
q, r := bits.Div32(a, b, c)
fmt.Println("Number 1:", a)
fmt.Println("Number 2:", b)
fmt.Println("Number 3:", c)
fmt.Println("Quotient:", q)
fmt.Println("Remainder:", r)
}
输出:
数字1: 1
数字2: 13
数字3: 5
商: 858993461
余数: 4
极客教程