Before we dive into the technical side, let's address something upfront here.
I know that most people, myself included, don't really enjoy math, especially when it gets abstract
or overly technical.
However, the math we will use in today's lecture is not boring math.
To be honest, it's practical, visual, and absolutely essential for understanding how computers work
at a lowest level.
Now, you do not need to be a math expert to follow along.
What you will need is a bit of patience and willingness to look at the numbers in a new way.
Once you start seeing how simple operations like addition subtraction work inside the computer, it
becomes very logical and surprisingly interesting.
Now this lecture will set the foundation for everything we will do with the low level computing, which
is the number one requirement of the low level computer is math.
Right now, especially in system programming, uh, digital forensics and embedded systems, you will
always need the low level computing math.
Now, in the digital world, everything must fit into a fixed number of bits.
Now.
That's right.
No matter how big or a smaller number is, it must be squeezed into a limited space.
Now, previously, as you remember, we discussed how data types are stored in a memory.
Um, a variable declared as integer, for example, might get 32 bits here, 32 time bits, which is
either 1 or 0 right now.
Um, and the character only gets eight bits eight times bit, which is it is either 1 or 0.
The limit is absolute, so there's no overflow zone or extra margin.
Now this limitation creates complications for arithmetic operations.
Now, if you do a calculation that goes beyond the space available here, something has to give.
Right.
And what gives this accuracy?
Now you will either lose data or wrap around to a different result entirely.
And in our daily lives we do math on paper or in our heads.
We assume that we can keep writing more digits and so on and so forth.
But in computing computing, we can't.
Now the CPU has to make decisions based on a set of number of bits.
And to manage these constraints, the CPU include single bit bit condition flags in the architecture.
Now these are part of registers called the status register or flags register or flags.
And the two flags we will talk about here is carry flag and overflow flag.
Carry flag.
Now this is used in unsigned arithmetic.
Now it tells us whether a carry out of the most significant bit has occurred.
Now, in simple terms, it means the result was too big to fit and in the overflow flag.
This is used in a signed arithmetic.
It tells us whether the sign bit is incorrect due to the operation, indicating that an overflow has
occurred in a signed context.
So this is an unsigned and this is signed arithmetic.
Now we will go deeper into these flags in upcoming lectures.
But for now just remember they tell us when something went wrong in a binary.
Now welcome back.
In this lecture we are going to talk about how computers do arithmetic.
Now we will start with what's familiar which is a decimal numbers.
Now before we jump into binary it is extremely helpful to understand how we manually perform addition
and subtraction with decimal numbers.
Now, because the logic is the same just with different digits and base values.
And since this is a visual visual lecture, I will go with you with a step by step drawings as we go.
Now in the decimal addition you already know.
Let's start with a simple example.
Now let's say we are adding 67 and 79 addition.
Now we begin by adding the digits in the ones place right seven and nine which is 16.
So that's more than ten.
So we write down six and carry one to the tens place here.
And now we have six from seven nine and plus one carry.
What we have here is seven plus seven which is 14.
Since we don't have any numbers here or here we will just end the subtraction.
And that's our summary.
And yeah that's basically the summary of this decimal addition.
Now let's break down the algorithm that makes this work right now.
Algorithm for for decimal addition.
Now let's suppose x or let's use a different color.
Here x and y are two n digit decimal numbers.
Now we will add them digit by digit from right to left.
Now let's call x I.
This is the digit from the number x at position I and y I the digit from the number y at position I,
and we have the carry and sum.
And why does this work here?
Because we are using positional notation.
Every digit to the left here is worth ten times more.
Here this 10th.
And this is 100th.
So and this goes on right.
And that is how the addition algorithm works.