当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


GO Mul32用法及代码示例

GO语言"math/bits"包中"Mul32"函数的用法及代码示例。

用法:

func Mul32(x, y uint32)(hi, lo uint32)

Mul32 返回 x 和 y 的 64 位乘积:(hi, lo) = x * y,乘积位的上半部分在 hi 中返回,下半部分在 lo 中返回。

此函数的执行时间不依赖于输入。

例子:

package main

import (
    "fmt"
    "math/bits"
)

func main() {
    // First number is 0<<32 + 12
    n1 := []uint32{0, 12}
    // Second number is 0<<32 + 12
    n2 := []uint32{0, 12}
    // Multiply them together without producing overflow.
    hi, lo := bits.Mul32(n1[1], n2[1])
    nsum := []uint32{hi, lo}
    fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)

    // First number is 0<<32 + 2147483648
    n1 = []uint32{0, 0x80000000}
    // Second number is 0<<32 + 2
    n2 = []uint32{0, 2}
    // Multiply them together producing overflow.
    hi, lo = bits.Mul32(n1[1], n2[1])
    nsum = []uint32{hi, lo}
    fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
}

输出:

12 * 12 = [0 144]
2147483648 * 2 = [1 0]

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Mul32。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。