Computer Science: Assembly Language for x86 Processors C++ Twos Complement with
ID: 3883048 • Letter: C
Question
Computer Science: Assembly Language for x86 Processors
C++
Twos Complement with Hexadecimal
To implement TwosComplement(), only functions allowed to use are strlen(), strcpy(), and toupper() if needed. Please don't use printf() or scanf(). An implementation of TwosComplement() is suggested here:
1. Receive a 4-char C-string with 4 Hex digits
2. Loop through each char from right to left
- Get a Hex char digit
- Make it upper case if necessary
- Convert char digit to a number value
- Reverse every bit in Hex digit
- Plus one to the lowest digit (may cause a carry to propagate)
- Convert each number value back to char digit
3. A single loop should be enough without nested one
And please show me sample output.
Twos_Complement_with_Hexadecimal (Chapter 1) Write a C+program converting a 4-digit Hexadecimal integer to its Two's Complement. You can create functions declared like this : // Function: TwosComplement / Description: This function converts a Hexadecimal to its Two's Complement / Parameter: S [IN]- a C-string with a 4-digit Hexadecimal received / Return: void TwosComplement (char* s, char s2) s2 [out] - a C-string with a 4-digit of s two's complement None Then you can call it repeatedly in main() to verify that the 2's Complement is reversible with TwosComplement exe Please Enter 4-digit Hexadecimal integer (e.g., A1B2): A0Bc Two's Complement of Hex A0Bc is 5F44 Try again? (y/n) y Please Enter 4-digit Hexadecimal integer (e.g., A1B2) 5f44 Two's Complement of Hex 5f44 is A0BC Try again? (y/n) y Please Enter 4-digit Hexadecimal integer (e.g., A1B2) AoBC Two's Complement of Hex AoBC is Error Try again? (y/n) y Please Enter 4-digit Hexadecimal integer (e.g., A1B2) 0000 Two s Complement of Hex 0000 is 0000 Try again? (y/n) y Please Enter 4-digit Hexadecimal integer (e.g., A1B2): 0001 Two 's Complement of Hex 0001 is FFFF Try again? (y/n) n Press any key to continue .. .Explanation / Answer
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int convertToNum(char ch)
{
switch (ch)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'A':
return 10;
case 'B':
return 11;
case 'C':
return 12;
case 'D':
return 13;
case 'E':
return 14;
case 'F':
return 15;
default:
break;
}
}
char convertToChar(int num)
{
switch (num)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
case 10:
return 'A';
case 11:
return 'B';
case 12:
return 'C';
case 13:
return 'D';
case 14:
return 'E';
case 15:
return 'F';
default:
break;
}
}
vector<int> ConvertToBinary(int num)
{
vector<int> result;
while (num)
{
if (num % 2)
result.push_back(1);
else
result.push_back(0);
num = num / 2;
}
reverse(result.begin(), result.end());
return result;
}
void TwosComplement(char *s, char* s2)
{
for (int i = 3; i >= 0; i--)
{
char hexdigit = s[i];
s2[i] = toupper(hexdigit);
int num = convertToNum(s2[i]);
vector<int> binaryValue = ConvertToBinary(num);
int binary1 = 0, binary2 = 1;;
for (int j = 0; j < binaryValue.size(); j++)
{
binary1 += binaryValue[j] * pow(10, 3 - j);
}
char sum[20];
int k = 0;
int remainder = 0;
while (binary1 != 0 || binary2 != 0)
{
sum[k++] = (binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder = (binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum[k++] = remainder;
--k;
cout <<"Sum of two binary numbers: ";
while (k >= 0)
cout << sum[k--];
cout << endl;
for (int id = 0; id < 4; id++)
num += sum[i] * pow(2, 3 - id);
char ConvertChar = convertToChar(num);
//add to s2
s2[i] = ConvertChar;
}
}
int main()
{
char *s = new char[4];
char *s2 = new char[4];
while (true)
{
cout << "Please Enter 4-digit Hexadecimal Integer (e.g. A1B2):";
cin >> s;
TwosComplement(s, s2);
cout << "Twos's complement of Hex " << s << " is " << s2;
char ch;
cout << "Try Again? (y/n)";
cin >> ch;
if (ch != 'y' || ch != 'Y')
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.