Question

In: Computer Science

need c++ format 1.Check endianness on your system . Either Big or Little Endian 2.Convert a...

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; 

}

Solutions

Expert Solution

#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 . . .


Related Solutions

need c++ format 1.Check endianness on your system . Either Big or Little Endian 2.Convert a...
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(); }...
Write a MIPS program to check if computer is a big-endian or little-endian system. This must...
Write a MIPS program to check if computer is a big-endian or little-endian system. This must be done in MIPS assembly language.
what is the different between little endian and big endian on mips ?
what is the different between little endian and big endian on mips ?
Problem: On an ARM processor using big endian format, given the following memory map and [R1]...
Problem: On an ARM processor using big endian format, given the following memory map and [R1] = 0xA10E0C2D, [R2] = 0x00000060, [R3] = 0x00000002, [R4] = 0x0000000C, predict [R1] and [R2] and draw the updated memory map after an ARM data transfer instruction is executed in EACH case. (hint: (1) in this map, each memory location is a word long and occupies 4 bytes; also you only need to draw the section of the memory including the changed word and...
Big Co. owns 80% of the stock of Little Co. On 1/1/23 Little issues $100,000 of...
Big Co. owns 80% of the stock of Little Co. On 1/1/23 Little issues $100,000 of 10%, 10 year bonds directly to Big for $103,000.  Big and Little both use straight-line amortization. Prepare elimination entries RELATING TO THIS INTERCOMPANY TRANSACTION for 2023 and 2024
please check my answer if its right or need better answer. I need example of big...
please check my answer if its right or need better answer. I need example of big five personality traits. so please check my answer • Openness: the founder of x country he is open minded and every one can meet him. also he likes to travel new places and try new food • consciousnesses: like local astonaut called xxxx he is so immagination guy and organized because of his awarness and imagination he went to space to discover more and...
Big Co. owns 60% of the stock of Little Co.   On 1/1/22, Little Co sells land...
Big Co. owns 60% of the stock of Little Co.   On 1/1/22, Little Co sells land to Big Co for $50,000. The land had cost Little Co. $30,000 several years earlier. On 3/1/25, Big Co sells the land to a thirds party for $80,000 Little Co reports earnings of $50,000 each year. What is the unrealized gain on sale in 2022? What is the income to the NC Interest in 2022 and 2023? What is the income to the NC...
Digital arithmetic: a) Convert +35 to 2-complement b) Convert -35 to 2-complement c) Convert 2-complement from...
Digital arithmetic: a) Convert +35 to 2-complement b) Convert -35 to 2-complement c) Convert 2-complement from 1101 1101 to decimal d) Add 35 - 35 in binary
Convert 101 from base-2 number system to base-10 number system Convert 101 from base-2 number system...
Convert 101 from base-2 number system to base-10 number system Convert 101 from base-2 number system to base-16 number system Convert 100 from base-10 number system to base-2 number system Convert 100 from base-10 number system to base-16 number system Convert ef from base-16 number system to base-2 number system Convert ef from base-16 number system to base-10 number system
On 1/1/22 Big Co. acquires 40% of Little Co's voting stock for $300,000. Little Co's book...
On 1/1/22 Big Co. acquires 40% of Little Co's voting stock for $300,000. Little Co's book value on that date was $500,000. Little Co. had the following mis-valued assets at 1/1/22: Land: Undervalued by $40,000 (total) Inventory, FIFO basis: Undervalued by $20,000 (total) Equipment, 5 year life: undervalued by $30,000 (total) Any remaining differential is attributed to goodwill. During 2022, Little reports earnings of $50,000 and pays dividends of $10,000 During 2023, Little reports earnings of $60,000 and pays no...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT