private void CopySourceBitsIntoLeftArray(byte[] sourceBits, int sourceArrayBitIndex,
                                                 int destinationBitIndex, int numBits)
        {
            var leftByteIndex = GetByteIndexFromBitIndex(destinationBitIndex);
            var startingBitIndexOfFirstSourceByte = sourceArrayBitIndex % 8;

            // Number of bit positions to shift the source bits to fill the unused positions in the last
            // destination byte:
            var numBitPositionsToShiftSourceBy = duoBitArrayUtilities
                                                 .GetNumBitsInUseInLastByte(destinationBitIndex) - startingBitIndexOfFirstSourceByte;

            var firstByteIndexInSource = GetByteIndexFromBitIndex(sourceArrayBitIndex);

            var lastSourceBitIndex    = sourceArrayBitIndex + numBits - 1;
            var lastByteIndexInSource = GetByteIndexFromBitIndex(lastSourceBitIndex);

            var sourceBitsCopy = GetOnlyRelevantSourceBytes(sourceBits, sourceArrayBitIndex, numBits,
                                                            firstByteIndexInSource, lastByteIndexInSource, lastSourceBitIndex, numBitPositionsToShiftSourceBy);

            for (var sourceByteIndex = 0;
                 sourceByteIndex < sourceBitsCopy.Length + (numBitPositionsToShiftSourceBy > 0 ? 1 : 0);
                 sourceByteIndex++, leftByteIndex++)
            {
                var sourceByteShiftedIntoPositionWithBorrowedBits = duoBitArrayUtilities
                                                                    .ShiftBitsInOneByte(sourceBitsCopy, sourceByteIndex, numBitPositionsToShiftSourceBy);

                bitArray[leftByteIndex] |= sourceByteShiftedIntoPositionWithBorrowedBits;
            }
        }
Beispiel #2
0
        private byte[] GetMaskForConsecutiveLeftBits(int numBits)
        {
            var numBytes      = duoBitArrayUtilities.GetMinNumBytesToStoreBits(numBits);
            var generatedMask = new byte[numBytes];

            generatedMask[numBytes - 1] =
                duoBitArrayUtilities.GetByteMsbMask(duoBitArrayUtilities.GetNumBitsInUseInLastByte(numBits));

            for (var i = 0; i < numBytes - 2; i++)
            {
                generatedMask[i] = 0xFF;
            }

            return(generatedMask);
        }