Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C and signed byte representation in memory

Tags:

c

byte

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

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

How comes?

like image 635
Olivier Pons Avatar asked Aug 02 '13 09:08

Olivier Pons


1 Answers

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)
like image 89
cmaster - reinstate monica Avatar answered Oct 24 '22 14:10

cmaster - reinstate monica