GO語言"math/bits"包中"Add32"函數的用法及代碼示例。
用法:
func Add32(x, y, carry uint32)(sum, carryOut uint32)
Add32 返回 x、y 和進位的總和:sum = x + y + 進位。進位輸入必須為 0 或 1;否則行為未定義。進位輸出保證為 0 或 1。
此函數的執行時間不依賴於輸入。
例子:
package main
import (
"fmt"
"math/bits"
)
func main() {
// First number is 33<<32 + 12
n1 := []uint32{33, 12}
// Second number is 21<<32 + 23
n2 := []uint32{21, 23}
// Add them together without producing carry.
d1, carry := bits.Add32(n1[1], n2[1], 0)
d0, _ := bits.Add32(n1[0], n2[0], carry)
nsum := []uint32{d0, d1}
fmt.Printf("%v + %v = %v (carry bit was %v)\n", n1, n2, nsum, carry)
// First number is 1<<32 + 2147483648
n1 = []uint32{1, 0x80000000}
// Second number is 1<<32 + 2147483648
n2 = []uint32{1, 0x80000000}
// Add them together producing carry.
d1, carry = bits.Add32(n1[1], n2[1], 0)
d0, _ = bits.Add32(n1[0], n2[0], carry)
nsum = []uint32{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 2147483648] + [1 2147483648] = [3 0] (carry bit was 1)
相關用法
- GO Add64用法及代碼示例
- 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大神的英文原創作品 Add32。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。