asgard20032 wrote:Because the negation (~) reverse all bit: 0 became 1, and 1 become 0. But on signed integer (and other signed variable), the first bit, the most left bit, is a little special. Its used for the sign.
For example, if we take a 4 bit variable, 0 is represented as 0000 in memory. 1 Is represented as 0001 for positive, and as 1000 for negative. So the way to convert from negative to positive, and positive to negative is this:
Positive -> Negative value
Reverse all bit, and add 1.
Negative-> Positive
Reverse all bit, and subtract 1.
Since unsigned type, have 1 more bit used for the number representation than signed, they can hold up bigger number.
So when you read a signed number, if the sign bit is set to 1, just subtract 1, and read it as normally, and add a - sign.
So for your -11, you need to add 1 : -11 + 1 = 10
Also, since 0000 is used for 0, it mean that positive value has 1 possibility less than negative value. Its why signed char have this range value: -128 to 127.
-128 because we can include 11111111 but since 00000000 is used for the 0, positive can't get up to 128.
Most of the time, we use Two complement binary system. Read this:
http://en.wikipedia.org/wiki/Two%27s_complementBut it may happen that a language or system(like calculator, some processor's native code) use a One complement system. Read this:
http://en.wikipedia.org/wiki/One%27s_complement
so it will be like -11 to 10? xD
I have another question

// Binary AND
c = a & b;
printf("c = a & b\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = a & b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");
// Binary OR
c = a | b;
printf("c = a | b\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = a | b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");
// Binary XOR
c = a ^ b;
printf("c = a ^ b\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = a ^ b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");
return 0;
}
here c = a & b; 'c' become 0 o,o
Is it because int c? xD and not unsign int c? o,o
and here
c = a | b; 'c' become 11 o,o
is it because a and b value add?
and last here, the 'c' current value is 11 but since c were use again.. it will change right? xD
c = a ^ b; 'c' become 11 again o,o
is it because a and b just add again? o,o and the previous 11 value were disregard? xD
Please tell me if this is right or wrong
thank you thank you
