natix.CompactDS.BitStream32.WriteAt C# (CSharp) Method

WriteAt() public method

public WriteAt ( uint x, int numbits, long pos ) : void
x uint
numbits int
pos long
return void
        public void WriteAt(uint x, int numbits, long pos)
        {
            if (numbits < 1) {
                return;
            }
            if (pos + numbits > this.N) {
                throw new IndexOutOfRangeException ();
            }
            int offset_r = (int)(pos & 31);
            int offset_q = (int)(pos >> 5);
            // same cases than Read, cases:
            // 1) numbits is contained in q-th item
            // 2) numbits is in two items, q and q+1 th items
            // r is the remainder in the this.Offset bit-stream
            // q is the integer number containing the this.Offset bit
            uint mask = uint.MaxValue;
            if (numbits < 32) {
                mask = (1u << numbits) - 1;
            }
            x &= mask;
            if (numbits + offset_r <= 32) {
                // case 1
                var c = this.Buffer [offset_q];
                c &= ~(mask << offset_r);
                c |= x << offset_r;
                this.Buffer [offset_q] = c;
            } else {
                // case 2
                // cleaning the remainding bits of x if necessary
                var c = this.Buffer [offset_q];
                c &= ~(mask << offset_r);
                c |= x << offset_r;
                this.Buffer [offset_q] = c;
                // remainder
                var numbits_written = 32 - offset_r;
                x >>= numbits_written;
                mask >>= numbits_written;
                c = this.Buffer [offset_q + 1];
                c &= ~mask;
                c |= x;
                this.Buffer [offset_q + 1] = c;
            }
        }

Usage Example

Example #1
0
 public void ArraySet(BitStream32 Buffer, int pos, int val)
 {
     long p = pos;
     p *= this.NumBits;
     Buffer.WriteAt ((uint)val, this.NumBits, p);
 }