pub fn extract_bits(
    buffer: &[u8],
    buffer_bit_length: usize,
    start_bit: usize,
    mut bit_length: usize,
) -> Vec<u8> {
    if bit_length < 1 || bit_length > (buffer_bit_length - start_bit) {
        bit_length = buffer_bit_length - start_bit;
    }

    let start_byte = start_bit / BYTE;
    let byte_length = byte_length(bit_length as isize) as usize;
    let shift = start_bit % BYTE;
    let mask: u8 = (BYTE - shift).checked_shl(0xFF).unwrap_or(u8::MAX as usize) as u8;

    let mut new_byte_array: Vec<u8> = vec![0; byte_length];
    if shift == 0 {
        /* Faster loop if no shift needed */
        for byte_index in 0..byte_length {
            new_byte_array[byte_index] = buffer[start_byte + byte_index];
        }
    } else {
        /* Loop through bytes to shift */
        for byte_index in 0..byte_length {
            new_byte_array[byte_index] = (0xFF & buffer[start_byte + byte_index]) << shift;
        }

        let end_byte = if byte_length < (buffer.len() - start_byte) {
            byte_length
        } else {
            byte_length - 1
        };

        for byte_index in 0..end_byte {
            new_byte_array[byte_index] |=
                (0xFF & buffer[start_byte + byte_index + 1] & mask) >> (BYTE - shift);
        }
    }

    crop_bits(new_byte_array, bit_length as isize)
}
/**
 * Returns a byte array with a subset of the bits in the buffer
 * @param startBit the first bit of the subset to extract
 * @param bitLength the length of the subset, i.e. the number of bits to extract
 * @return a byte array with the extracted bits
 */
Returns a byte array with a subset of the bits in the buffer
@param startBit the first bit of the subset to extract
@param bitLength the length of the subset, i.e. the number of bits to extract
@return a byte array with the extracted bits
Returns a byte array with a subset of the bits in the buffer @param startBit the first bit of the subset to extract @param bitLength the length of the subset i . e . the number of bits to extract @return a byte array with the extracted bits
pub fn extract_bits(
    buffer: &[u8],
    buffer_bit_length: usize,
    start_bit: usize,
    mut bit_length: usize,
) -> Vec<u8> {
    if bit_length < 1 || bit_length > (buffer_bit_length - start_bit) {
        bit_length = buffer_bit_length - start_bit;
    }
    let start_byte = start_bit / BYTE;
    let byte_length = byte_length(bit_length as isize) as usize;
    let shift = start_bit % BYTE;
    let mask: u8 = (BYTE - shift).checked_shl(0xFF).unwrap_or(u8::MAX as usize) as u8;
    let mut new_byte_array: Vec<u8> = vec![0; byte_length];
    if shift == 0 {
        /* Faster loop if no shift needed */
        for byte_index in 0..byte_length {
            new_byte_array[byte_index] = buffer[start_byte + byte_index];
        }
    } else {
        /* Loop through bytes to shift */
        for byte_index in 0..byte_length {
            new_byte_array[byte_index] = (0xFF & buffer[start_byte + byte_index]) << shift;
        }
        let end_byte = if byte_length < (buffer.len() - start_byte) {
            byte_length
        } else {
            byte_length - 1
        };
        for byte_index in 0..end_byte {
            new_byte_array[byte_index] |=
                (0xFF & buffer[start_byte + byte_index + 1] & mask) >> (BYTE - shift);
        }
    }
    crop_bits(new_byte_array, bit_length as isize)
}
pub fn extract_bits ( buffer : & [ u8 ] , buffer_bit_length : usize , start_bit : usize , mut bit_length : usize , ) -> Vec < u8 > { if bit_length < 1 || bit_length > ( buffer_bit_length - start_bit ) { bit_length = buffer_bit_length - start_bit ; } let start_byte = start_bit / BYTE ; let byte_length = byte_length ( bit_length as isize ) as usize ; let shift = start_bit % BYTE ; let mask : u8 = ( BYTE - shift ) . checked_shl ( 0xFF ) . unwrap_or ( u8 :: MAX as usize ) as u8 ; let mut new_byte_array : Vec < u8 > = vec ! [ 0 byte_length ] ; if shift == 0 { /* Faster loop if no shift needed */ for byte_index in 0 .. byte_length { new_byte_array [ byte_index ] = buffer [ start_byte + byte_index ] ; } } else { /* Loop through bytes to shift */ for byte_index in 0 .. byte_length { new_byte_array [ byte_index ] = ( 0xFF & buffer [ start_byte + byte_index ] ) << shift ; } let end_byte = if byte_length < ( buffer . len ( ) - start_byte ) { byte_length } else { byte_length - 1 } ; for byte_index in 0 .. end_byte { new_byte_array [ byte_index ] |= ( 0xFF & buffer [ start_byte + byte_index + 1 ] & mask ) >> ( BYTE - shift ) ; } } crop_bits ( new_byte_array , bit_length as isize ) }
Returns a byte array with a subset of the bits in the buffer
 @param startBit the first bit of the subset to extract
 @param bitLength the length of the subset, i.e.
Returns a byte array with a subset of the bits in the buffer @param startBit the first bit of the subset to extract @param bitLength the length of the subset i . e .
buffer
&[u8]
buffer_bit_length
usize
start_bit
usize
bit_length
usize
buffer
&[u8]
buffer_bit_length
usize
start_bit
usize
bit_length
usize