Golang中的bits.Sub64()函数用于求a、b和借位的差值,即diff = a – b -borrow。这里的借位必须是0或1;否则,行为未定义。要访问此函数,需要在程序中导入 math/bits 包。在任何情况下, borrowOutput的返回值将始终为 0 或 1。
句法:
func Sub64(a, b, borrow uint64) (diff, borrowOut uint64)
参数:该函数需要三个uint64类型的参数,即a、b和借位。借用参数的值为 1 或 0。
返回值:该函数返回两个uint64 类型的值,即diff 和borrowOut。这里 diff 包含 a – b -borrow 的结果,而borrowOut 是 1 或 0。
示例 1:
// Golang program to illustrate bits.Sub64() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Finding diff and borrowOu
// of the specified numbers
// Using Sub64() function
nvalue_1, borrowOut := bits.Sub64(11, 5, 0)
fmt.Println("Diff:", nvalue_1)
fmt.Println("BorrowOut :", borrowOut)
}
输出:
Diff: 6
BorrowOut : 0
示例 2:在这里,您可以看到结果并不像我们将借位的值设为 7 那样异常。因此,如果我们采用 1 和 0 以外的借位输入,则行为将是未定义的。
// Golang program to illustrate bits.Sub64() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Finding diff and borrowOut
// of the specified numbers
// Using Sub64() function
var a, b, borrow uint64 = 12, 87, 7
Diff, borrowOut := bits.Sub64(a, b, borrow )
fmt.Println("Number 1:", a)
fmt.Println("Number 2:", b)
fmt.Println("Borrow :", borrow )
fmt.Println("Diff:", Diff)
fmt.Println("BorrowOut :", borrowOut )
}
输出:
Number 1: 12
Number 2: 87
Borrow : 7
Diff: 18446744073709551540
BorrowOut : 1