/// <summary> /// Constructor for injecting utilities. /// </summary> protected DuoBitArrayUtilities(IBitArrayUtilities bitArrayUtilities, IBitMaskUtilities bitMaskUtilities, IBitCounter bitCounter) { this.bitArrayUtilities = bitArrayUtilities; this.bitMaskUtilities = bitMaskUtilities; this.bitCounter = bitCounter; }
/// <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); }
public byte AlignAndMergeBytes(byte sourceByte, int sourceMsbIndex, byte destinationByte, int destinationLsbIndex, IBitMaskUtilities maskUtilities) => bitArrayUtilities.AlignAndMergeBytes(sourceByte, sourceMsbIndex, destinationByte, destinationLsbIndex, maskUtilities);
public IBitMaskUtilities AddBitMaskUtilities(IBitMaskUtilities bitMaskUtilities) { this.bitMaskUtilities = bitMaskUtilities; return(this); }
public static IDuoBitArrayUtilities Create(IBitArrayUtilities bitArrayUtilities, IBitMaskUtilities bitMaskUtilities, IBitCounter bitCounter) => new DuoBitArrayUtilities(bitArrayUtilities, bitMaskUtilities, bitCounter);