/// <summary> /// Reads next <paramref name="bitsCount"/> bits from byte array as <see cref="Int32"/> /// </summary> /// <param name="bitsCount"></param> /// <returns>The 32-bit signed integer value of the specified amount of bits</returns> /// <exception cref="ArgumentException">Thrown if <paramref name="bitsCount"/> is not in inclusive range [1, 32]</exception> /// <exception cref="InvalidOperationException">Thrown if requested <paramref name="bitsCount"/> amount of bits couldn't be read</exception> public int ReadInt32(int bitsCount = BitsCount.BitsPerDWord) { BitsCount.EnsureBitsCount(bitsCount, BitsCount.BitsPerDWord); EnsureRequestedBitsCount(mBitsRead, bitsCount, mBitArray.Count); int result = (int)GetSigned(bitsCount); if (bitsCount == BitsCount.BitsPerDWord) { mBitsRead += bitsCount; return(result); } int mask = (1 << bitsCount) - 1; int signMask = 1 << (bitsCount - 1); if ((result & signMask) != 0) { result |= ~mask; } mBitsRead += bitsCount; return(result); }
/// <summary> /// Reads next <paramref name="bitsCount"/> bits from byte array as <see cref="Int16"/> /// </summary> /// <param name="bitsCount"></param> /// <returns>The 16-bit signed integer value of the specified amount of bits</returns> /// <exception cref="ArgumentException">Thrown if <paramref name="bitsCount"/> is not in inclusive range [1, 16]</exception> /// <exception cref="InvalidOperationException">Thrown if requested <paramref name="bitsCount"/> amount of bits couldn't be read</exception> public short ReadInt16(int bitsCount = BitsCount.BitsPerWord) { BitsCount.EnsureBitsCount(bitsCount, BitsCount.BitsPerWord); EnsureRequestedBitsCount(mBitsRead, bitsCount, mBitArray.Count); short result = (short)GetSigned(bitsCount); if (bitsCount == BitsCount.BitsPerWord) { mBitsRead += bitsCount; return(result); } var mask = (short)((1 << bitsCount) - 1); var signMask = (short)(1 << (bitsCount - 1)); if ((result & signMask) != 0) { result |= (short)~mask; } mBitsRead += bitsCount; return(result); }
public sbyte ReadInt8(int bitsCount = BitsCount.BitsPerByte) { BitsCount.EnsureBitsCount(bitsCount, BitsCount.BitsPerByte); EnsureRequestedBitsCount(mBitsRead, bitsCount, mBitArray.Count); sbyte result = (sbyte)GetSigned(bitsCount); if (bitsCount == BitsCount.BitsPerByte) { mBitsRead += bitsCount; return(result); } var mask = (sbyte)((1 << bitsCount) - 1); var signMask = (sbyte)(1 << (bitsCount - 1)); if ((result & signMask) != 0) { result |= (sbyte)~mask; } mBitsRead += bitsCount; return(result); }
public ushort ReadUInt16(int bitsCount = BitsCount.BitsPerWord) { BitsCount.EnsureBitsCount(bitsCount, BitsCount.BitsPerWord); EnsureRequestedBitsCount(mBitsRead, bitsCount, mBitArray.Count); var result = (ushort)GetUnsigned(bitsCount); mBitsRead += bitsCount; return(result); }
/// <summary> /// Reads next <paramref name="bitsCount"/> bits from byte array as <see cref="Byte"/> /// </summary> /// <param name="bitsCount"></param> /// <returns>The 8-bit unsigned integer value of the specified amount of bits</returns> /// <exception cref="ArgumentException">Thrown if <paramref name="bitsCount"/> is not in inclusive range [1, 8]</exception> /// <exception cref="InvalidOperationException">Thrown if requested <paramref name="bitsCount"/> amount of bits couldn't be read</exception> public byte ReadUInt8(int bitsCount = BitsCount.BitsPerByte) { BitsCount.EnsureBitsCount(bitsCount, BitsCount.BitsPerByte); EnsureRequestedBitsCount(mBitsRead, bitsCount, mBitArray.Count); var result = (byte)GetUnsigned(bitsCount); mBitsRead += bitsCount; return(result); }
public ulong ReadUInt64(int bitsCount = BitsCount.BitsPerQWord) { BitsCount.EnsureBitsCount(bitsCount, BitsCount.BitsPerQWord); EnsureRequestedBitsCount(mBitsRead, bitsCount, mBitArray.Count); ulong result = GetUnsigned(bitsCount); mBitsRead += bitsCount; return(result); }
/// <summary> /// Reads next <paramref name="bitsCount"/> bits from byte array as <see cref="Int64"/> /// </summary> /// <param name="bitsCount"></param> /// <returns>The 64-bit signed integer value of the specified amount of bits</returns> /// <exception cref="ArgumentException">Thrown if <paramref name="bitsCount"/> is not in inclusive range [1, 64]</exception> /// <exception cref="InvalidOperationException">Thrown if requested <paramref name="bitsCount"/> amount of bits couldn't be read</exception> public long ReadInt64(int bitsCount = BitsCount.BitsPerQWord) { BitsCount.EnsureBitsCount(bitsCount, BitsCount.BitsPerQWord); EnsureRequestedBitsCount(mBitsRead, bitsCount, mBitArray.Count); long result = GetSigned(bitsCount); if (bitsCount == BitsCount.BitsPerQWord) { mBitsRead += bitsCount; return(result); } long mask; if (bitsCount < 63) { mask = (1L << bitsCount) - 1; } else if (bitsCount == 63) { mask = long.MaxValue; } else { mask = -1; } long signMask = 1L << (bitsCount - 1); if ((result & signMask) != 0) { result |= ~mask; } mBitsRead += bitsCount; return(result); }