アセンブリ言語クラスを受講していて、符号付き整数を入力として受け入れ、対応する2の補数を出力するアプリケーションを作成するように求められました。私はインターネットのいたるところで役立つコードを見つけようとしていますが、見つけることができるのは正確なバイナリに変換するコードだけです(先行ゼロで必要な16ビット形式ではありません)。これは私がこれまでに持っているコードです:
#include<iostream>
#include<string>
using namespace std;
string binaryArray[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void toBinary(int);
void convertNegative();
int main()
{
cout << "This app converts an integer from -32768 to 32767 into 16-bit 2's complement binary format" << endl;
cout << "Please input an integer in the proper range: ";
int num;
cin >> num;
if (num < -32768 || num > 32767)
cout << "You have entered an unacceptable number, sorry." << endl;
if (num < 0)
{
toBinary(num);
convertNegative();
}
else
toBinary(num);
cout << endl;
system("pause");
return 0;
}
私のtoBinary関数は、インターネットで10進数から2進数に変換できる関数でしたが、コンソールに出力している場合にのみ機能し、負の数では機能しないため、2の補数を取ることはできません。何か案は?