I’m taking a Computer Systems class as a pre-req for my Masters and came across something I found fascinating and hard to see practical use of and that is “faking subtraction” and the fact that there doesn’t need to be a subtraction instruction.
Something like:
x – y
Can be written as:
x + (~y + 1)
Now, that’s all well and good but it seems like that is overly complicated for a simple subtraction, especially when you could just easily put “x – y”. Are there situations where it would be necessary to do this, or is it just something that CAN be done but isn’t.
The two’s complement implementation is done in hardware, so you do not need to implement them like that for builtin datatypes.
If you are making an n-bit integer arithmetic library, then you need to emulate the integer addition, subtraction, multiplication and division etc operations, in which case such a technique might be implemented to add the n-bit length numbers, but using the carry flag to do so is a better implementation in my opinion.