问题
the teacher told us that a binary number, for examle 1000 0001, have 2 meanings. one is represent -127 (signed), which is from -127 to 127 and the another is a unsigned number , from 0 to 256
If I have a number in binary, for example 1000 0001 , the calculator shows only the signed number (-127). how can I know what is the unsigned number that this binary number represent?
回答1:
A signed and an unsigned number have exactly the same bits!
In your calculator, you can display as hex (0xff). It's up to you whether you want to interpret the hex digits and "signed" or "unsigned".
In x86 assembler, you can check the "sign bit" in the CPU status flags.
Check out this tutorial:
Signed vs Unsigned Numbers
回答2:
Unsigned numbers are the easiest to interpret from binary; simply add all the values that the bits represent (2^7+2^0
in the case of the number 1000 0001
=129).
The signed interpretation is pretty much the same, except for one extra step first:
If the leading digit is 1
, then you invert all the bits (in this case 0111 1110
) and add 1
to the result to get the (absolute) value of the negative number (in this case 0111 1111
=127)
To test that you did it correctly, do the same operation again and you should end up with the original number.
来源:https://stackoverflow.com/questions/37821887/interpreting-signed-and-unsigned-numbers