What does signed and unsigned values mean?

后端 未结 9 859
旧巷少年郎
旧巷少年郎 2020-12-14 02:28

What does signed mean in C? I have this table to show:

\"enter

Th

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 03:09

    It was a typo in the book; signed char goes from -128 to 127.

    Signed integers are stored using the two's complement representation, in which the first bit is used to indicate the sign.

    In C, chars are just 8 bit integers. This means that they can go from -(2^7) to 2^7 - 1. That's because we use the 7 last bits for the number and the first bit for the sign. 0 means positive and 1 means negative (in two's complement representation).

    • The biggest positive 7 bit number is (01111111)b = 2^7 - 1 = 127.
    • The smallest negative 7 bit number is (11111111)b = -128
      (because 11111111 is the two's complement of 10000000 = 2^7 = 128).

    Unsigned chars don't have signs so they can use all the 8 bits. Going from (00000000)b = 0 to (11111111)b = 255.

提交回复
热议问题