From e1b7c497af5bee3ee7a9a9e6e80f6aa943634704 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 6 Nov 2021 16:07:51 -0500 Subject: [PATCH] uint256: Add basic usage example. This adds an example of calculating the result of dividing a max unsigned 256-bit integer by a max unsigned 128-bit integer and outputting that result in hex with leading zeros. This is part of a series of commits to fully implement the uint256 package. --- .../primitives/uint256/example_test.go | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 internal/staging/primitives/uint256/example_test.go diff --git a/internal/staging/primitives/uint256/example_test.go b/internal/staging/primitives/uint256/example_test.go new file mode 100644 index 0000000000..edf656bf68 --- /dev/null +++ b/internal/staging/primitives/uint256/example_test.go @@ -0,0 +1,24 @@ +// Copyright (c) 2021 The Decred developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package uint256_test + +import ( + "fmt" + + "github.com/decred/dcrd/internal/staging/primitives/uint256" +) + +// This example demonstrates calculating the result of dividing a max unsigned +// 256-bit integer by a max unsigned 128-bit integer and outputting that result +// in hex with leading zeros. +func Example_basicUsage() { + // Calculate maxUint256 / maxUint128 and output it in hex with leading zeros. + maxUint128 := new(uint256.Uint256).SetUint64(1).Lsh(128).SubUint64(1) + result := new(uint256.Uint256).Not().Div(maxUint128) + fmt.Printf("result: %064x\n", result) + + // Output: + // result: 0000000000000000000000000000000100000000000000000000000000000001 +}