GO語言"math/bits"包中"Add64"函數的用法及代碼示例。
用法:
func Add64(x, y, carry uint64)(sum, carryOut uint64)
Add64 返回 x、y 和進位的總和:sum = x + y + 進位。進位輸入必須為 0 或 1;否則行為未定義。進位輸出保證為 0 或 1。
此函數的執行時間不依賴於輸入。
例子:
package main
import (
"fmt"
"math/bits"
)
func main() {
// First number is 33<<64 + 12
n1 := []uint64{33, 12}
// Second number is 21<<64 + 23
n2 := []uint64{21, 23}
// Add them together without producing carry.
d1, carry := bits.Add64(n1[1], n2[1], 0)
d0, _ := bits.Add64(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 1<<64 + 9223372036854775808
n1 = []uint64{1, 0x8000000000000000}
// Second number is 1<<64 + 9223372036854775808
n2 = []uint64{1, 0x8000000000000000}
// Add them together producing carry.
d1, carry = bits.Add64(n1[1], n2[1], 0)
d0, _ = bits.Add64(n1[0], n2[0], carry)
nsum = []uint64{d0, d1}
fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
}
輸出:
[33 12] + [21 23] = [54 35] (carry bit was 0) [1 9223372036854775808] + [1 9223372036854775808] = [3 0] (carry bit was 1)
相關用法
- GO Add32用法及代碼示例
- GO AppendRune用法及代碼示例
- GO Atan2用法及代碼示例
- GO AppendQuoteRune用法及代碼示例
- GO Atan用法及代碼示例
- GO AppendInt用法及代碼示例
- GO Acos用法及代碼示例
- GO Acosh用法及代碼示例
- GO AppendBool用法及代碼示例
- GO Asinh用法及代碼示例
- GO As用法及代碼示例
- GO Asin用法及代碼示例
- GO Atoi用法及代碼示例
- GO AppendQuoteToASCII用法及代碼示例
- GO AppendFloat用法及代碼示例
- GO AppendQuoteRuneToASCII用法及代碼示例
- GO After用法及代碼示例
- GO AppendQuote用法及代碼示例
- GO AppendUint用法及代碼示例
- GO Atanh用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Add64。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。