]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: implement Int.TrailingZeroBits
authorBrian Kessler <brian.m.kessler@gmail.com>
Fri, 1 Feb 2019 05:24:00 +0000 (22:24 -0700)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 12 Mar 2019 13:18:27 +0000 (13:18 +0000)
Implemented via the underlying nat.trailingZeroBits.

Fixes #29578

Change-Id: If9876c5a74b107cbabceb7547bef4e44501f6745
Reviewed-on: https://go-review.googlesource.com/c/go/+/160681
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/math/big/int.go
src/math/big/int_test.go

index 8c1a54a9c6f7de17cfcbfe60e9ebc11be904ff77..eb0285c48f4aa36482133194f1f0482c9c0925c8 100644 (file)
@@ -448,6 +448,12 @@ func (x *Int) BitLen() int {
        return x.abs.bitLen()
 }
 
+// TrailingZeroBits returns the number of consecutive least significant zero
+// bits of |x|.
+func (x *Int) TrailingZeroBits() uint {
+       return x.abs.trailingZeroBits()
+}
+
 // Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z.
 // If m == nil or m == 0, z = x**y unless y <= 0 then z = 1.
 //
index 48d08d0e7ebadbb7f7216ae8b5ac3e9af874cd0c..2435b3610c03bb9b9e1d428e2d59af9e65cc9748 100644 (file)
@@ -1335,6 +1335,31 @@ func TestBitSet(t *testing.T) {
        }
 }
 
+var tzbTests = []struct {
+       in  string
+       out uint
+}{
+       {"0", 0},
+       {"1", 0},
+       {"-1", 0},
+       {"4", 2},
+       {"-8", 3},
+       {"0x4000000000000000000", 74},
+       {"-0x8000000000000000000", 75},
+}
+
+func TestTrailingZeroBits(t *testing.T) {
+       for i, test := range tzbTests {
+               in, _ := new(Int).SetString(test.in, 0)
+               want := test.out
+               got := in.TrailingZeroBits()
+
+               if got != want {
+                       t.Errorf("#%d: got %v want %v", i, got, want)
+               }
+       }
+}
+
 func BenchmarkBitset(b *testing.B) {
        z := new(Int)
        z.SetBit(z, 512, 1)