当前位置: 首页>>代码示例>>Golang>>正文


Golang Int.SetBit方法代码示例

本文整理汇总了Golang中math/big.Int.SetBit方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.SetBit方法的具体用法?Golang Int.SetBit怎么用?Golang Int.SetBit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在math/big.Int的用法示例。


在下文中一共展示了Int.SetBit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: MakeRandom

func MakeRandom(gen *rand.Rand, degree int) *Polynomial {
	if degree == 0 {
		return NewPolynomialFromInt(gen.Int63n(2))
	}

	coeffs := new(big.Int)
	// x^0 + x^1 + ... + x^n => n + 1 terms
	// However, only the first n terms are variable.  (x^n is fixed to
	// have degree n.)  Thus, we randomly generate the first n terms
	// and fix the final term x^n.
	numBits := degree
	numBlocks := numBits / 32
	for ii := 0; ii < numBlocks; ii++ {
		v := gen.Uint32()

		// Merge.
		bigV := big.NewInt(int64(v))
		coeffs.Lsh(coeffs, 32).Or(coeffs, bigV)
	}
	// Handle the remainder.
	numRemainingBits := uint(numBits % 32)
	if numRemainingBits > 0 {
		mask := (int64(1) << numRemainingBits) - 1
		v := int64(gen.Uint32()) & mask
		coeffs.Lsh(coeffs, numRemainingBits).Or(coeffs, big.NewInt(v))
	}
	coeffs.SetBit(coeffs, degree, 1)
	return NewPolynomial(uint(degree), coeffs)
}
开发者ID:lumjjb,项目名称:rabin,代码行数:29,代码来源:util.go

示例2: String

func (p *Polynomial) String() string {
	zero := new(big.Int)

	// As a special case, we need to handle the zero term.
	if p.degree == 0 && p.coeffs.Cmp(zero) == 0 {
		return "0"
	}

	terms := make([]string, 0)

	// We deal with sparse polynomials using big.Int's BitLen(), which
	// should be optimized for word-sized operations.  Thus, copy p and then
	// walk through the 1 bits, MSB first.
	coeffs := new(big.Int).Set(&p.coeffs)
	for coeffs.Cmp(zero) > 0 {
		deg := calcDegree(coeffs)
		if deg == 0 {
			terms = append(terms, "1")
		} else {
			terms = append(terms, fmt.Sprintf("x^%v", deg))
		}
		coeffs.SetBit(coeffs, int(deg), 0)
	}

	return strings.Join(terms, " + ")
}
开发者ID:lumjjb,项目名称:rabin,代码行数:26,代码来源:polynomial.go

示例3: Mul

func (z *Polynomial) Mul(x, y *Polynomial) *Polynomial {
	var zCoeffs *big.Int
	if z == x || z == y {
		// Ensure that we do not modify z if it's a parameter.
		zCoeffs = new(big.Int)
	} else {
		zCoeffs = &z.coeffs
		zCoeffs.SetInt64(0)
	}

	small, large := x, y
	if y.degree < x.degree {
		small, large = y, x
	}

	// Walk through small coeffs, shift large by the corresponding amount,
	// and accumulate in z.
	coeffs := new(big.Int).Set(&small.coeffs)
	zero := new(big.Int)
	for coeffs.Cmp(zero) > 0 {
		deg := calcDegree(coeffs)
		factor := new(big.Int).Lsh(&large.coeffs, deg)
		zCoeffs.Xor(zCoeffs, factor)

		// Prepare for next iteration.
		coeffs.SetBit(coeffs, int(deg), 0)
	}

	z.degree = calcDegree(zCoeffs)
	z.coeffs = *zCoeffs
	return z
}
开发者ID:lumjjb,项目名称:rabin,代码行数:32,代码来源:polynomial.go

示例4: main

func main() {
	c := exec.Command("ft", "1000000", "2", `17/91 78/85 19/51 23/38
        29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1`)
	c.Stderr = os.Stderr
	r, err := c.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}
	if err = c.Start(); err != nil {
		log.Fatal(err)
	}
	var n big.Int
	for primes := 0; primes < 20; {
		if _, err = fmt.Fscan(r, &n); err != nil {
			log.Fatal(err)
		}
		l := n.BitLen() - 1
		n.SetBit(&n, l, 0)
		if n.BitLen() == 0 && l > 1 {
			fmt.Printf("%d ", l)
			primes++
		}
	}
	fmt.Println()
}
开发者ID:travis1230,项目名称:RosettaCodeData,代码行数:25,代码来源:fractran-2.go

示例5: TestModPow2

func TestModPow2(t *testing.T) {
	const N = 1e3
	data := []struct{ e, m uint32 }{
		// e == 0 -> x == 0
		{0, 2},
		{0, 3},
		{0, 4},

		{1, 2},
		{1, 3},
		{1, 4},
		{1, 5},

		{2, 2},
		{2, 3},
		{2, 4},
		{2, 5},

		{3, 2},
		{3, 3},
		{3, 4},
		{3, 5},
		{3, 6},
		{3, 7},
		{3, 8},
		{3, 9},

		{4, 2},
		{4, 3},
		{4, 4},
		{4, 5},
		{4, 6},
		{4, 7},
		{4, 8},
		{4, 9},
	}

	var got big.Int
	f := func(e, m uint32) {
		x := ModPow2(e, m)
		exp := ModPow(2, e, m)
		got.SetInt64(0)
		got.SetBit(&got, int(x), 1)
		if got.Cmp(exp) != 0 {
			t.Fatalf("\ne %d, m %d\ng: %s\ne: %s", e, m, &got, exp)
		}
	}

	for _, v := range data {
		f(v.e, v.m)
	}

	rg, _ := mathutil.NewFC32(2, 1<<10, true)
	for i := 0; i < N; i++ {
		f(uint32(rg.Next()), uint32(rg.Next()))
	}
}
开发者ID:klizhentas,项目名称:acbuild,代码行数:57,代码来源:all_test.go

示例6: Div

// Sets q = x / y.  Returns q and the remainder r.
func (q *Polynomial) Div(x, y *Polynomial) (*Polynomial, *Polynomial) {
	zero := new(big.Int)
	if y.degree == 0 {
		// y is 0 or 1.
		if y.coeffs.Cmp(zero) == 0 {
			panic("divide by 0")
		}
		q.Set(x)
		r := NewPolynomial(0, zero)
		return q, r
	}
	if x.degree == 0 {
		// 0 / y = 0.
		if x.coeffs.Cmp(zero) == 0 {
			q.degree = 0
			q.coeffs.Set(zero)

			r := NewPolynomial(0, zero)
			return q, r
		}
	}

	var qCoeffs *big.Int
	// Be careful not to modify any of the input parameters.
	if q == x || q == y {
		qCoeffs = new(big.Int)
	} else {
		// Re-use q to minimize allocations.
		qCoeffs = &q.coeffs
		qCoeffs.SetInt64(0)
	}

	// The remainder, which is x initially.
	r := new(Polynomial).Set(x)

	// This is just elementary long division in GF(2).
	for r.degree >= y.degree {
		// s(x) = y(x) * x^{shift_len}
		// Then, r'(x) = r(x) - s(x)

		// Record the new component in the quotient.
		shiftLen := int(r.degree - y.degree)
		qCoeffs.SetBit(qCoeffs, shiftLen, 1)

		sCoeffs := new(big.Int).Lsh(&y.coeffs, uint(shiftLen))

		// Determine r'(x) by subtracting s(x) from the remainder.
		r.coeffs.Xor(&r.coeffs, sCoeffs)
		r.degree = calcDegree(&r.coeffs)
	}
	// r.degree < y.degree at this point.

	q.coeffs = *qCoeffs
	q.degree = calcDegree(&q.coeffs)
	return q, r
}
开发者ID:lumjjb,项目名称:rabin,代码行数:57,代码来源:polynomial.go

示例7: regionQuery

func regionQuery(p int, ret *big.Int, dist *mat64.Dense, eps float64) *big.Int {
	rows, _ := dist.Dims()
	// Return any points within the Eps neighbourhood
	for i := 0; i < rows; i++ {
		if dist.At(p, i) <= eps {
			ret = ret.SetBit(ret, i, 1) // Mark as neighbour
		}
	}
	return ret
}
开发者ID:CTLife,项目名称:golearn,代码行数:10,代码来源:dbscan.go

示例8: NewPolynomialFromCoeffs

// coeffs must contain the degree of each non-zero element.
func NewPolynomialFromCoeffs(degrees []uint) *Polynomial {
	maxDegree := uint(0)
	coeffs := new(big.Int)
	for _, deg := range degrees {
		coeffs.SetBit(coeffs, int(deg), 1)
		if deg > maxDegree {
			maxDegree = deg
		}
	}
	return &Polynomial{degree: maxDegree, coeffs: *coeffs}
}
开发者ID:lumjjb,项目名称:rabin,代码行数:12,代码来源:polynomial.go

示例9: benchmarkMod

func benchmarkMod(b *testing.B, w, exp uint32) {
	b.StopTimer()
	var n, mod big.Int
	n.Rand(rand.New(rand.NewSource(1)), New(w))
	n.SetBit(&n, int(w), 1)
	runtime.GC()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		Mod(&mod, &n, exp)
	}
}
开发者ID:klizhentas,项目名称:acbuild,代码行数:11,代码来源:all_test.go

示例10: 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)
}
开发者ID:kenpu,项目名称:go-loglogcounter,代码行数:11,代码来源:loglog.go

示例11: BitFill

// Set z to x, but with the bit-field from bit i
// up or down to bit j filled with bit value b.
func BitFill(z, x *big.Int, i, j int, b uint) {
	if z != x {
		z.Set(x)
	}
	inc := 1
	if i > j {
		inc = -1
	}
	for ; i != j; i += inc {
		z.SetBit(z, i, b)
	}
}
开发者ID:LegoShrimp,项目名称:crypto,代码行数:14,代码来源:bits.go

示例12: main

func main() {

	var bitVector big.Int

	var number int64 = 15
	var bitValue uint = 0
	var bitIndex int = 3

	//Setting the 3rd bit of 15 to zero
	bitVector.SetBit(big.NewInt(number), bitIndex, bitValue)

	fmt.Printf("\nSetting the bit at %d of %b to %b \n", bitIndex, number, bitValue)

	fmt.Printf("\nThe new value %d => %b \n\n", bitVector.Uint64(), bitVector.Uint64())
}
开发者ID:nrshrivatsan,项目名称:meg,代码行数:15,代码来源:bit-twidling.go

示例13: instantiateINTEGER

func instantiateINTEGER(inst *Instance, data interface{}, p *pathNode) (*Instance, error) {
	switch data := data.(type) {
	case *UnmarshalledPrimitive:
		if len(data.Data) == 0 {
			return nil, fmt.Errorf("%vAttempt to instantiate INTEGER from empty DER data", p)
		}
		var b big.Int
		b.SetBytes(data.Data)
		if data.Data[0]&128 != 0 { // negative number
			var x big.Int
			x.SetBit(&x, len(data.Data)*8, 1)
			x.Sub(&x, &b)
			b.Neg(&x)
		}
		return instantiateINTEGER(inst, &b, p)
	case *Instance:
		inst.value = data.value
	case *big.Int:
		i := int(data.Int64())
		if big.NewInt(int64(i)).Cmp(data) == 0 {
			inst.value = i // store as int if possible
		} else {
			inst.value = data // use *big.Int if necessary
		}
	case int:
		inst.value = data
	case float64:
		if math.Floor(data) != data {
			return nil, fmt.Errorf("%vAttempt to instantiate INTEGER with non-integral float64: %v", p, data)
		}
		inst.value = int(data)
	case string:
		if i, found := inst.namedints[data]; found {
			inst.value = i
		} else {
			bi := new(big.Int)
			_, ok := bi.SetString(data, 10)
			if ok {
				return instantiateINTEGER(inst, bi, p)
			} else {
				return nil, fmt.Errorf("%vAttempt to instantiate INTEGER/ENUMERATED from illegal string: %v", p, data)
			}
		}
	default:
		return nil, instantiateTypeError(p, "INTEGER", data)
	}
	return inst, nil
}
开发者ID:tomzhang,项目名称:certifidog,代码行数:48,代码来源:instantiate.go

示例14: UnBWST

// Compute the inverse of the Burrows-Wheeler-Scott transform. This is done
// out-of-place.
func UnBWST(b []byte) []byte {
	sorted := make([]byte, len(b))
	copy(sorted, b)
	sort.Sort(bytesorter(sorted))
	used := new(big.Int)
	used.SetBit(used, len(b), 1) // reserve capacity
	links := make([]int, len(b))
	// TODO: use O(lg(N)) search in sorted instead of O(N) search in b
	for i, c := range sorted {
		// find the first unused index in b of c
		for j, c2 := range b {
			if c == c2 && used.Bit(j) == 0 {
				links[i] = j
				used.SetBit(used, j, 1)
				break
			}
		}
	}
	// We need to know once again whether each byte is used, so instead of
	// resetting the bitset or using more memory, we can just ask whether it's
	// unused.
	unused := used
	words := multibytesorter{}
	for i := range sorted {
		if unused.Bit(i) == 1 {
			word := []byte{}
			x := i
			for unused.Bit(x) == 1 {
				word = append(word, sorted[x])
				unused.SetBit(unused, x, 0)
				x = links[x]
			}
			words = append(words, nil)
			copy(words[1:], words)
			words[0] = word
		}
	}
	if !sort.IsSorted(words) {
		sort.Sort(words)
	}
	x := len(b)
	s := make([]byte, len(b))
	for _, word := range words {
		x -= len(word)
		copy(s[x:], word)
	}
	return s
}
开发者ID:zephyrtronium,项目名称:bwst,代码行数:50,代码来源:unbwst.go

示例15: Test_PowerTable

func Test_PowerTable(t *testing.T) {
	p := NewPolynomialFromUint64(kIrreduciblePolyDegree, kIrreduciblePolyCoeffs)
	// Offset by a little bit to test forwarding.
	pt := makePowerTable(70)
	for ii := 0; ii < len(pt); ii++ {
		coeffs := new(big.Int)
		coeffs.SetBit(coeffs, 70+ii, 1)
		powerPoly := NewPolynomialFromBigInt(coeffs)
		powerPoly.Mod(powerPoly, p)
		_, cmpCoeffs := powerPoly.Uint64()
		if cmpCoeffs != pt[ii] {
			t.Error(fmt.Sprintf("mismatch term %d (0x%x, 0x%x)",
				ii, cmpCoeffs, pt[ii]))
		}
	}
}
开发者ID:lumjjb,项目名称:rabin,代码行数:16,代码来源:util_test.go


注:本文中的math/big.Int.SetBit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。