A while ago I set myself the challenge of replicating the basic mathematical operators in (C++) computer code, without using any of the basic mathematical operators themselves.
Using binary, I was able to reduce addition, subtraction, multiplication and division solely to logical operators and “bit-shifting” (0010 shifted left once is 0100 etc. - any binary digit (bit) shifted in from the right “from nowhere” is always zero).
I observed the same process that you learn in school, writing one number above the other and conducting addition etc. starting with the smallest colomn and working up, “carrying” when appropriate.
Addition was the simplest, requiring only the logical XOR function (exclusive OR) and the logical AND function, bit-shifted left - and repeating the process with the two results until the answer is converged upon.
Relevant logic tables, just to be explicit (using C++ syntax):
[tab]XOR:
0^0 = 0
0^1 = 1
1^0 = 1
1^1 = 0
AND:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1[/tab]
Example, 5 + 3 = 8:
[tab]5 in (4-bit) binary is 0101
3 in (4-bit) binary is 0011
8 in (4-bit) binary is 1000
0101
0011+
1000
Using the logical process described above:
Stage 1:
0101 ^ 0011 = 0110 (6 in decimal)
0101 & 0011 = 0001, 0001 << 1 = 0010 (bit shifting the “AND” result once to the left) (2 in decimal)
Stage 2:
0110 ^ 0010 = 0100 (4 in decimal)
0110 & 0010 = 0010, 0010 << 1 = 0100 (also 4 in decimal)
Stage 3:
0100 ^ 0100 = 0000 (obviously 0 in decimal)
0100 & 0100 = 0100, 0100 << 1 = 1000 (8 in decimal)
Stage 4:
0000 ^ 1000 = 1000 (8 in decimal)
0000 & 1000 = 0000 (obviously 0 in decimal, even if bit-shifted)
Any subsequent stages will yield the exact same results indefinitely, so the process terminates here giving the expected answer of 5 + 3 = 8. Different numbers need different numbers of stages before they are complete.[/tab]
Essentially this iterative process re-arranges the two terms into numbers that have no two terms the same for each colomn, with the numbers at each stage adding together to equal the answer.
It is also functionally equivalent to a “logic” table for addition (very similar to XOR):
[tab]0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 (with this special case actually being “10”, with the “AND” result being shifted left to simulate the “carrying” process when you run out of digits in whatever number base you are operating with).[/tab]
Subtraction is remarkably similar. A “logic” table for subtraction (also very similar to XOR):
[tab]0 - 0 = 0
0 - 1 = 1 (with this special case actually being “1” with 1 deducted from all bits to the left of this 0 that has 1 subtracted from it, until a 1 is reduced to 0. For some reason that I’m not actually quite sure of, the same simulation of the “carrying” process as used in addition amounts to the same effect).
1 - 0 = 1
1 - 1 = 0[/tab]
Basically the same as addition, but with the “AND” operator replaced by a programmer-defined function (let’s call it “?”) that swaps the “1 & 1 = 1” in the AND table with “0 ? 1 = 1” (the others result in zero in just the same way). The same “bit-shift to the left” is applied to the result of this operation after each stage.
Example 5 - 3 = 2:
[tab]Stage 1:
0101 ^ 0011 = 0110 (6 in decimal)
0101 ? 0011 = 0010, 0010 << 1 = 0100 (4 in decimal)
Stage 2:
0110 ^ 0100 = 0010 (2 in decimal)
0110 ? 0100 = 0000 (0 in decimal, even if bit-shifted)
Subsequent stages will yield the exact same results indefinitely, so the process terminates here giving the expected answer of 5 - 3 = 2. Only 2 stages were needed this time.[/tab]
Multiplication is glorified addition: 5 * 3 is the same as 5 + 5 + 5 (5 added to itself 3 times) = 15.
It can obviously be simplified to a further iteration applied to my logical interpretation of addition.
Division is glorified subtraction: 5 / 3 is the same as subtracting 3 from 5 until the next time would result in a negative value, taking the remainder (the modulus) and shifting the digit once to the left, and subtracting the denominator from it again until the next time would result in the negative value - the value is placed in the next decimal (or other base system) place to the right (20 / 3 = 6, remainder 2: this recurs infinitely to equal 1.6 recurring).
Powers are glorified multiplication and roots are glorified division etc. All mathematical operators boil down to these basic logical steps.
Mathematics is built on logic.
I think I heard elsewhere on this forum that the opposite were true. If anyone believed this to be so, is this still so despite my demonstration? If so, why?