// ********************************************************************** // InfoTypes.cpp // // Definitions of types and classes that are used to represent // the data in the computer. // // ********************************************************************** // CS251 - Computer Organization // Spring 1997 // Dickinson College // Grant Braught // // ********************************************************************** // #include #include #include #include #include "InfoTypes.h" #include "Logic.h" #include "BaseConv.h" #include "SysSpecs.h" BitData::BitData(int InitNumBits, Bool InitValue) { NumBits = InitNumBits; // Set the number of bits in this instance. (*this) = InitValue; // Use the overloaded assignment operator. } BitData::BitData(int InitNumBits, BitString InitValue) { if (InitNumBits <= 0) { cout << "\n** BitData - Number of bits must be > 0. **\n"; exit(-1); } if (InitNumBits > (8 * WORD_SIZE)) { cout << "\n** BitData - Number of bits must be <= the word size. **\n"; exit(-1); } NumBits = InitNumBits; // Set the number of bits in this instance. (*this) = InitValue; // Use the overloaded assignment operator. } Bool& BitData::operator [] (int theIndex) { if (theIndex >= NumBits) { cout << "\n** BitData - Bit index in [] > Number of bits. **\n"; exit(-1); } return (theBits[theIndex]); } void BitData::Base2Value(BitString theValue) { int theBit; for (theBit = 0; theBit < NumBits; theBit++) theValue[theBit] = B2C((*this)[(NumBits-1)-theBit]); theValue[NumBits] = '\0'; // Null terminate the string. } DecValue BitData::Base10Value(Bool Signed) { BitString theData; Base2Value(theData); return(ToBase10(theData,Signed)); } BitData& BitData::operator = (BitString NewValue) { if ((int)strlen(NewValue) > (8 * WORD_SIZE)) { cout << "\n** BitData - Number of bits must be <= "; cout << (8 * WORD_SIZE) << " **\n"; exit(-1); } int theBit; NumBits = (int)strlen(NewValue); for (theBit = 0; theBit < NumBits; theBit++) (*this)[theBit] = C2B(NewValue[(NumBits-1)-theBit]); return(*this); } BitData& BitData::operator = (BitData NewValue) { int theBit; NumBits = NewValue.NumBits; for (theBit = 0; theBit < NumBits; theBit++) (*this)[theBit] = NewValue[theBit]; return(*this); } BitData& BitData::operator = (Bool NewValue) { int theBit; for (theBit = 0; theBit < NumBits; theBit++) (*this)[theBit] = NewValue; return(*this); } BitData& BitData::operator <<= (BitData NewValue) { if (NumBits == 2*NewValue.NumBits) { int theBit; for (theBit = 0; theBit < (NumBits/2); theBit++) (*this)[theBit+(NumBits/2)] = NewValue[theBit]; } else if (2*NumBits == NewValue.NumBits) { int theBit; for (theBit = 0; theBit < (NumBits); theBit++) (*this)[theBit] = NewValue[theBit+NumBits]; } else { cout << "\n** BitData - "; cout << "Bit length mismatch in <<= operation. **\n"; exit(-1); } return(*this); } BitData& BitData::operator >>= (BitData NewValue) { if (NumBits == 2*NewValue.NumBits) { int theBit; for (theBit = 0; theBit < (NumBits/2); theBit++) (*this)[theBit] = NewValue[theBit]; } else if (2*NumBits == NewValue.NumBits) { int theBit; for (theBit = 0; theBit < (NumBits); theBit++) (*this)[theBit] = NewValue[theBit]; } else { cout << "\n** BitData - "; cout << "Bit length mismatch in >>= operation. **\n"; exit(-1); } return(*this); } ostream& operator << (ostream& theStream, BitData theBitData) { BitString theData; ToBase2(theBitData.Base10Value(), theData, theBitData.Length()); return (theStream << theData); }