Question

In: Computer Science

unsigned u =10; int i = -42; cout << i+i <<endl; cout << u+i<<endl;//if int take...

unsigned u =10;
int i = -42;
cout << i+i <<endl;
cout << u+i<<endl;//if int take 32 bit, output 4294967264

I know when unsigned and singed operate together, they need to be converted, but why is the answer 4294967264? What does it have to do with int .…… 32 bit

Solutions

Expert Solution

-42 in stored in memory using 2's complement
This is negative. so, follow these steps to convert this into a 2's complement binary
Step 1:
Divide 42 successively by 2 until the quotient is 0
   > 42/2 = 21, remainder is 0
   > 21/2 = 10, remainder is 1
   > 10/2 = 5, remainder is 0
   > 5/2 = 2, remainder is 1
   > 2/2 = 1, remainder is 0
   > 1/2 = 0, remainder is 1
Read remainders from the bottom to top as 101010
So, 42 of decimal is 101010 in binary
Adding 26 zeros on left hand side of this number to make this of length 32
So, 42 in normal binary is 00000000000000000000000000101010
Step 2: flip all the bits. Flip all 0's to 1 and all 1's to 0.
   00000000000000000000000000101010 is flipped to 11111111111111111111111111010101
Step 3:. Add 1 to above result
11111111111111111111111111010101 + 1 = 11111111111111111111111111010110
so, -42 in 2's complement binary is 11111111111111111111111111010110

unsigned value of 11111111111111111111111111010110 is
=> 11111111111111111111111111010110
=> 1x2^31+1x2^30+1x2^29+1x2^28+1x2^27+1x2^26+1x2^25+1x2^24+1x2^23+1x2^22+1x2^21+1x2^20+1x2^19+1x2^18+1x2^17+1x2^16+1x2^15+1x2^14+1x2^13+1x2^12+1x2^11+1x2^10+1x2^9+1x2^8+1x2^7+1x2^6+0x2^5+1x2^4+0x2^3+1x2^2+1x2^1+0x2^0
=> 1x2147483648+1x1073741824+1x536870912+1x268435456+1x134217728+1x67108864+1x33554432+1x16777216+1x8388608+1x4194304+1x2097152+1x1048576+1x524288+1x262144+1x131072+1x65536+1x32768+1x16384+1x8192+1x4096+1x2048+1x1024+1x512+1x256+1x128+1x64+0x32+1x16+0x8+1x4+1x2+0x1
=> 2147483648+1073741824+536870912+268435456+134217728+67108864+33554432+16777216+8388608+4194304+2097152+1048576+524288+262144+131072+65536+32768+16384+8192+4096+2048+1024+512+256+128+64+0+16+0+4+2+0
=> 4294967254

so, u + i = 10 + 4294967254 = 4294967264

Related Solutions

Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT   = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE =...
c++ programming What will the following C++ code display? int numbers[4]={99,87}; cout<<numbers[3]<<endl; 87                     &nbs
c++ programming What will the following C++ code display? int numbers[4]={99,87}; cout<<numbers[3]<<endl; 87                            b. 0                                  c. garbage                        d. an error What is the output of the following program segment? double mylist[5]; for(int i=0; i<5; i++) mylist[i]= (pow(i,2)+ 1 /2.0; cout<<fixed<<showpoint<<setprecision(2); for(int i=0; i<5; i++) { cout<<setw(5)<<mylist[i]; }               a. .5           1.5        4.5        9.5        16.5               b. 0.50      1.50      4.50      9.50     16.50              c. 0.50      1.50      8.50     27.50     64.50             d. 0.50      1.50      2.50      3.50      4.50      15. what is stored...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
#include using namespace std; int menu(){    int x;    cout<<"1.Add Entry\n";    cout<<"2.Edit Entry\n";   ...
#include using namespace std; int menu(){    int x;    cout<<"1.Add Entry\n";    cout<<"2.Edit Entry\n";    cout<<"3.remove Entry\n";    cout<<"4.print Entry\n";    cout<<"5.Close Console\n";    cout<<"Enter Your Choice:-";    cin>>x;              return x; } int main() {    int x;    int entry[1000];    int entnum = 0;    int num = 0;    int nom =0;     while (1)     {         int choice = menu();         if (choice == 1){            cout<<"A entry " <<...
int value = 1; do { if (value % 2 == 0) cout << value <<...
int value = 1; do { if (value % 2 == 0) cout << value << " "; value = value + 1; } while (value % 7 != 0); cout << "\n" << value; What will be displayed?
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left;...
#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left; using std::right; using std::setw; using std::string; // The IntVariableTable constructor dynamically allocates the fixed size array of integer variables. IntVariableTable::IntVariableTable() { int_variable_table = new IntVariableEntry[MAX_INT_VARIABLES]; } // The IntVariableTable destructor deallocates the integer variable array. IntVariableTable::~IntVariableTable() { delete[] int_variable_table; } // Returns the number of variables added to the integer variable table. int IntVariableTable::numVariables() const { return num_int_variables; } // Returns the index of...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {     cout<<i<<endl; }
14. How many times does "#" print? for(int i = 0; i < 10; ++i) {...
14. How many times does "#" print? for(int i = 0; i < 10; ++i) { if(i == 2 || i==4 || i==6) { continue; } cout << "#"; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT