Operations
16.9 Operations: Write methods to implement the multiply, subtract, and divide operations for integers. The results of all of these are integers. Use only the add operator.
The key to this problem was to try and simplify as much as possible the operators using other operators. For example, subtraction is addition negated. Multiplication is stacking up addition. Division is just counting the amount of times you can subtract a number.
So when we define negate, we immediately can define subtraction.
With subtraction we can immediately define division.
Multiplication can be defined immediately on its own. I use xor to know when I have to negate.
One flaw I have right now is with the negate and absNew, because they rely on eachother - they get into an infinite loop - so I've had one of them use the the real abs. Edit: one solution to this would be to negate a number by flipping its MSB.
Last updated