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();

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;

}


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