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();
int main(){
int menu;
do {
print_menu();
cout<< "Enter a number to select the menu:
"<<endl;
cin >>menu;
if(menu == 1){
endianness();
}
else if (menu == 2){
get_two_complement();
}
else if (menu == 0){
break;
}
else {
cout<<"wrong input!!!"<<endl;
}
print_menu();
cout<< "Enter a number to select the menu: ";
cin >> menu;
}while (menu !=0);
cout<< "Bye !!!" << endl;
return 0;
}
void endianness(){
cout << "My system uses the following endianness..." <<
endl;
unsigned int x = 5;
char *q = 0;
q = (char*)&x; //A char pointer is assigned to point to the
first (least-significant) byte of the integer value(i.e. x).
if (*q == 5) //If the first byte of the integer is having value
equal to 5, then the system is Little-Endian. Otherwise the system
is Big-Endian.
cout << "Little Endian" << endl;
else
cout<< "Big Endian" << endl;
}
void get_two_complement(){
int number;
cout << "Input the number to convert two's complement:
";
cin >> number;
cout << number << "'s two's complement is: ";
int a[32];int i;
//converting decimal into binary format
for( i =0; i<32; i++){
a[i]=0;
}
for(i=31; number >0; i--) {
a[i]=number%2;
number= number/2;
}
//traverse the array until we get first 1 from right.
for (i = 31 ; i >= 0 ; i--)
if (a[i] == 1)
break;
// If there exists no '1', concatenate 1 at the starting to get the
2's complement.
if (i == -1) {
cout<< "1 0000 0000 0000 0000 0000 0000 0000
0000"<<endl; // 2's complement of '0000...0000' is
'10000..0000'
return ;
}
// flip the values after the position of first '1'
for (int k = i-1 ; k >= 0; k--)
{
if (a[k] == 1)
a[k] = 0;
else
a[k] = 1;
}
int count=0;
//Displaying the 2's complement.
for (i = 0 ; i < 32 ; i++){
cout << a[i];
count++;
if(count==4){
count=0;
cout<<"\t";
}
}
cout<<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;
}