本文整理汇总了Golang中math/big.Int.And方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.And方法的具体用法?Golang Int.And怎么用?Golang Int.And使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.And方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: pow
// pow sets d to x ** y and returns z.
func (z *Big) pow(x *Big, y *big.Int) *Big {
switch {
case y.Sign() < 0, (x.ez() || y.Sign() == 0):
return z.SetMantScale(1, 0)
case y.Cmp(oneInt) == 0:
return z.Set(x)
case x.ez():
if x.isOdd() {
return z.Set(x)
}
z.form = zero
return z
}
x0 := new(Big).Set(x)
y0 := new(big.Int).Set(y)
ret := New(1, 0)
var odd big.Int
for y0.Sign() > 0 {
if odd.And(y0, oneInt).Sign() != 0 {
ret.Mul(ret, x0)
}
y0.Rsh(y0, 1)
x0.Mul(x0, x0)
}
*z = *ret
return ret
}
示例2: TestModAdc
func TestModAdc(t *testing.T) {
A := new(big.Int)
B := new(big.Int)
C := new(big.Int)
Carry := new(big.Int)
Mask := new(big.Int)
for _, a := range numbers {
A.SetUint64(a)
for _, b := range numbers {
B.SetUint64(b)
for width := uint8(1); width < 64; width++ {
carry := b
c := mod_adc(a, width, &carry)
C.Add(A, B)
Carry.Rsh(C, uint(width))
expectedCarry := Carry.Uint64()
Mask.SetUint64(uint64(1)<<width - 1)
C.And(C, Mask)
expected := C.Uint64()
if c != expected || expectedCarry != carry {
t.Fatalf("adc(%d,%d,%d): Expecting %d carry %d but got %d carry %d", a, b, width, expected, expectedCarry, c, carry)
}
}
}
}
}
示例3: U256
func U256(x *big.Int) *big.Int {
//if x.Cmp(Big0) < 0 {
// return new(big.Int).Add(tt256, x)
// }
x.And(x, tt256m1)
return x
}
示例4: Len
// Len returns the number of elements in the set.
// Its complexity is O(n).
func (s *Set) Len() int {
var l int
zero := big.NewInt(0)
v := new(big.Int).Set((*big.Int)(s))
for l = 0; v.Cmp(zero) != 0; l++ {
vMinusOne := new(big.Int).Sub(v, big.NewInt(1))
v.And(v, vMinusOne)
}
return l
}
示例5: Size
// Size returns the number of integers in the set.
// Alogrithm stolen from github.com/kisielk/bigset
func (set *BitSet) Size() int {
var l int
zero := big.NewInt(0)
v := new(big.Int).Set(set.data)
for l = 0; v.Cmp(zero) != 0; l++ {
vMinusOne := new(big.Int).Sub(v, big.NewInt(1))
v.And(v, vMinusOne)
}
return l
}
示例6: SetEntry
func SetEntry(hv *big.Int, numBits int, entry *Entry) {
mask := new(big.Int)
for i := 0; i < numBits; i++ {
mask.SetBit(mask, i, 1)
}
mv := uint64(mask.And(mask, hv).Int64())
hv2 := new(big.Int).Rsh(hv, uint(numBits))
entry.MValue = mv
entry.Rank = rank(hv2)
}
示例7: NetworkAddress
// NetworkAddress returns an IPv6Network of the IPv6Addr's network address.
func (ipv6 IPv6Addr) NetworkAddress() IPv6Network {
addr := new(big.Int)
addr.SetBytes((*ipv6.Address).Bytes())
mask := new(big.Int)
mask.SetBytes(*ipv6.NetIPMask())
netAddr := new(big.Int)
netAddr.And(addr, mask)
return IPv6Network(netAddr)
}
示例8: Int
// z.Int() returns a representation of z, truncated to be an int of
// length bits. Valid values for bits are 8, 16, 32, 64. Result is
// otherwise undefined If a truncation occurs, the decimal part is
// dropped and the conversion continues as usual. truncation will be
// true If an overflow occurs, the result is equivelant to a cast of
// the form int32(x). overflow will be true.
func (z *BigComplex) Int(bits int) (_ int64, truncation, overflow bool) {
var integer *BigComplex
integer, truncation = z.Integer()
res := new(big.Int).Set(integer.Re.Num())
// Numerator must fit in bits - 1, with 1 bit left for sign
if overflow = res.BitLen() > bits-1; overflow {
var mask uint64 = ^uint64(0) >> uint(64-bits)
res.And(res, new(big.Int).SetUint64(mask))
}
return res.Int64(), truncation, overflow
}
示例9: trunc
// trunc truncates a value to the range of the given type.
func (t *_type) trunc(x *big.Int) *big.Int {
r := new(big.Int)
m := new(big.Int)
m.Lsh(one, t.bits)
m.Sub(m, one)
r.And(x, m)
if t.signed && r.Bit(int(t.bits)-1) == 1 {
m.Neg(one)
m.Lsh(m, t.bits)
r.Or(r, m)
}
return r
}
示例10: intToBase64
//intToBase64 makes string from int.
func intToBase64(n *big.Int) string {
var result string
and := big.NewInt(0x3f)
var tmp, nn big.Int
nn.Set(n)
for nn.Cmp(big.NewInt(0)) > 0 {
bit := tmp.And(&nn, and).Uint64()
result += string(base64en[bit])
nn.Rsh(&nn, 6)
}
return result + string(base64en[0]*byte(86-len(result)))
}
示例11: ensure256
func ensure256(x *big.Int) {
//max, _ := big.NewInt(0).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639936", 0)
//if x.Cmp(max) >= 0 {
d := big.NewInt(1)
d.Lsh(d, 256).Sub(d, big.NewInt(1))
x.And(x, d)
//}
// Could have done this with an OR, but big ints are costly.
if x.Cmp(new(big.Int)) < 0 {
x.SetInt64(0)
}
}
示例12: binaryIntOp
func binaryIntOp(x *big.Int, op token.Token, y *big.Int) interface{} {
var z big.Int
switch op {
case token.ADD:
return z.Add(x, y)
case token.SUB:
return z.Sub(x, y)
case token.MUL:
return z.Mul(x, y)
case token.QUO:
return z.Quo(x, y)
case token.REM:
return z.Rem(x, y)
case token.AND:
return z.And(x, y)
case token.OR:
return z.Or(x, y)
case token.XOR:
return z.Xor(x, y)
case token.AND_NOT:
return z.AndNot(x, y)
case token.SHL:
// The shift length must be uint, or untyped int and
// convertible to uint.
// TODO 32/64bit
if y.BitLen() > 32 {
panic("Excessive shift length")
}
return z.Lsh(x, uint(y.Int64()))
case token.SHR:
if y.BitLen() > 32 {
panic("Excessive shift length")
}
return z.Rsh(x, uint(y.Int64()))
case token.EQL:
return x.Cmp(y) == 0
case token.NEQ:
return x.Cmp(y) != 0
case token.LSS:
return x.Cmp(y) < 0
case token.LEQ:
return x.Cmp(y) <= 0
case token.GTR:
return x.Cmp(y) > 0
case token.GEQ:
return x.Cmp(y) >= 0
}
panic("unreachable")
}
示例13: bigIntAnd
func bigIntAnd(oldValues [][]byte, newValues [][]byte, w float64) (result [][]byte) {
var sum *big.Int
if oldValues != nil {
sum = new(big.Int).SetBytes(oldValues[0])
for _, b := range newValues {
sum.And(sum, new(big.Int).Mul(common.DecodeBigInt(b), big.NewInt(int64(w))))
}
} else {
sum = new(big.Int).Mul(new(big.Int).SetBytes(newValues[0]), big.NewInt(int64(w)))
for _, b := range newValues[1:] {
sum.And(sum, new(big.Int).Mul(common.DecodeBigInt(b), big.NewInt(int64(w))))
}
}
return [][]byte{sum.Bytes()}
}
示例14: Uint
// z.Uint() returns a representation of z truncated to be a uint of
// length bits. Valid values for bits are 0, 8, 16, 32, 64. The
// returned result is otherwise undefined. If a truncation occurs, the
// decimal part is dropped and the conversion continues as
// usual. Return values truncation and overflow will be true if an
// overflow occurs. The result is equivelant to a cast of the form
// uint32(x).
func (z *BigComplex) Uint(bits int) (_ uint64, truncation, overflow bool) {
var integer *BigComplex
integer, truncation = z.Integer()
res := new(big.Int).Set(integer.Re.Num())
var mask uint64 = ^uint64(0) >> uint(64-bits)
if overflow = res.BitLen() > bits; overflow {
res.And(res, new(big.Int).SetUint64(mask))
res = new(big.Int).And(res, new(big.Int).SetUint64(mask))
}
r := res.Uint64()
if res.Sign() < 0 {
overflow = true
r = (^r + 1) & mask
}
return r, truncation, overflow
}
示例15: pad
func pad(v *big.Int) []byte {
buf := make([]byte, SRP_KEY_SIZE)
var m big.Int
var n *big.Int
n = big.NewInt(0)
n = n.Add(n, v)
for i, _ := range buf {
buf[i] = byte(m.And(m.SetInt64(255), n).Int64())
n = n.Div(n, m.SetInt64(256))
}
// reverse
for i, j := 0, len(buf)-1; i < j; i, j = i+1, j-1 {
buf[i], buf[j] = buf[j], buf[i]
}
return buf
}