当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。