There's something I dont get:
A signed char
in C is represented on 8 bits:00000000
0000 0000
=> 00000 0001
=> 10111 1111
=> 127So I thought the 8th bits is the sign bit. So I thought:
1000 0000
=> 01000 0001
=> -1But 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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With