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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。