← Back to challenges

Subtract Without Subtract

JavaScriptHardnumbersmathbit_operations

Instructions

Create a function that subtracts one positive integer from another, without using any arithmetic operators such as -, %, /, +, etc.

Examples

mySub(5, 9) ➞ 4

mySub(10, 30) ➞ 20

mySub(0, 0) ➞ 0

Notes

  • Do not multiply by -1.
  • Use bitwise operations only: <<, |, ~, &, etc.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.