Esempio n. 1
0
        /// <summary>
        /// Reads a 32 bit integer value written using WriteRangedInteger()
        /// </summary>
        /// <param name="min">The minimum value used when writing the value</param>
        /// <param name="max">The maximum value used when writing the value</param>
        /// <returns>A signed integer value larger or equal to MIN and smaller or equal to MAX</returns>
        public int ReadRangedInteger(int min, int max)
        {
            uint range   = (uint)(max - min);
            int  numBits = BitUtility.BitsToHoldUInt(range);

            uint rvalue = ReadUInt32(numBits);

            return((int)(min + rvalue));
        }
Esempio n. 2
0
        /// <summary>
        /// Reads a 64 bit integer value written using WriteRangedInteger() (64 version)
        /// </summary>
        /// <param name="min">The minimum value used when writing the value</param>
        /// <param name="max">The maximum value used when writing the value</param>
        /// <returns>A signed integer value larger or equal to MIN and smaller or equal to MAX</returns>
        public long ReadRangedInteger(long min, long max)
        {
            ulong range   = (ulong)(max - min);
            int   numBits = BitUtility.BitsToHoldUInt64(range);

            ulong rvalue = ReadUInt64(numBits);

            return(min + (long)rvalue);
        }
Esempio n. 3
0
        /// <summary>
        /// Writes an integer with the least amount of bits need for the specified range
        /// Returns number of bits written
        /// </summary>
        public BitWriter WriteRangedInteger(long min, long max, long value, out int bitsWritten)
        {
            BitBufferException.Assert(value >= min && value <= max, "Value not within min/max range!");

            ulong range   = (ulong)(max - min);
            int   numBits = BitUtility.BitsToHoldUInt64(range);

            ulong rvalue = (ulong)(value - min);

            Write(rvalue, numBits);

            bitsWritten = numBits;

            return(this);
        }
Esempio n. 4
0
        /// <summary>
        /// Reads the specified number of bits into a preallocated array
        /// </summary>
        /// <param name="into">The destination array</param>
        /// <param name="offset">The offset where to start writing in the destination array</param>
        /// <param name="numberOfBits">The number of bits to read</param>
        public BitReader ReadBits(byte[] into, int offset, int numberOfBits)
        {
            ReadOverflowException.Assert(_lengthBits - _readPosition >= numberOfBits);
            BitBufferException.Assert(offset + BitUtility.BytesToHoldBits(numberOfBits) <= into.Length);

            int numberOfWholeBytes = numberOfBits / 8;
            int extraBits          = numberOfBits - (numberOfWholeBytes * 8);

            BitReaderWriter.ReadBytes(_data, numberOfWholeBytes, _readPosition, into, offset);
            _readPosition += (8 * numberOfWholeBytes);

            if (extraBits > 0)
            {
                into[offset + numberOfWholeBytes] = ReadByte(extraBits);
            }

            return(this);
        }