// More generic version of ShiftOut which shifts out n of data from value. The
// value shifted out is always the lowest n bits of the value, but 'order'
// determines whether the msb or lsb from that value are shifted first
193018
func ShiftOutSize(dataPin Pin, clockPin Pin, value uint, order BitShiftOrder, n uint) error {
	bit := uint(0)
	v := value
	mask := uint(1) << (n - 1)
	for i := uint(0); i < n; i++ {
		// get the next bit
		if order == LSBFIRST {
			bit = v & 1
			v = v >> 1
		} else {
			bit = v & mask
			if bit != 0 {
				bit = 1
			}
			v = v << 1
		}
		// write to data pin
		e := DigitalWrite(dataPin, int(bit))
		if e != nil {
			return e
		}
		// pulse clock high and then low
		e = DigitalWrite(clockPin, HIGH)
		if e != nil {
			return e
		}
		DigitalWrite(clockPin, LOW)
	}
	return nil
}
475317
func Convert(input []byte, output []byte, fromEncoding string, toEncoding string) (bytesRead int, bytesWritten int, err error) {
	// create a temporary converter
	converter, err := NewConverter(fromEncoding, toEncoding)

	if err == nil {
		// call converter's Convert
		bytesRead, bytesWritten, err = converter.Convert(input, output)

		if err == nil {
			var shiftBytesWritten int

			// call Convert with a nil input to generate any end shift sequences
			_, shiftBytesWritten, err = converter.Convert(nil, output[bytesWritten:])

			// add shift bytes to total bytes
			bytesWritten += shiftBytesWritten
		}

		// close the converter
		converter.Close()
	}

	return
}
1054987
func SumOutputValues(outputs []*wire.TxOut) (totalOutput btcutil.Amount) {
	for _, txOut := range outputs {
		totalOutput += btcutil.Amount(txOut.Value)
	}
	return totalOutput
}
787231
func (this *state) isBetterThanOrEqualTo(other *state) bool {
	mySize := this.bitCount + (latchTable[this.mode][other.mode] >> 16)

	if other.bShiftByteCount > 0 && (this.bShiftByteCount == 0 || this.bShiftByteCount > other.bShiftByteCount) {
		mySize += 10 // Cost of entering Binary Shift mode.
	}
	return mySize <= other.bitCount
}
1042765
func SliceShiftRight(s1 []byte, bits uint) []byte {
	l := len(s1)
	s := make([]byte, l)

	r := bits / 8
	rs := bits - r*8

	for i := 0; i < l; i++ {
		// relative index
		ri := i - int(r)
		bl := byte(0)
		if ri >= 1 {
			bl = s1[ri-1] << (8 - rs)
		}

		bu := byte(0)
		if ri >= 0 {
			bu = s1[ri] >> rs
		}
		s[i] = bl | bu
	}

	return s
}
301597
func (s Uint8Slice) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}
1030004
func (z fermat) ShiftHalf(x fermat, k int, tmp fermat) {
	n := len(z) - 1
	if k%2 == 0 {
		z.Shift(x, k/2)
		return
	}
	u := (k - 1) / 2
	a := u + (3*_W/4)*n
	b := u + (_W/4)*n
	z.Shift(x, a)
	tmp.Shift(x, b)
	z.Sub(z, tmp)
}
473128
func setBit(n int, pos int) int {
	n |= (1 << uint(pos))
	return n
}
1094271
func (s Float64Slice) Less(i, j int) bool {
	return s[i] < s[j]
}
422599
func fmtB(dst EncodingBuffer, neg bool, mant uint64, exp int, flt *floatInfo) {
	// sign
	if neg {
		dst.WriteByte('-')
	}

	// mantissa
	formatBits(dst, mant, 10, false)

	// p
	dst.WriteByte('p')

	// ±exponent
	exp -= int(flt.mantbits)
	if exp >= 0 {
		dst.WriteByte('+')
	}
	formatBits(dst, uint64(exp), 10, exp < 0)

	return
}
542054
func (b *bitReader) fillFast() {
	if b.bitsRead < 32 {
		return
	}
	// Do single re-slice to avoid bounds checks.
	v := b.in[b.off-4 : b.off]
	low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
	b.value = (b.value << 32) | uint64(low)
	b.bitsRead -= 32
	b.off -= 4
}
620389
func (l *Logger) SwapOutput(infoLogger log.Logger) {
	l.Output.Swap(infoLogger)
}
672509
func rightShift(slice []byte, num int) {
	for i := len(slice) - num - 1; i >= 0; i-- {
		slice[num+i] = slice[i]
	}

	for i := 0; i < num; i++ {
		slice[i] = 0
	}
}
7431
func ShiftLeft(b []byte) []byte {
	l := len(b)
	if l == 0 {
		panic("shiftLeft requires a non-empty buffer.")
	}

	output := make([]byte, l)

	overflow := byte(0)
	for i := int(l - 1); i >= 0; i-- {
		output[i] = b[i] << 1
		output[i] |= overflow
		overflow = (b[i] & 0x80) >> 7
	}

	return output
}
107958
func NewTxOut(value int64, pkScript []byte) *TxOut {
	return &TxOut{
		Value:    value,
		PkScript: pkScript,
	}
}
1358094
func writeTxOut(w io.Writer, txo *wire.TxOut) error {
	var scratch [8]byte

	binary.BigEndian.PutUint64(scratch[:], uint64(txo.Value))
	if _, err := w.Write(scratch[:]); err != nil {
		return err
	}

	if err := wire.WriteVarBytes(w, 0, txo.PkScript); err != nil {
		return err
	}

	return nil
}
619432
func (a *decimal) Shift(k int) {
	switch {
	case a.nd == 0:
		// nothing to do: a == 0
	case k > 0:
		for k > maxShift {
			leftShift(a, maxShift)
			k -= maxShift
		}
		leftShift(a, uint(k))
	case k < 0:
		for k < -maxShift {
			rightShift(a, maxShift)
			k += maxShift
		}
		rightShift(a, uint(-k))
	}
}