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

#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...
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                      ...
(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 << "#"; }
import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k) { this.i=i; this.j=j; this.k=k; } public String toString() { return "A("+i+","+j+","+k+")"; } } class Main { public static void main(String[] args) { ArrayList<A> aL=new ArrayList<A>(); Random rand= new Random(1000); //1000 is a seed value for (int p=0; p<10; p++) { int i = rand.nextInt(100); int j = rand.nextInt(200); int k = rand.nextInt(300); aL.add(new A(i, j, k)); } System.out.println("----- Original arraylist------"); for (A a: aL)...
#include <iostream> using namespace std; int main() { int even=0,odd=0,sum=0,sum2=0,largest,smallest; int num,i; for ( i=1; i<=10;...
#include <iostream> using namespace std; int main() { int even=0,odd=0,sum=0,sum2=0,largest,smallest; int num,i; for ( i=1; i<=10; i++){ cout << " Enter " << i << " number: "; cin >> num; if ( num%2==0){ even++; sum+=num; } else { odd++; sum2+=num; if(num>largest){ largest = num; } if(num<largest) { smallest = num; } } cout << " The sum of even number is : " << sum << endl; cout << " The total-count of even number is : " <<...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT