當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang bits.Sub32()用法及代碼示例

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



相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 bits.Sub32() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。