當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Mul64用法及代碼示例

GO語言"math/bits"包中"Mul64"函數的用法及代碼示例。

用法:

func Mul64(x, y uint64)(hi, lo uint64)

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

此函數的執行時間不依賴於輸入。

例子:

package main

import (
	"fmt"
	"math/bits"
)

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

	// First number is 0<<64 + 9223372036854775808
	n1 = []uint64{0, 0x8000000000000000}
	// Second number is 0<<64 + 2
	n2 = []uint64{0, 2}
	// Multiply them together producing overflow.
	hi, lo = bits.Mul64(n1[1], n2[1])
	nsum = []uint64{hi, lo}
	fmt.Printf("%v * %v = %v\n", n1[1], n2[1], nsum)
}

輸出:

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

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Mul64。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。