In this article, it says the following:
1111 + 0001 = 0000 (carry flag is turned on)
As far as I know, a carry happens when the result does not fit in a certain number of bits (4 bits in this example), so the above equation indeed caused a carry (because the result is in effect 10000
, which does not fit in 4 bits).
But look at the following from the same article:
0000 - 0001 = 1111 (carry flag is turned on)
I don't understand why the carry flag is set in this case, I mean the result is 1111
, so it fits in 4 bits!
we know from grade school that a - b = a + (-b) and we know that using twos complement to negate something means invert and add one. and some know that processors dont subtract they add instead so
is really
invert and add one, since we have to carry in anyway we add the one there rather than do extra work. so we invert the carry in and we invert the second operand on the way into the adder.
And you are correct that is the answer but some processors invert the carry out, and it sounds like the one you are using does that. Why? Because if you examine some other sets of numbers you see that the bit can be used as a borrow or not borrow bit rather than an unsigned overflow which is what it means for unsigned numbers, as in this case. 0000 + 1111 = 1111 which is not an unsigned overflow.
So from one architecture to the next (ARM, MIPS, AVR, x86, etc) if you want to use that flag (well not MIPS) you read the documentation, or do an experiment as the docs are not that good on this topic. Sometimes the add with carry or subtract with borrow instructions will hint at which way that architecture works (inverts the carry flag on the way out of a subtract).