Size of an ASN.1 BIT STRING.

@param value
the BIT STRING value
@param nbits
the number of bits in the bit string
@return the size of the encoded data
430841
public static int sizeOfBitString(BitSet value, int nbits) {
        return DerUtils.sizeOf(ASN1_BIT_STRING_TAG_NUM, (int) Math.ceil(nbits / 8.0d) + 1); // +1 for padding
    }
689299
public static String encode(byte[] data, int size)
    {
        if (size % 4 != 0) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        int byteNbr = 0;
        long value = 0;
        while (byteNbr < size) {
            //  Accumulate value in base 256 (binary)
            int d = data[byteNbr++] & 0xff;
            value = value * 256 + d;
            if (byteNbr % 4 == 0) {
                //  Output value in base 85
                int divisor = 85 * 85 * 85 * 85;
                while (divisor != 0) {
                    int index = (int) (value / divisor % 85);
                    builder.append(encoder.charAt(index));
                    divisor /= 85;
                }
                value = 0;
            }
        }
        assert (builder.length() == size * 5 / 4);
        return builder.toString();
    }
195333
def integer_byte_size(n):
    ''''''
    quanta, mod = divmod(integer_bit_size(n), 8)
    if mod or n == 0:
        quanta += 1
    return quanta
468901
private static String unsignedToString(final long value) {
    if (value >= 0) {
      return Long.toString(value);
    } else {
      // Pull off the most-significant bit so that BigInteger doesn't think
      // the number is negative, then set it again using setBit().
      return BigInteger.valueOf(value & 0x7FFFFFFFFFFFFFFFL)
                       .setBit(63).toString();
    }
  }
1070052
public synchronized int setNextBit(boolean value) {
        increaseLength();
        index = (index + 1) % size;

        int previous = bitSet.set(index, value);
        int current = value ? 1 : 0;
        cardinality = cardinality - previous + current;
        return cardinality;
    }
1040808
func asn1BitLength(bitString []byte) int {
	bitLen := len(bitString) * 8

	for i := range bitString {
		b := bitString[len(bitString)-i-1]

		for bit := uint(0); bit < 8; bit++ {
			if (b>>bit)&1 == 1 {
				return bitLen
			}
			bitLen--
		}
	}

	return 0
}
689942
public void encodeNBitUnsignedInteger(int b, int n) throws IOException {
		if (b < 0 || n < 0) {
			throw new IllegalArgumentException(
					"Negative value as unsigned integer!");
		}
		assert (b >= 0);
		assert (n >= 0);

		if (n == 0) {
			// 0 bytes
		} else if (n < 9) {
			// 1 byte
			encode(b & 0xff);
		} else if (n < 17) {
			// 2 bytes
			encode(b & 0x00ff);
			encode((b & 0xff00) >> 8);
		} else if (n < 25) {
			// 3 bytes
			encode(b & 0x0000ff);
			encode((b & 0x00ff00) >> 8);
			encode((b & 0xff0000) >> 16);
		} else if (n < 33) {
			// 4 bytes
			encode(b & 0x000000ff);
			encode((b & 0x0000ff00) >> 8);
			encode((b & 0x00ff0000) >> 16);
			encode((b & 0xff000000) >> 24);
		} else {
			throw new RuntimeException(
					"Currently not more than 4 Bytes allowed for NBitUnsignedInteger!");
		}
	}
744054
def size_on_disk(self, start_pos=0):
        
        # All instructions are at least 1 byte (the opcode itself)
        size = 1
        fmts = opcode_table[self.opcode]['operands']

        if self.wide:
            size += 2
            # Special case for iinc which has a 2nd extended operand.
            if self.opcode == 0x84:
                size += 2
        elif fmts:
            # A simple opcode with simple operands.
            for fmt, _ in fmts:
                size += fmt.value.size
        elif self.opcode == 0xAB:
            # lookupswitch
            padding = 4 - (start_pos + 1) % 4
            padding = padding if padding != 4 else 0
            size += padding
            # default & npairs
            size += 8
            size += len(self.operands[0]) * 8
        elif self.opcode == 0xAA:
            # tableswitch
            raise NotImplementedError()

        return size
623181
public static function sizeInBytes($value)
    {
        $value = trim($value);
        $last = strtolower($value[strlen($value)-1]);

        switch($last) {
            case 'g':
                $value *= 1024;
            case 'm':
                $value *= 1024;
            case 'k':
                $value *= 1024;
        }

        return (int) $value;
    }
1341373
public static boolean isBitOn(int value, int bitNumber) {
    if (bitNumber <= 0 || bitNumber > 8) {
      throw new IllegalArgumentException("Only bits 1 through 8 are supported");
    }

    return ((value & MASKS[bitNumber - 1]) == MASKS[bitNumber - 1]);
  }
918615
@Override
    public int size () {
        int size = Smb2Constants.SMB2_HEADER_LENGTH + 36 + size8(2 * this.dialects.length, 4);
        if ( this.negotiateContexts != null ) {
            for ( NegotiateContextRequest ncr : this.negotiateContexts ) {
                size += 8 + size8(ncr.size());
            }
        }
        return size8(size);
    }
49070
public static int writeAscii(final BitOutput bitOutput, final int lengthSize, final String value)
            throws IOException {
        return writeBytes(bitOutput, lengthSize, true, 7, value.getBytes("ASCII"));
    }
151803
def get_net_size(mask):
    '''
    
    '''
    binary_str = ''
    for octet in mask.split('.'):
        binary_str += bin(int(octet))[2:].zfill(8)
    return len(binary_str.rstrip('0'))
520867
private static int bigDigitLength(BigInteger b) {
        /*
         * Same idea as the long version, but we need a better
         * approximation of log10(2). Using 646456993/2^31
         * is accurate up to max possible reported bitLength.
         */
        if (b.signum == 0)
            return 1;
        int r = (int)((((long)b.bitLength() + 1) * 646456993) >>> 31);
        return b.compareMagnitude(bigTenToThe(r)) < 0? r : r+1;
    }
685272
@Override
	public int readBit() {
		if (position == size) throw new EndOfBitStreamException();
		int count = (int) position & 7;
		if (count == 0) { // need new bits
			buffer = readByte();
			if (buffer == -1) throw new EndOfBitStreamException();
		}
		position++;
		// other endian
		// return (buffer >> count) & 1;
		return (buffer >> (7 - count)) & 1;
	}
726095
public int readBits(int n) throws IOException {
		assert (n > 0);
		int result;

		if (n <= capacity) {
			// buffer already holds all necessary bits
			result = (buffer >> (capacity -= n))
					& (0xff >> (BUFFER_CAPACITY - n));
		} else if (capacity == 0 && n == BUFFER_CAPACITY) {
			// possible to read direct byte, nothing else to do
			result = readDirectByte();
		} else {
			// get as many bits from buffer as possible
			result = buffer & (0xff >> (BUFFER_CAPACITY - capacity));
			n -= capacity;
			capacity = 0;

			// possibly read whole bytes
			while (n > 7) {
				if (capacity == 0) {
					readBuffer();
				}
				result = (result << BUFFER_CAPACITY) | buffer;
				n -= BUFFER_CAPACITY;
				capacity = 0;
			}

			// read the rest of the bits
			if (n > 0) {
				if (capacity == 0) {
					readBuffer();
				}
				result = (result << n)
						| (buffer >> (capacity = (BUFFER_CAPACITY - n)));
			}
		}

		return result;
	}
255
protected void unsigned8(final int size, int value) throws IOException {
        requireValidSizeUnsigned8(size);
        final int required = size - available;
        if (required > 0) {
            unsigned8(available, value >> required);
            unsigned8(required, value);
            return;
        }
        octet <<= size;
        octet |= (value & ((1 << size) - 1));
        available -= size;
        if (available == 0) {
            write(octet);
            count++;
            octet = 0x00;
            available = Byte.SIZE;
        }
    }