简体   繁体   中英

C and signed byte representation in memory

There's something I dont get:

A signed char in C is represented on 8 bits: 00000000

  • 0 is 0000 0000 => 0
  • 1 is 0000 0001 => 1
  • ...
  • 127 is 0111 1111 => 127

So I thought the 8th bits is the sign bit. So I thought:

  • 128 is 1000 0000 => 0
  • 129 is 1000 0001 => -1

But no!

Try this:

int main(int argc, const char *argv[])
{
    int i;
    for (i = 0; i < 256; i++) {
        printf("%3d = %d\n", i, (signed char)i);
    }
    return 0;
}

And you get: gcc -o tt tt.c ; ./tt | more gcc -o tt tt.c ; ./tt | more

  • ...
  • 126 = 126
  • 127 = 127
  • 128 = -128
  • 129 = -127
  • 130 = -126
  • 254 = -2
  • 255 = -1

How comes?

This is called the 2's complement representation of signed integers and is used for all integer types.

The idea is this: The adder in the CPU simply ignores when an overflow happens and wraps around, so you are actually calculating on a modulo ring of numbers. So, if you subtract 1 from zero, you get

 00000000 = 0
-00000001 = 1
--------------
 11111111 = -1

and so on.

That way, your computer can simply ignore signs while calculating. This is even true for multiplications, as in this example multiplication of 3 and -2 (4-bit arithmetic for brevity):

0011 * 1110
-----------
       0000
      0011
     0011
    0011
-----------
     101010
truncated to 4 bits: 1010

1010 is the negative of 0110 , which is 6, so the result is -6 as it should be.

There are only very few points where signedness needs to be taken into account, like comparisons, divisions and casts to larger integer types. Comparisons are relatively straightforward, casts work like this:

00000001 -> 0000000000000001
10000000 -> 0000000010000000 (unsigned extension 128 -> 128)
10000000 -> 1111111110000000 (sign extension, the sign bit is copied into all new bits, preserving the value of negative numbers -128 -> -128)

This is because of 8 bit 2's compliment encoding.Signed character range is from -128 to 127.

EDIT1: Response to @Grijesh's comment

-128 and +128 have the same representation: 10000000.So to avoid confusion and to make a rule All bit-patterns with 1 in higher-order bit represent negative numbers it is decided to consider it as -128 which makes range from -128 to 127 rather than -127..+128.

In two's complement, the left-most bit is negative, and the others positive, so

1000 0000

=> -128

and

1000 0001

=> -128 + 1 = -127

Therefore the largest number is 0111 1111 (127) and smallest is 1000 0000 (-128)

You can think signed datatypes as a clock. It will always hit 12 O clock after 12 hours. Char range is -128 to +127 . So as you hit 127 according to my clock rule next hit will be -128 .

I suggest you to do some binary calculation.

For more information read http://en.wikipedia.org/wiki/Two%27s_complement

In c char is the only 1 byte memory allocate means totally 8 bit. This is because of 8 bit 2's compliment encoding.Signed character range is from -128 to 127.
because you used.

   printf("%3d = %d\n", i, (signed char)i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM