Louis Reasoner repeats the above three sub-exercises determining the number of b
ID: 3887114 • Letter: L
Question
Louis Reasoner repeats the above three sub-exercises determining the number of bits for an unsigned int, long and signed int on his home computer, but he finds that the results don't match with what he found while working on the lab computers. How could this be?
Two's complement number representations have one more negative number than they do positive numbers. That is, the most negative number does not have a positive counter-part. The final piece of information printed is the signed value of what you get when you try to perform two's complement negation of the most negative value. Can you explain why this happens?
Explanation / Answer
1. The reason for seeing different results is due to the fact tha both computers might be running on different processors or both might be using different compilers. The sizes of the data types basically depends on the implementation of the compilers or the limits which have been imposed by the processors(hardware)
2. For a 8 bit signed integer, the range varies from -128 to +127, Means it has one more negative number than the positive number.. It happens due to the reason that 0 is always considered to be lying on the positive side.. Hence the implementation of 0 is given as 0000 0000. While -128 is implemented as 1000 0000 (both in signed format)
So if we need to print 1000 0000, we proceed in below way:
1. We notice signed bit is 1, it means it is a negative number. Print a '-ve' sign.
2. For Negative number, again take 2' complement of the given number.. It will come as 1000 0000, Now this will give us the absolute value of the number, that is 128.. Hence the complete number is -128.
Now Lets try to understand why the above happens.. A number as positive 8 is expressed as 0000 1000 in binary.. Now if we say we need -8, we need to take the 2's complement of the number.. The 2's complement of the number is 1111 1000. If you notice carefully, taking 2's complement of the number, makes the sign of the number opposite.. means it changed +8 to -8. Now if You Again take 2's complement of the number, it will become 0000 1000, That is +8. So taking 2s complement again made -8 to +8. (Sign change)
Hence to print the negative number(we already know it is negative by the signed bit), We print the sign seperately.. and take the modulous of negative number by passing it through 2's complement once.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.