/// <summary> /// Reads a signed integer stored in 1 to 32 bits, written using Write(Int32, Int32) /// </summary> public Int32 ReadInt32(int numberOfBits) { Debug.CheckArgument(numberOfBits > 0 && numberOfBits <= 32, "ReadInt32(bits) can only read between 1 and 32 bits"); Debug.CheckArgument(m_bitLength - m_readPosition >= numberOfBits, c_readOverflowError); uint retval = BitWriter.ReadUInt32(m_data, numberOfBits, m_readPosition); m_readPosition += numberOfBits; if (numberOfBits == 32) { return((int)retval); } int signBit = 1 << (numberOfBits - 1); if ((retval & signBit) == 0) { return((int)retval); // positive } // negative unchecked { uint mask = ((uint)-1) >> (33 - numberOfBits); uint tmp = (retval & mask) + 1; return(-((int)tmp)); } }
/// <summary> /// Reads an 32 bit unsigned integer written using Write(UInt32) /// </summary> public UInt32 ReadUInt32() { Debug.CheckArgument(m_bitLength - m_readPosition >= 32, c_readOverflowError); uint retval = BitWriter.ReadUInt32(m_data, 32, m_readPosition); m_readPosition += 32; return(retval); }
/// <summary> /// Reads an unsigned integer stored in 1 to 32 bits, written using Write(UInt32, Int32) /// </summary> public UInt32 ReadUInt32(int numberOfBits) { Debug.CheckArgument(numberOfBits > 0 && numberOfBits <= 32, "ReadUInt32(bits) can only read between 1 and 32 bits"); //Debug.CheckArgument(m_bitLength - m_readBitPtr >= numberOfBits, "tried to read past buffer size"); UInt32 retval = BitWriter.ReadUInt32(m_data, numberOfBits, m_readPosition); m_readPosition += numberOfBits; return(retval); }
/// <summary> /// Reads an 32 bit unsigned integer written using Write(UInt32) and returns true for success /// </summary> public bool ReadUInt32(out UInt32 result) { if (m_bitLength - m_readPosition < 32) { result = 0; return(false); } result = BitWriter.ReadUInt32(m_data, 32, m_readPosition); m_readPosition += 32; return(true); }
/// <summary> /// Reads a 64 bit unsigned integer written using Write(UInt64) /// </summary> public UInt64 ReadUInt64() { Debug.CheckArgument(m_bitLength - m_readPosition >= 64, c_readOverflowError); ulong low = BitWriter.ReadUInt32(m_data, 32, m_readPosition); m_readPosition += 32; ulong high = BitWriter.ReadUInt32(m_data, 32, m_readPosition); ulong retval = low + (high << 32); m_readPosition += 32; return(retval); }
/// <summary> /// Reads an unsigned integer stored in 1 to 64 bits, written using Write(UInt64, Int32) /// </summary> public UInt64 ReadUInt64(int numberOfBits) { Debug.CheckArgument(numberOfBits > 0 && numberOfBits <= 64, "ReadUInt64(bits) can only read between 1 and 64 bits"); Debug.CheckArgument(m_bitLength - m_readPosition >= numberOfBits, c_readOverflowError); ulong retval; if (numberOfBits <= 32) { retval = (ulong)BitWriter.ReadUInt32(m_data, numberOfBits, m_readPosition); } else { retval = BitWriter.ReadUInt32(m_data, 32, m_readPosition); retval |= BitWriter.ReadUInt32(m_data, numberOfBits - 32, m_readPosition) << 32; } m_readPosition += numberOfBits; return(retval); }