How Computers Perform Mathematical Calculations Under the Hood?
Saying ‘Computers are really an amazing device’ is no-brainer, but it’s quite fascinating to think in terms of collections of circuits that are functioning so well that it takes fractions of sec. to solve complex logic that a typical human possibly can’t.
It’s surreal how capable it has become🤖
Let’s investigate how it’s really doing all that it does. What if I say we all are aware of it already, we just need to make the connection what we already know.
So, excited?👾
Let me approach it with a simpler question, say how does a computer interpret the numbers? Again, how does it add two numbers together?
Computer’s Number System :
As we know, Binary is the number system that computer uses.
You may wonder here why do computers use binary number system?
Or why do computers convert to and from binary numbers and not just using our usual decimal number system? Is decimal(base10) computer also possible?
Yes, in fact, the very first electronic computer, ENIAC, was based on base10 numbers. Even Charles Babbage’s Analytical Engine was fully mechanical and used standard base 10 numbers.🤨
But why bother with binary?
Wouldn’t it be more efficient to store data with comparatively less circuitry(less digits) for same calculation?.
Yes, it’ll be but main reason was circuitry with binary consumes less energy resulting cost reduction, and it avoids unambiguous calculation.
As it’s quite simpler to make a circuitry that switches between two states, for instance, 0 and +5V, than between say 10 states.
The 2 state electronic gate switches between off and on.
Whereas the Multistate switch would have to switch between levels that are much closer together, which would demand significant strict control over the currents and voltages involved. Moreover, by the time the precise levels of the multistate switches could drift and would eventually cause a malfunction.
And I believe USP of any machine is reduction of human error. A bit miscalculation can do much harm to any business one can’t estimate.
Hence, we can’t rely on a machine that tends to malfunction.
Someone has rightly said Computer isn’t really all that clever.
Yes, it’s pretty efficient and smart to take some logic circuit and have it do your maths assignments, but eventually, it’s not performing any operations that a 10 year old child couldn’t. The only difference between a computer and a 10-year-old in this sense is it’s precision level and efficiency, computers don’t get slow down or tired, or bored.😎
Computer’s Algebra:
To perform any mathematical calculation, one needs to be aware of mathematical algebra. And as Computer boils down all number to binary number system before performing calculation on them and then again, coverts solution to it’s corresponding number system.
Hence, we come to the point that computer needs an algebra that can work on binary numbers.
Magically, George Boole has already done that back in 1847 for us.
He devised a method of algebra on binary values, that takes certain inputs, evaluates logically the output. He essentially gave us logic tools like AND, OR, NOT, XOR, etc.
In case you need to refresh functioning of logic gates, click here.
You must have learnt that bitwise operation are the fastest and simplest operation than the arithmetic’s as it’s operate on individual bit level and it’s directly supported by the processor. You may relate now why bitwise is fast & why it’s supported at processor level as computer performs calculation internally on binary no. only.
To know more about bitwise operators, click here.
What if one ask to write a function to perform addition of two numbers, but without using arithmetic operators?
You don’t have to use + operator, surely not any native methods too.
How you will approach this question?
One must be aware of how the computer performs internally to evaluate this calculation, then probably it may take couple of min to formulate the logic.
Coming from electronic background, I can recall the Adder circuitry that we have learnt during engineering days. There’s typically two adder circuit namely half-adder and full adder logic circuit to evaluate the sum of given numbers.
Only difference between two is the full adder circuit does multi-level addition considering the carry bit with numbers to be added whereas half adder doesn’t include carry bit while addition.
So, to write a function to add two numbers could be deduced from half adder logic circuit that takes two input parameters and passes both into XOR logic gate that returns sum bit, and when same input gets passed into AND gate it returns the carry bit.
For ref. this addition algebra is being done on binary(bit level), hence sum and carry bit is evaluated individually. Like if we do 9 + 6 in decimal it gives 15( 5 as sum and 1 as carry and eventually carry gets evaluated resulting 15)
The correct sum that’s needed is without carry bit. Hence, we have to add carry at proper positional notation and have to make sure there’s no carry in final solution.
In terms of code, it could be written as below:
//half adder circuit logic
function addTwoNumbers(num1, num2) {
//looping till carry bit isn't zero
while (num2 !== 0) {
//applying bitwise XOR between two input to evaluate sum bit
let sum = num1 ^ num2;
//applying bitwise AND between two input to get carry bit; and then
//left shift by 1 bit to make sure carry gets added at correct position
let carry = (num1 & num2) << 1;
//reassigning the parameters with sum & carry bit,so final sum without
//carry gets returned as when num2 gets assigned 0 carry,while gets stopped
[num1, num2] = [sum, carry];
}
//when carry bit gets zero and gets reassigned into second param.
//num1 will get returned with final addition value
return num1;
}
addTwoNumbers(6, 9);
Hence, one can say all complex logics gets boiled down to digital binary circuitry, that’s again nothing but the digital logic gates to facilitate algebra among binary number system.
That’s it for now!
Thanks for reading.. 🥂