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