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