Signed versus Unsigned Integers

2019-01-02 19:17发布

Am I correct to say the difference between a signed and unsigned integer is:

  1. Unsigned can hold a larger positive value, and no negative value.
  2. Unsigned uses the leading bit as a part of the value, while the signed version uses the left-most-bit to identify if the number is positive or negative.
  3. signed integers can hold both positive and negative numbers.

Any other differences?

15条回答
爱死公子算了
2楼-- · 2019-01-02 19:31

Generally speaking that is correct. Without knowing anything more about why you are looking for the differences I can't think of any other differentiators between signed and unsigned.

查看更多
残风、尘缘若梦
3楼-- · 2019-01-02 19:33

I'll go into differences at the hardware level, on x86. This is mostly irrelevant unless you're writing a compiler or using assembly language. But it's nice to know.

Firstly, x86 has native support for the two's complement representation of signed numbers. You can use other representations but this would require more instructions and generally be a waste of processor time.

What do I mean by "native support"? Basically I mean that there are a set of instructions you use for unsigned numbers and another set that you use for signed numbers. Unsigned numbers can sit in the same registers as signed numbers, and indeed you can mix signed and unsigned instructions without worrying the processor. It's up to the compiler (or assembly programmer) to keep track of whether a number is signed or not, and use the appropriate instructions.

Firstly, two's complement numbers have the property that addition and subtraction is just the same as for unsigned numbers. It makes no difference whether the numbers are positive or negative. (So you just go ahead and ADD and SUB your numbers without a worry.)

The differences start to show when it comes to comparisons. x86 has a simple way of differentiating them: above/below indicates an unsigned comparison and greater/less than indicates a signed comparison. (E.g. JAE means "Jump if above or equal" and is unsigned.)

There are also two sets of multiplication and division instructions to deal with signed and unsigned integers.

Lastly: if you want to check for, say, overflow, you would do it differently for signed and for unsigned numbers.

查看更多
还给你的自由
4楼-- · 2019-01-02 19:33

Basically he asked only about signed and unsigned. Don't know why people are adding extra stuff in this. Let me tell you answer.

  1. Unsigned: It consist only positive value i.e 0 to 255.

  2. Signed: It consist both negative and positive values but in different formats like

    • -1 to -128
    • 0 to +127

And this all explanation is about 8 bit number system.

查看更多
怪性笑人.
5楼-- · 2019-01-02 19:35

Everything except for point 2 is correct. There are many different notations for signed ints, some implementations use the first, others use the last and yet others use something completely different. That all depends on the platform you're working with.

查看更多
君临天下
6楼-- · 2019-01-02 19:36

Another difference is when you are converting between integers of different sizes.

For example, if you are extracting an integer from a byte stream (say 16 bits for simplicity), with unsigned values, you could do:

i = ((int) b[j]) << 8 | b[j+1]

(should probably cast the 2nd byte, but I'm guessing the compiler will do the right thing)

With signed values you would have to worry about sign extension and do:

i = (((int) b[i]) & 0xFF) << 8 | ((int) b[i+1]) & 0xFF
查看更多
看淡一切
7楼-- · 2019-01-02 19:38

Just a few points for completeness:

  • this answer is discussing only integer representations. There may be other answers for floating point;

  • the representation of a negative number can vary. The most common (by far - it's nearly universal today) in use today is two's complement. Other representations include one's complement (quite rare) and signed magnitude (vanishingly rare - probably only used on museum pieces) which is simply using the high bit as a sign indicator with the remain bits representing the absolute value of the number.

  • When using two's complement, the variable can represent a larger range (by one) of negative numbers than positive numbers. This is because zero is included in the 'positive' numbers (since the sign bit is not set for zero), but not the negative numbers. This means that the absolute value of the smallest negative number cannot be represented.

  • when using one's complement or signed magnitude you can have zero represented as either a positive or negative number (which is one of a couple of reasons these representations aren't typically used).

查看更多
登录 后发表回答