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