Golang bits.Sub64() 函数及示例

Golang bits.Sub64() 函数及示例

Golang 中的 bits.Sub64() 函数用于查找 a、b 和借位 borrow 的差异,即 diff = a – b – borrow。这里的 borrow 必须是 0 或 1;否则,行为是未定义的。为了访问这个函数,需要在程序中导入 math/bits 包。在任何情况下,borrowOutput 的返回值始终是 0 或 1。

语法:

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

参数: 此函数接受三个 uint64 类型的参数,即 a、b 和 borrow。borrow 参数的值为 1 或 0。

返回值: 此函数返回两个 uint64 类型的值,即 diff 和 borrowOut。这里的 diff 包含 a – b – borrow 的结果,而 borrowOut 是 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程