Program: 135823
Project: 135823
Buggy source:
/** 
 * Reads the specified number of bits from the passed byte array starting to read from the specified offset The bits read are stored in a byte array which size is dictated by the number of bits to be stored. The bits are stored in the byte array LSB aligned. <p/> Ex. Read 7 bits at offset 10 0         9 10     16 17 0101000010 | 0000101 | 1111001010010101011 will be returned as {0,0,0,0,0,1,0,1}
 * @param byte[] data
 * @param int    startOffset - offset to start fetching bits from data from
 * @param int    numBits - number of bits to be fetched from data
 * @return byte [] - LSB aligned bits
 * @throws BufferException when the startOffset and numBits parameters are not congruentwith the data buffer size
 */
public static byte[] getBits(byte[] data,int startOffset,int numBits) throws BufferException {
  int startByteOffset=0;
  int valfromcurr, valfromnext;
  int extranumBits=numBits % NetUtils.NumBitsInAByte;
  int extraOffsetBits=startOffset % NetUtils.NumBitsInAByte;
  int numBytes=(numBits % NetUtils.NumBitsInAByte != 0) ? 1 + numBits / NetUtils.NumBitsInAByte : numBits / NetUtils.NumBitsInAByte;
  byte[] shiftedBytes=new byte[numBytes];
  startByteOffset=startOffset / NetUtils.NumBitsInAByte;
  byte[] bytes=new byte[numBytes];
  if (numBits == 0) {
    return bytes;
  }
  checkExceptions(data,startOffset,numBits);
  if (extraOffsetBits == 0) {
    if (extranumBits == 0) {
      System.arraycopy(data,startByteOffset,bytes,0,numBytes);
      return bytes;
    }
 else {
      System.arraycopy(data,startByteOffset,bytes,0,numBytes - 1);
      bytes[numBytes - 1]=(byte)((int)data[startByteOffset + numBytes - 1] & getMSBMask(extranumBits));
    }
  }
 else {
    int i;
    for (i=0; i < numBits / NetUtils.NumBitsInAByte; i++) {
      valfromcurr=(data[startByteOffset + i]) & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits);
      valfromnext=(data[startByteOffset + i + 1]) & getMSBMask(extraOffsetBits);
      bytes[i]=(byte)(valfromcurr << (extraOffsetBits) | (valfromnext >> (NetUtils.NumBitsInAByte - extraOffsetBits)));
    }
    if (extranumBits != 0) {
      if (extranumBits < (NetUtils.NumBitsInAByte - extraOffsetBits)) {
        valfromnext=(byte)(data[startByteOffset + i] & ((getMSBMask(extranumBits)) >> extraOffsetBits));
        bytes[i]=(byte)(valfromnext << extraOffsetBits);
      }
 else       if (extranumBits == (NetUtils.NumBitsInAByte - extraOffsetBits)) {
        valfromcurr=(data[startByteOffset + i]) & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits);
        bytes[i]=(byte)(valfromcurr << extraOffsetBits);
      }
 else {
        valfromcurr=(data[startByteOffset + i]) & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits);
        valfromnext=(data[startByteOffset + i + 1]) & (getMSBMask(extranumBits - (NetUtils.NumBitsInAByte - extraOffsetBits)));
        bytes[i]=(byte)(valfromcurr << (extraOffsetBits) | (valfromnext >> (NetUtils.NumBitsInAByte - extraOffsetBits)));
      }
    }
  }
  shiftedBytes=shiftBitsToLSB(bytes,numBits);
  return shiftedBytes;
}

Fixed version (ground truth):
/** 
 * Reads the specified number of bits from the passed byte array starting to read from the specified offset The bits read are stored in a byte array which size is dictated by the number of bits to be stored. The bits are stored in the byte array LSB aligned. <p> Ex. Read 7 bits at offset 10 0         9 10     16 17 0101000010 | 0000101 | 1111001010010101011 will be returned as {0,0,0,0,0,1,0,1}
 * @param data
 * @param startOffset offset to start fetching bits from data from
 * @param numBits number of bits to be fetched from data
 * @return LSB aligned bits
 * @throws BufferException when the startOffset and numBits parameters are not congruentwith the data buffer size
 */
public static byte[] getBits(byte[] data,int startOffset,int numBits) throws BufferException {
  int startByteOffset=0;
  int valfromcurr, valfromnext;
  int extranumBits=numBits % NetUtils.NumBitsInAByte;
  int extraOffsetBits=startOffset % NetUtils.NumBitsInAByte;
  int numBytes=(numBits % NetUtils.NumBitsInAByte != 0) ? 1 + numBits / NetUtils.NumBitsInAByte : numBits / NetUtils.NumBitsInAByte;
  byte[] shiftedBytes=new byte[numBytes];
  startByteOffset=startOffset / NetUtils.NumBitsInAByte;
  byte[] bytes=new byte[numBytes];
  if (numBits == 0) {
    return bytes;
  }
  checkExceptions(data,startOffset,numBits);
  if (extraOffsetBits == 0) {
    if (extranumBits == 0) {
      System.arraycopy(data,startByteOffset,bytes,0,numBytes);
      return bytes;
    }
 else {
      System.arraycopy(data,startByteOffset,bytes,0,numBytes - 1);
      bytes[numBytes - 1]=(byte)((int)data[startByteOffset + numBytes - 1] & getMSBMask(extranumBits));
    }
  }
 else {
    int i;
    for (i=0; i < numBits / NetUtils.NumBitsInAByte; i++) {
      valfromcurr=(data[startByteOffset + i]) & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits);
      valfromnext=(data[startByteOffset + i + 1]) & getMSBMask(extraOffsetBits);
      bytes[i]=(byte)(valfromcurr << (extraOffsetBits) | (valfromnext >> (NetUtils.NumBitsInAByte - extraOffsetBits)));
    }
    if (extranumBits != 0) {
      if (extranumBits < (NetUtils.NumBitsInAByte - extraOffsetBits)) {
        valfromnext=(byte)(data[startByteOffset + i] & ((getMSBMask(extranumBits)) >> extraOffsetBits));
        bytes[i]=(byte)(valfromnext << extraOffsetBits);
      }
 else       if (extranumBits == (NetUtils.NumBitsInAByte - extraOffsetBits)) {
        valfromcurr=(data[startByteOffset + i]) & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits);
        bytes[i]=(byte)(valfromcurr << extraOffsetBits);
      }
 else {
        valfromcurr=(data[startByteOffset + i]) & getLSBMask(NetUtils.NumBitsInAByte - extraOffsetBits);
        valfromnext=(data[startByteOffset + i + 1]) & (getMSBMask(extranumBits - (NetUtils.NumBitsInAByte - extraOffsetBits)));
        bytes[i]=(byte)(valfromcurr << (extraOffsetBits) | (valfromnext >> (NetUtils.NumBitsInAByte - extraOffsetBits)));
      }
    }
  }
  shiftedBytes=shiftBitsToLSB(bytes,numBits);
  return shiftedBytes;
}