Olivier Pons
Olivier Pons

Reputation: 15778

C and signed byte representation in memory

There's something I dont get:

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

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

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

How comes?

Upvotes: 6

Views: 4477

Answers (5)

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)

Upvotes: 8

Dayal rai
Dayal rai

Reputation: 6606

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.

Upvotes: 5

someone
someone

Reputation: 1696

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

Upvotes: 2

Mohsin Shaikh
Mohsin Shaikh

Reputation: 504

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);

Upvotes: 1

Rob
Rob

Reputation: 208

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)

Upvotes: 4

Related Questions