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


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

Golang中的bits.Sub64()函數用於查找a,b與借位之差,即diff = a-b-借位,這裏的借位必須為0或1;否則,行為是不確定的。要訪問此函數,需要在程序中導入math /bits程序包。無論如何,借款輸出的返回值將始終為0或1。

用法:

func Sub64(a, b, borrow uint64) (diff, borrowOut uint64)

參數:此函數采用uint64類型的三個參數,即a,b和借位。借位參數的值為1或0。

返回值:此函數返回uint64類型的兩個值,即diff和roweOut。此處diff包含a-b-借位的結果,並且roweOut為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



相關用法


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