// ********************************************************************** // BaseConv.cpp // // Definitions of functions for converting between base2 and base10. // // ********************************************************************** // CS251 - Computer Organization // Spring 1997 // Dickinson College // Grant Braught // // ********************************************************************** // // NOTE: These functions are written by the students before they // ever see the Bit, BitPair, Nibble, Byte and Word classes. // // These functions are then used by those functions for // conversions between base2 and base10. // #include #include #include #include "Logic.h" #include "SysSpecs.h" #include "BaseConv.h" DecValue ToBase10(BitString Base2Value, Bool Signed) // Convert the base2 representation of the number to base10. // You need to be sure to accouunt for negative numbers // represented in twos complement format if Signed=TRUE { char *theBit; DecValue theResult = 0; DecValue thePower; Bool Negative = FALSE; theBit = &Base2Value[0]; thePower = strlen(Base2Value) - 1; // If the number is negative then we take its twos complement // convert it to base10 then change its sign back to negative. if (Signed) if(*theBit == HIGHC) { Negative = TRUE; TwosComp(Base2Value); } // Convert each bit to its base10 equivalent. while(*theBit) theResult = theResult + (int)pow(2,thePower--)*(*theBit++ == HIGHC); // Change the sign on any negative numbers. Take the twos comp. // again since the TwosComp function changes the original var. due // to pass by reference. if (Signed) if (Negative) { theResult = -theResult; TwosComp(Base2Value); } return (theResult); } void ToBase2(DecValue Base10Value, BitString Base2Value, int NumBits, Bool Signed) // Convert the base10 representation of the number passed in the // parameter Base10Value to base2. Put the result in Base2Value // using the indicated number of bits. // Again be sure to represent any negative numbers using twos // complement format if Signed=TRUE { char *theBit; DecValue thePower = NumBits - 1; div_t DivResult; Bool Negative = FALSE; theBit = &Base2Value[0]; // If the value to be converted is negative then flip its sign // convert it to base2 then take its two's complement. if(Signed) if (Base10Value < 0) { Negative = TRUE; Base10Value = -Base10Value; } while(thePower >= 0) { DivResult = div(Base10Value,(int)(pow(2,thePower--))); // Set this bit of the bit string appropriately. if (DivResult.quot) *theBit++ = HIGHC; else *theBit++ = LOWC; // Now operate on the rest of the value. Base10Value = DivResult.rem; } *theBit = '\0'; // Null terminate the string. // Make the base2 number negative. if(Signed) if (Negative) TwosComp(Base2Value); } void TwosComp(BitString Base2Value) // Take the twos complement of the base2 number that is passed in the // parameter Base2Value. The Twos complement of the number can simply // over write the current value. // This is simply changing the sign of the number. // // NOTE: Taking the twos complement means flipping all of the bits // then adding one. These two things can be done in sequence // or simultaneously. The following solution does them // simultaneously. It does so by implementing the following // truth table: // // INPUTS: OUTPUTS: // theBit CarryIn !theBit theBit Carry // --------------------------------------------------------- // 0 0 | 1 | 1 0 // 0 1 | 1 | 0 1 // 1 0 | 0 | 0 0 // 1 1 | 0 | 1 0 // --------------------------------------------------------- { char *theBit; char CarryIn = '1'; theBit = &Base2Value[0]; while(*theBit) // Find the end of the string. theBit ++; // Which is also the LSB of the bit string. theBit --; // Implement the truth table above for each of the bits in // the bit string. while(theBit >= &Base2Value[0]) { // Row #1 if ((*theBit == LOWC) && (CarryIn == LOWC)) *theBit = HIGHC; else // Row #3 if((*theBit == HIGHC) && (CarryIn == LOWC)) *theBit = LOWC; else // Row #4 if ((*theBit == HIGHC) && (CarryIn == HIGHC)) { *theBit = HIGHC; CarryIn = LOWC; } // NOTE: Row #2 doesn't require any action. theBit--; // Move to the next bit. } }