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


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

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

用法:

func Sub(a, b, borrow uint) (diff, borrowOut uint)

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

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



相關用法


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