Пример #1
0
        /// <summary>
        /// Takes source bits, starting at the given MSB-0 bit index, and merges it into the given destination byte,
        /// at the given destination MSB-0 bit index.
        /// </summary>
        /// <param name="sourceByte">The source bits that will be merged with the destination bits.</param>
        /// <param name="sourceMsbIndex">The MSB-0 index into the <paramref name="sourceByte"/>, which is the
        /// starting index of the bits to be merged; MSB-0 index 7 (which is the LSB) is the ending index.</param>
        /// <param name="destinationByte">The other group of bits that will be merged with the source bits.</param>
        /// <param name="destinationLsbIndex">The MSB-0 index into the <paramref name="destinationByte"/>, which
        /// is the <i>ending</i> index of the bits to be merged; MSB-0 index 0 (which is the MSB) is the starting
        /// index.</param>
        /// <param name="maskUtilities">An injected object which implements the <see cref="IBitMaskUtilities"/>
        /// interface.</param>
        /// <returns>The two bytes, blended together, at their given offsets.</returns>
        public byte AlignAndMergeBytes(byte sourceByte, int sourceMsbIndex, byte destinationByte,
            int destinationLsbIndex, IBitMaskUtilities maskUtilities)
        {
            const byte byteMaskToTruncateLeftBits = 0xFF;
            var sourceByteMask = (byte)~maskUtilities.GetByteMsbMask(sourceMsbIndex);
            var destinationByteMask = maskUtilities.GetByteMsbMask(destinationLsbIndex + 1);

            sourceByte &= sourceByteMask;

            var deltaShiftAmount = destinationLsbIndex - sourceMsbIndex + 1;
            if (deltaShiftAmount < 0)
            {
                sourceByte = (byte)((sourceByte << -deltaShiftAmount) & byteMaskToTruncateLeftBits);
            }
            else
            {
                sourceByte = (byte)(sourceByte >> deltaShiftAmount);
            }

            return (byte)(destinationByte & destinationByteMask | sourceByte);
        }
        [TestCase(8, 0b11111111)] // 0xFF
        public void ShouldGetByteMsbMask(int numBitsToMask, byte expectedMask)
        {
            var actualMask = bmu.GetByteMsbMask(numBitsToMask);

            Assert.That(actualMask, Is.EqualTo(expectedMask));
        }
 public byte GetByteMsbMask(int numBitsToMask) => bitMaskUtilities.GetByteMsbMask(numBitsToMask);