In: Computer Science
need c++ format
1.Check endianness on your system
. Either Big or Little Endian
2.Convert a decimal number to 2’s complement (32bits)
E.g., 5 -> 0000 0000 0000 0000 0000 0000 0000 1001
here is the start code
********************************************************* #include <iostream> using namespace std; void endianness(); void get_two_complement(); void print_menu(); int main(){ int menu; do { print_menu(); cout<< "Enter a number to select the menu: "; cin >>menu; if(menu == 1){ endianness(); } else if (menu == 2){ get_two_complement(); } else if (menu == 0){ break; } else { printf("wrong input!!!\n"); } print_menu(); cout<< "Enter a number to select the menu: "; cin >> menu; }while (menu !=0); cout<< "Bye !!!" << endl; exit(0); } void endianness(){ cout << "My system uses the following endianness..." << endl; //write the code here //print either little or big below //end of code } void get_two_complement(){ int number; cout << "Input the number to convert two's complement: "; cin >> number; cout << number << "'s two's complement is: "; //write the code here //end of code cout << endl << "[format]:" << endl; cout << number << "'s two's complement is: "; cout << "0000 0000 0000 0000 0000 0000 0000 0000"<< endl; } void print_menu(){ cout << "********************************" << endl; cout << "**** Homwwork 1: Menu ****" << endl; cout << "* 1. Check endianness *" << endl; cout << "* 2. Convert two's complement *" << endl; cout << "* 0. Exit the program *" << endl; cout << "********************************" << endl; }
#include <iostream>
using namespace std;
void endianness();
void get_two_complement();
void print_menu();
void convert(int, int[]);
int main(){
int menu;
do {
print_menu();
cout<< "Enter a number to select the menu: ";
cin >>menu;
if(menu == 1){
endianness();
}
else if (menu == 2){
get_two_complement();
}
else if (menu == 0){
break;
}
else {
printf("wrong input!!!\n");
}
}while (menu !=0);
cout<< "Bye !!!" << endl;
exit(0);
}
void endianness(){
cout << "My system uses the following endianness..." <<
endl;
//write the code here
//print either little or big below
unsigned int i = 1;
char *c = (char*)&i;
if (*c)
cout<<"Little Endian"<<endl;
else
cout<<"Big Endian"<<endl;
//end of code
}
void get_two_complement(){
int number, binary[32], carry;
cout << "Input the number to convert two's complement:
";
cin >> number;
cout << number << " 2's two's complement is: ";
//write the code here
//check the number positive or negative
if(number>=0)
{
convert(number, binary);
}
else
{
convert(-number, binary);
//at first 1's complement of the
number
for(int i=0; i<32; i++)
{
binary[i] =
(binary[i]==0)?1:0;
}
//add 1 to the 1's complement of
the number
carry = 1;
for(int i=31; i>=0; i--)
{
int t =
binary[i] + carry;
binary[i] = t %
2;
carry = t /
2;
if(carry==0)
break;
}
}
//display the number
for(int i=0; i<32; i++)
{
cout<<binary[i];
if((i+1)%4==0) cout<<"
";
}
cout<<endl;
//end of code
}
//function to convert from decimal to binary
void convert(int n, int binary[])
{
for(int i=31; i>=0; i--)
{
if(n==0)
{
binary[i] =
0;
continue;
}
binary[i] = n%2;
n/=2;
}
}
void print_menu(){
cout << "********************************" <<
endl;
cout << "**** Homwwork 1: Menu ****" << endl;
cout << "* 1. Check endianness *" << endl;
cout << "* 2. Convert two's complement *" <<
endl;
cout << "* 0. Exit the program *" << endl;
cout << "********************************" << endl;
}
Output:
********************************
**** Homwwork 1: Menu ****
* 1. Check endianness *
* 2. Convert two's complement *
* 0. Exit the program *
********************************
Enter a number to select the menu: 1
My system uses the following endianness...
Little endian
********************************
**** Homwwork 1: Menu ****
* 1. Check endianness *
* 2. Convert two's complement *
* 0. Exit the program *
********************************
Enter a number to select the menu: 2
Input the number to convert two's complement: -6
-6 2's two's complement is: 1111 1111 1111 1111 1111 1111 1111
1010
********************************
**** Homwwork 1: Menu ****
* 1. Check endianness *
* 2. Convert two's complement *
* 0. Exit the program *
********************************
Enter a number to select the menu: 2
Input the number to convert two's complement: 6
6 2's two's complement is: 0000 0000 0000 0000 0000 0000 0000
0110
********************************
**** Homwwork 1: Menu ****
* 1. Check endianness *
* 2. Convert two's complement *
* 0. Exit the program *
********************************
Enter a number to select the menu: 0
Bye !!!
--------------------------------
Process exited after 30.42 seconds with return value 0
Press any key to continue . . .