Flips all bits: 1 --> 0 and 0 --> 1
3796
public void flip() {
        int fromIndex=0, toIndex=size();
        if (fromIndex == toIndex)
            return;

        int startWordIndex = wordIndex(fromIndex);
        int endWordIndex   = wordIndex(toIndex);

        long firstWordMask = WORD_MASK << fromIndex;
        long lastWordMask  = WORD_MASK >>> -toIndex;
        if (startWordIndex == endWordIndex) {
            // Case 1: One word
            words[startWordIndex] ^= (firstWordMask & lastWordMask);
        } else {
            // Case 2: Multiple words
            // Handle first word
            words[startWordIndex] ^= firstWordMask;

            // Handle intermediate words, if any
            for (int i = startWordIndex+1; i < endWordIndex; i++)
                words[i] ^= WORD_MASK;

            // Handle last word
            words[endWordIndex] ^= lastWordMask;
        }
    }
956106
private void doWrite(int bits, int count) {

		int frontBits = ((int) position) & 31;
		int firstInt = (int) (position >> 5);

		int sumBits = count + frontBits;
		if (sumBits <= 32) {
			int i = ints[firstInt];
			int mask = frontMask(frontBits) | backMask(sumBits);
			i &= mask;
			i |= (bits << (32 - sumBits)) & ~mask;
			ints[firstInt] = i;
		} else {
			int i = ints[firstInt];
			int mask = frontMask(frontBits);
			i &= mask;
			int lostBits = sumBits - 32;
			i |= (bits >> lostBits) & ~mask;
			ints[firstInt] = i;

			i = ints[firstInt + 1];
			mask = backMask(lostBits);
			i &= mask;
			i |= (bits << (32 - lostBits)) & ~mask;
			ints[firstInt + 1] = i;
		}
	}
1112312
def changed_bit_pos(a, b):
    
    c = a ^ b
    n = 0
    while c > 0:
        if c & 1 == 1:
            return n
        c >>= 1
        n += 1
    return None
1234331
function decinputtobin(decinput){
  var len = decinput.length;
  if(len == 0){return filledarray(false, 1);}
  var bin = new Array();
  var digit = new Array(4);
  for (var i = 0; i < len; i++){
    switch (decinput[i]){
    case '0': digit[0] = false; digit[1] = false; digit[2] = false; digit[3] = false; break;
    case '1': digit[0] = false; digit[1] = false; digit[2] = false; digit[3] = true;  break;
    case '2': digit[0] = false; digit[1] = false; digit[2] = true;  digit[3] = false; break;
    case '3': digit[0] = false; digit[1] = false; digit[2] = true;  digit[3] = true;  break;
    case '4': digit[0] = false; digit[1] = true;  digit[2] = false; digit[3] = false; break;
    case '5': digit[0] = false; digit[1] = true;  digit[2] = false; digit[3] = true;  break;
    case '6': digit[0] = false; digit[1] = true;  digit[2] = true;  digit[3] = false; break;
    case '7': digit[0] = false; digit[1] = true;  digit[2] = true;  digit[3] = true;  break;
    case '8': digit[0] = true;  digit[1] = false; digit[2] = false; digit[3] = false; break;
    case '9': digit[0] = true;  digit[1] = false; digit[2] = false; digit[3] = true;  break;
    default: continue;
    }
    bin = binadd(binten(bin), digit);
  }
  return removezeros(bin);
}
806870
private int resetbuf(int bit_pos) {
		int pos = bit_pos >> 3;
		System.arraycopy(data, pos, data, 0, end - pos);
		end -= pos;
		return 0;
	}
436867
def _number_of_set_bits(x):
    '''
    
    '''
    # Taken from http://stackoverflow.com/a/4912729. Many thanks!
    x -= (x >> 1) & 0x55555555
    x = ((x >> 2) & 0x33333333) + (x & 0x33333333)
    x = ((x >> 4) + x) & 0x0f0f0f0f
    x += x >> 8
    x += x >> 16
    return x & 0x0000003f
948564
function binl(x, len) {
        var T, j, i, l,
          h0 = 0x67452301,
          h1 = 0xefcdab89,
          h2 = 0x98badcfe,
          h3 = 0x10325476,
          h4 = 0xc3d2e1f0,
          A1, B1, C1, D1, E1,
          A2, B2, C2, D2, E2;

        /* append padding */
        x[len >> 5] |= 0x80 << (len % 32);
        x[(((len + 64) >>> 9) << 4) + 14] = len;
        l = x.length;

        for (i = 0; i < l; i += 16) {
          A1 = A2 = h0;
          B1 = B2 = h1;
          C1 = C2 = h2;
          D1 = D2 = h3;
          E1 = E2 = h4;
          for (j = 0; j <= 79; j += 1) {
            T = safe_add(A1, rmd160_f(j, B1, C1, D1));
            T = safe_add(T, x[i + rmd160_r1[j]]);
            T = safe_add(T, rmd160_K1(j));
            T = safe_add(bit_rol(T, rmd160_s1[j]), E1);
            A1 = E1;
            E1 = D1;
            D1 = bit_rol(C1, 10);
            C1 = B1;
            B1 = T;
            T = safe_add(A2, rmd160_f(79 - j, B2, C2, D2));
            T = safe_add(T, x[i + rmd160_r2[j]]);
            T = safe_add(T, rmd160_K2(j));
            T = safe_add(bit_rol(T, rmd160_s2[j]), E2);
            A2 = E2;
            E2 = D2;
            D2 = bit_rol(C2, 10);
            C2 = B2;
            B2 = T;
          }

          T = safe_add(h1, safe_add(C1, D2));
          h1 = safe_add(h2, safe_add(D1, E2));
          h2 = safe_add(h3, safe_add(E1, A2));
          h3 = safe_add(h4, safe_add(A1, B2));
          h4 = safe_add(h0, safe_add(B1, C2));
          h0 = T;
        }
        return [h0, h1, h2, h3, h4];
      }
473425
def _count_righthand_zero_bits(number, bits):
    
    if number == 0:
        return bits
    for i in range(bits):
        if (number >> i) & 1:
            return i
    # All bits of interest were zero, even if there are more in the number
    return bits
1136697
public boolean flip(int bitIndex)
    {
        int wordIndex = wordIndex(bitIndex);
        words[wordIndex] ^= (1L << bitIndex);
        
        return ((words[wordIndex] & (1L << bitIndex)) != 0);
    }
1051587
def write_bit(self, b):
        
        if b:
            b = 1
        else:
            b = 0
        shift = self.bitcount % 8
        if shift == 0:
            self.bits.append(0)
        self.bits[-1] |= (b << shift)
        self.bitcount += 1
612094
function halve_(x) {
  var i;
  for (i=0; i<x.length-1; i++) {
    x[i]=mask & ((x[i+1]<<(bpe-1)) | (x[i]>>1));
  }
  x[i]=(x[i]>>1) | (x[i] & (radix>>1));  //most significant bit stays the same
}
1302128
function convert(data, output, frombits, tobits, pad, off) {
  const maxv = (1 << tobits) - 1;
  let acc = 0;
  let bits = 0;
  let j = 0;

  if (pad !== -1)
    output[j++] = pad;

  for (let i = off; i < data.length; i++) {
    const value = data[i];

    if ((value >> frombits) !== 0)
      throw new Error('Invalid bech32 bits.');

    acc = (acc << frombits) | value;
    bits += frombits;

    while (bits >= tobits) {
      bits -= tobits;
      output[j++] = (acc >>> bits) & maxv;
    }
  }

  if (pad !== -1) {
    if (bits > 0)
      output[j++] = (acc << (tobits - bits)) & maxv;
  } else {
    if (bits >= frombits || ((acc << (tobits - bits)) & maxv))
      throw new Error('Invalid bech32 bits.');
  }

  return output.slice(0, j);
}
817394
def _bit_mismatch(int1: int, int2: int) -> int:
    """"""
    for i in range(max(int1.bit_length(), int2.bit_length())):
        if (int1 >> i) & 1 != (int2 >> i) & 1:
            return i
    return -1
1113135
def int2baseTwo(x):
    """"""
    # repeating x >>= 1 and x & 1 will do the trick
    assert x >= 0
    bitInverse = []
    while x != 0:
        bitInverse.append(x & 1)
        x >>= 1
    return bitInverse
1205458
function zip_bi_reverse(
  code,	// the value to invert
  len) {	// its bit length
  var res = 0;
  do {
    res |= code & 1;
    code >>= 1;
    res <<= 1;
  } while (--len > 0);
  return res >> 1;
}
155155
def set_bit(bitmask, bit, is_on):
    
    bitshift = bit - 1
    if is_on:
        return bitmask | (1 << bitshift)
    return bitmask & (0xff & ~(1 << bitshift))
208016
def bits_clear_in_range( bits, range_start, range_end ):
    
    end = range_start
    while 1:
        start = bits.next_clear( end )
        if start >= range_end: break
        end = min( bits.next_set( start ), range_end )
        yield start, end