//************************************************************** v. 10/14/04 // Purpose: test conversion of sign-magnitude to twos complement // Code serves only to illustrate one approach to converting // sign-magnitude to twos-complement representations. // Name: cmo // Creation date: 10/13/04 // Reuse Instructions: // The sm2tc function might be reused even though it's a one liner. //************************************************************************** using namespace std; #include // for cin, cout short sm2tc( unsigned short x ) { if ( x < 32768 ) return( x ); else return( -(x-32768) ); } int main() { unsigned short x; // assumed to be a 16 bit sign-magnitude representation short y; // a 16 twos-complement representation of the same value cin >> x; while ( cin ) { y = sm2tc( x ); cout << y << endl; cin >> x; } }//end of main