Golang中的bits.Sub32()函數用於查找a,b和借位之差,即diff = a-b-借位。這裏的借位必須為0或1;否則,行為是不確定的。要訪問此函數,需要在程序中導入math /bits程序包。無論如何,借款輸出的返回值將始終為0或1。
用法:
func Sub32(a, b, borrow uint32) (diff, borrowOut uint32)
參數:此函數采用uint32類型的三個參數,即a,b和借位。借位參數的值為1或0。
返回值:此函數返回uint32類型的兩個值,即diff和roweOut。此處diff包含a-b-借位的結果,並且roweOut為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
相關用法
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 bits.Sub32() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。