Beispiel #1
0
 /// <summary>
 /// Clear out all the named fields
 /// </summary>
 public void ClearFields()
 {
     _fields.Clear();
     _bits = new BitString();
 }
Beispiel #2
0
        /// <summary>
        /// Concatenation of two BitStrings
        /// </summary>
        /// <param name="b1">A BitString that will be left concatenated with b2</param>
        /// <param name="b2">A BitString that will be right concatenated with b1</param>
        /// <returns></returns>
        public static BitString operator +(BitString b1, BitString b2) // input; BitString object to concatenate
        {
            // Declare new bit string to hold result
            Int32     newLength    = b1.Length + b2.Length;
            BitString newBitString = new BitString(newLength);

            // First, check if both operands are empty strings
            if (newLength == 0)
            {
                return(newBitString); // Return an empty string
            }

            // Now check if this object is empty
            if (b1.Length == 0)
            {
                return(new BitString(b2)); // No need to concatenate -- just return copy of right operand
            }

            // Now check if the right operand is empty
            if (b2.Length == 0)
            {
                return(new BitString(b1)); // No need to concatenate -- just return copy of left operand
            }

            // Otherwise start concatenation by copying this object into new bit string
            for (Int32 i = 0; i < b1.StorageSize; i++)
            {
                newBitString._buffer[i] = b1._buffer[i];
            }

            // Now we need to concatenate the right operand onto the new bit string

            // Calculate beginning left operand index
            Int64 leftPos = b1.StorageSize - 1;
            Int64 rightPos;

            if ((b1.Length & 31) != 0) // Shifting needed since this object is not multiple of 32 bits
            {
                // Calculate the number of places to shift
                Int32 shiftAmount = 32 - (b1.Length & 31); // Amount of bit shifting required

                // Calculate the Left Mask
                Int64 leftMask = -9223372036854775808; // Mask for tempLeft
                leftMask >>= (b1.Length & 31) - 1;

                // Calculate the Right Mask
                const UInt64 rightMask = 0x00000000FFFFFFFF; // Mask for tempRight

                // Now copy the right operand word by word to the end of the new bit string
                // along with the required shifting, masking and ORing operations
                for (rightPos = 0; rightPos < b2.StorageSize; rightPos++)
                {
                    // grab right most word from left bit string
                    UInt64 tempLeft = newBitString._buffer[leftPos]; // Temporary buffer variable
                    tempLeft <<= 32;

                    // mask out the lower trash bits
                    tempLeft &= (UInt64)leftMask;

                    // grab left most byte from right operand
                    UInt64 tempRight = b2._buffer[rightPos]; // Temporary buffer variable
                    tempRight  &= rightMask;
                    tempRight <<= shiftAmount;

                    // Now concatenate the words into temporary buffer by OR'ing them
                    tempLeft |= tempRight;

                    // Now copy this into the new bit string
                    Byte[] byteArray = BitConverter.GetBytes(tempLeft);

                    // Check for special end case of 1-word copy
                    if (rightPos == b2.StorageSize - 1 && (b2.Length & 31) != 0 &&
                        shiftAmount >= (b2.Length & 31))
                    {
                        UInt32 t1 = BitConverter.ToUInt32(byteArray, 4);
                        newBitString._buffer[leftPos] = t1; // Little Endian Machine
                    }
                    else // Normal 2 word copying
                    {
                        UInt32 t0 = BitConverter.ToUInt32(byteArray, 0);
                        UInt32 t1 = BitConverter.ToUInt32(byteArray, 4);

                        newBitString._buffer[leftPos]     = t1; // Little Endian Machine
                        newBitString._buffer[leftPos + 1] = t0; // Little Endian Machine
                    }

                    // Adjust indices
                    ++leftPos;
                } // end for
            }
            else  // No bit shifting needed since this object is multiple of 32 bits
            {
                // Pad the right operand to the new bit string
                for (rightPos = 0; rightPos < b2.StorageSize; rightPos++)
                {
                    newBitString._buffer[leftPos + 1] = b2._buffer[rightPos];
                    ++leftPos;
                }
            }

            // return the new bit string
            return(newBitString);
        }
Beispiel #3
0
 /// <summary>
 /// Initiates an empty BitField
 /// </summary>
 public BitField()
 {
     _bits   = new BitString();
     _fields = new OrderedDictionary();
 }