GO語言"math/bits"包中"Sub64"函數的用法及代碼示例。
用法:
func Sub64(x, y, borrow uint64)(diff, borrowOut uint64)
Sub64 返回 x、y 和借位的差:diff = x - y - 借位。借位輸入必須為 0 或 1;否則行為未定義。 borrowOut 輸出保證為 0 或 1。
此函數的執行時間不依賴於輸入。
例子:
package main
import (
"fmt"
"math/bits"
)
func main() {
// First number is 33<<64 + 23
n1 := []uint64{33, 23}
// Second number is 21<<64 + 12
n2 := []uint64{21, 12}
// Sub them together without producing carry.
d1, carry := bits.Sub64(n1[1], n2[1], 0)
d0, _ := bits.Sub64(n1[0], n2[0], carry)
nsum := []uint64{d0, d1}
fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
// First number is 3<<64 + 9223372036854775807
n1 = []uint64{3, 0x7fffffffffffffff}
// Second number is 1<<64 + 9223372036854775808
n2 = []uint64{1, 0x8000000000000000}
// Sub them together producing carry.
d1, carry = bits.Sub64(n1[1], n2[1], 0)
d0, _ = bits.Sub64(n1[0], n2[0], carry)
nsum = []uint64{d0, d1}
fmt.Printf("%v - %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
}
輸出:
[33 23] - [21 12] = [12 11] (carry bit was 0) [3 9223372036854775807] - [1 9223372036854775808] = [1 18446744073709551615] (carry bit was 1)
相關用法
- GO Sub32用法及代碼示例
- GO Sum256用法及代碼示例
- GO Sum用法及代碼示例
- GO Scanner.Scan用法及代碼示例
- GO StreamWriter用法及代碼示例
- GO Split用法及代碼示例
- GO Server.Shutdown用法及代碼示例
- GO Slice用法及代碼示例
- GO StructTag.Lookup用法及代碼示例
- GO SplitAfter用法及代碼示例
- GO SectionReader用法及代碼示例
- GO Sin用法及代碼示例
- GO Sprintf用法及代碼示例
- GO Strings用法及代碼示例
- GO SendMail用法及代碼示例
- GO StructTag用法及代碼示例
- GO Stmt用法及代碼示例
- GO Sprint用法及代碼示例
- GO SpecialCase用法及代碼示例
- GO SectionReader.ReadAt用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Sub64。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。