Instead of addition, subtraction, multiplication, and division operators, find 7 times the integer

Mondo Education Updated on 2024-02-08

There are three types of methods that can be used: bitwise addition, JS hack, and base conversion. The implementation is as follows:

The key to this problem is that you can't use the notation of the operation, so a direct idea is whether you can add and subtract integers without addition, subtraction, multiplication and division? In fact, it is not difficult, review the principles of computer composition in college textbooks, and you should be able to think of how to implement basic addition, subtraction, multiplication and division. Here, we really only need to implement a basic addition:

As you can see from the table above, an algorithm that implements simple multi-bit binary integer addition is as follows.

m and n are two binary integers, find m+n:

Use the operation to find the bit that is 1 together with m and n: m'= m & n, use XOR operations to find the bit of m and n, where one is 1: n'= m n if m'If it is not 0, then it will be m'Shift one place to the left (carry), i.e. m = m'<<1, i.e. n = n'to skip back to step 1 if m'is 0, then n'That's what we're asking for.

* -bit-arithmetic -- define bitbit arithmetic addition function bitadd(m, n) return n; } Bit operation implementation 1 - Loop accumulate 7 times let multiply7 bo 1 = (num)=> return sum; } Bit arithmetic implementation 2 - After the binary is 3 bits (multiplied by 8), add its own complement (multiplied by -1) let multiply7 bo 2 = (num) => bitadd(num <<3, -num) ; * -js hack -- hack way 1 - bytecode using function's constructor & multiplier let multiply7 hack 1 = (num) => new function(["return ",num,string.fromcharcode(42),"7"].join(""( hack method 2 - bytecode with eval executor & multiplication sign let multiply7 hack 2 = (num) => eval([num,string.]fromcharcode(42),"7"].join(""));Hack Method 3 - Bytecode with settimeout parameter & multiplication sign settimeout(["window.multiply7_hack_3=(num)=>(7",string.fromcharcode(42),"num)"].join("")) * -Base Conversion -- Base Conversion Method - Use tostring to convert to a seven-decimal integer; Then add 0 at the end (one place to the left) and turn back to decimal via parseint let multiply7 base7 = (num)=>parseint([num.tostring(7),'0'].join(''),7);

Related Pages