Reputation: 203
Just wanted to know something about signed vs unsigned interpretation. Am I right if I say that if a number is unsigned integers it's value is calculated normally by converting the binary into decimal..but when it comes to signed integers, the sign bit(msb) is noted first and then the binary value is calculated using 2's complement ; the value calculated is the real value with the sign noted.
Am I right or missing something?
Upvotes: 1
Views: 2020
Reputation: 23670
Yes, you are right. When converting a signed integer represented in 2's complement form into a decimal number, this is how you would do it. And 2's complement is how Intel processors store and do math with signed integers.
NOTE: But this answer is not about how Intel machines represent signed integers. It is general.
The technique you use to convert to decimal really depends on the representation used by the hardware.
A hardware designer may choose to store a signed number as:
11111111
represents a -0
, and 00000000
a +0
)And we humans need to use the corresponding 'conversion' when reading those in our preferred, decimal form.
e.g. If you were designing hardware that mostly does multiplications, storing signed integers in sign-magnitude form would make some sense. You just multiply the signs and magnitudes separately. On the other hand 2's complement numbers are a little unwieldy to multiply, but not much.
Of course, 2's complement is the way most hardware (almost all the general purpose CPUs today) stores signed numbers. This makes additions a piece of cake.
Upvotes: 1
Reputation: 62048
What you've described is the most typical way to convert signed binary integers into human-readable strings with decimal digits.
However, this is not the only way. You can convert your 2's complement negative 3-bit value 111 first into "7" and then subtract "8" from it and arrive to "-1".
Upvotes: 0