Reverse a number without having to do any conversions!
To install, run npm install reverse-number --save
The following snippet shows just how easily you can reverse a number:
const reverse = require('reverse-number');
const my_number = 1234; // The number to be reversed
const my_number_reversed = reverse(my_number); // The reverse of your number
console.log(`${my_number} reversed is ${my_number_reversed}`); // Prints 1234 reversed is 4321The reversing is one function with one statement. Apart from ?: and calling itself, it uses four operators: ~, &, << and >>>. It has no +, -, *, / or %, no comparisons and no numeric literals.
- Every constant comes from the input.
n & ~nis 0,~(n & ~n)is -1, and-1 >>> -1is 1, because a shift count of -1 means 31. Every 1 in the code is written out as(~(n & ~n) >>> ~(n & ~n)). - OR and XOR are built from
~and&. - To add, the function calls itself with a third argument and runs a ripple-carry adder, one call per round of carries. Subtracting adds the two's complement.
- Dividing by 10 is the shift-and-add method from Hacker's Delight, inlined everywhere it's needed.
The input checks are ordinary JavaScript.
- Numbers must be whole and not negative.
- Numbers must be at most 4294967295, the largest 32-bit unsigned integer, and so must their reverse.
reverse(1000000003)is3000000001, butreverse(1000000005)throws, as5000000001is too big. - Anything else throws an error saying which rule it broke.
