1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
using namespace std;
void convertIntToBinary(int iValue, bool baValues[], const int SIZE);
void printBinary(bool baValue[], const int SIZE);
void convertBinToInt (bool baValue[], int iValue);
int main()
{
const int SIZE = 16;
int iValue, iValueout;
bool baValues[SIZE] = [0];
cout << "Enter an integer from -32768 to 32767: ";
cin >> iValue;
while(iValue < -32768 || iValue > 32767)
{
cout << " ERROR, value must be between -32768 to 32767 \n\n";
cout << " Enter an integer from -32768 to 32767";
}
printBinary(baValues, SIZE);
//iValueout = convertIntToBinary(baValues, SIZE);
//cout << "The value after conversion back is " << iValueout << endl;
return 0;
}
void convertIntToBinary (int iValue, bool baValues[], const int SIZE)
{
int iTemp;
bool bNeg;
if (iValue< 0)
{
bNeg = true;
iValue= -iValue;
}
else
bNeg=false;
for(int i=0; i, SIZE; i++)
{
baValues[i] = iValue % 2;
iValue = iValue / 2;
}
for (int i=0, j=SIZE-1; i<(SIZE/2); i++, j--)
{
iTemp= baValues[j];
baValues[j] = baValues[i];
baValues[i] =iTemp;
}
}
void printBinary(bool baValues[], const int SIZE)
{
for (int i=0; i <SIZE; i++)
{
if ((i%4) ==0 )
cout << ' ';
cout << baValues[i];
}
}
|