Skip to content

Commit ef891e1

Browse files
bmkesslermvdan
authored andcommitted
math/big: implement Int.TrailingZeroBits
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 <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 14a58d6 commit ef891e1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/math/big/int.go

+6
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ func (x *Int) BitLen() int {
448448
return x.abs.bitLen()
449449
}
450450

451+
// TrailingZeroBits returns the number of consecutive least significant zero
452+
// bits of |x|.
453+
func (x *Int) TrailingZeroBits() uint {
454+
return x.abs.trailingZeroBits()
455+
}
456+
451457
// Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z.
452458
// If m == nil or m == 0, z = x**y unless y <= 0 then z = 1.
453459
//

src/math/big/int_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,31 @@ func TestBitSet(t *testing.T) {
13351335
}
13361336
}
13371337

1338+
var tzbTests = []struct {
1339+
in string
1340+
out uint
1341+
}{
1342+
{"0", 0},
1343+
{"1", 0},
1344+
{"-1", 0},
1345+
{"4", 2},
1346+
{"-8", 3},
1347+
{"0x4000000000000000000", 74},
1348+
{"-0x8000000000000000000", 75},
1349+
}
1350+
1351+
func TestTrailingZeroBits(t *testing.T) {
1352+
for i, test := range tzbTests {
1353+
in, _ := new(Int).SetString(test.in, 0)
1354+
want := test.out
1355+
got := in.TrailingZeroBits()
1356+
1357+
if got != want {
1358+
t.Errorf("#%d: got %v want %v", i, got, want)
1359+
}
1360+
}
1361+
}
1362+
13381363
func BenchmarkBitset(b *testing.B) {
13391364
z := new(Int)
13401365
z.SetBit(z, 512, 1)

0 commit comments

Comments
 (0)