WEBVTT

00:00.480 --> 00:05.160
Before we dive into the technical side, let's address something upfront here.

00:05.200 --> 00:12.880
I know that most people, myself included, don't really enjoy math, especially when it gets abstract

00:12.880 --> 00:14.720
or overly technical.

00:15.040 --> 00:21.400
However, the math we will use in today's lecture is not boring math.

00:21.440 --> 00:29.760
To be honest, it's practical, visual, and absolutely essential for understanding how computers work

00:29.960 --> 00:32.000
at a lowest level.

00:32.040 --> 00:36.120
Now, you do not need to be a math expert to follow along.

00:36.280 --> 00:41.840
What you will need is a bit of patience and willingness to look at the numbers in a new way.

00:42.040 --> 00:49.360
Once you start seeing how simple operations like addition subtraction work inside the computer, it

00:49.360 --> 00:52.160
becomes very logical and surprisingly interesting.

00:52.200 --> 00:58.880
Now this lecture will set the foundation for everything we will do with the low level computing, which

00:58.880 --> 01:05.200
is the number one requirement of the low level computer is math.

01:05.600 --> 01:11.950
Right now, especially in system programming, uh, digital forensics and embedded systems, you will

01:11.950 --> 01:15.710
always need the low level computing math.

01:15.750 --> 01:20.990
Now, in the digital world, everything must fit into a fixed number of bits.

01:21.150 --> 01:21.350
Now.

01:21.350 --> 01:21.790
That's right.

01:21.830 --> 01:29.230
No matter how big or a smaller number is, it must be squeezed into a limited space.

01:29.350 --> 01:34.350
Now, previously, as you remember, we discussed how data types are stored in a memory.

01:34.750 --> 01:46.510
Um, a variable declared as integer, for example, might get 32 bits here, 32 time bits, which is

01:46.510 --> 01:50.830
either 1 or 0 right now.

01:51.270 --> 02:02.990
Um, and the character only gets eight bits eight times bit, which is it is either 1 or 0.

02:03.350 --> 02:09.590
The limit is absolute, so there's no overflow zone or extra margin.

02:09.830 --> 02:13.660
Now this limitation creates complications for arithmetic operations.

02:13.700 --> 02:19.940
Now, if you do a calculation that goes beyond the space available here, something has to give.

02:19.980 --> 02:20.380
Right.

02:20.420 --> 02:22.780
And what gives this accuracy?

02:22.820 --> 02:29.300
Now you will either lose data or wrap around to a different result entirely.

02:29.580 --> 02:34.460
And in our daily lives we do math on paper or in our heads.

02:34.500 --> 02:40.100
We assume that we can keep writing more digits and so on and so forth.

02:40.100 --> 02:43.500
But in computing computing, we can't.

02:43.740 --> 02:48.500
Now the CPU has to make decisions based on a set of number of bits.

02:49.140 --> 02:58.100
And to manage these constraints, the CPU include single bit bit condition flags in the architecture.

02:58.140 --> 03:05.860
Now these are part of registers called the status register or flags register or flags.

03:06.020 --> 03:14.380
And the two flags we will talk about here is carry flag and overflow flag.

03:14.540 --> 03:15.580
Carry flag.

03:15.620 --> 03:19.210
Now this is used in unsigned arithmetic.

03:19.370 --> 03:24.410
Now it tells us whether a carry out of the most significant bit has occurred.

03:24.570 --> 03:31.770
Now, in simple terms, it means the result was too big to fit and in the overflow flag.

03:32.410 --> 03:34.730
This is used in a signed arithmetic.

03:34.930 --> 03:42.330
It tells us whether the sign bit is incorrect due to the operation, indicating that an overflow has

03:42.330 --> 03:45.610
occurred in a signed context.

03:45.610 --> 03:51.770
So this is an unsigned and this is signed arithmetic.

03:52.010 --> 03:55.530
Now we will go deeper into these flags in upcoming lectures.

03:55.530 --> 04:01.610
But for now just remember they tell us when something went wrong in a binary.

04:02.050 --> 04:03.130
Now welcome back.

04:03.890 --> 04:07.170
In this lecture we are going to talk about how computers do arithmetic.

04:07.490 --> 04:12.850
Now we will start with what's familiar which is a decimal numbers.

04:13.010 --> 04:18.610
Now before we jump into binary it is extremely helpful to understand how we manually perform addition

04:18.930 --> 04:22.210
and subtraction with decimal numbers.

04:22.450 --> 04:26.760
Now, because the logic is the same just with different digits and base values.

04:27.200 --> 04:35.440
And since this is a visual visual lecture, I will go with you with a step by step drawings as we go.

04:35.600 --> 04:38.920
Now in the decimal addition you already know.

04:39.080 --> 04:40.720
Let's start with a simple example.

04:41.000 --> 04:47.480
Now let's say we are adding 67 and 79 addition.

04:47.680 --> 04:56.360
Now we begin by adding the digits in the ones place right seven and nine which is 16.

04:56.680 --> 04:58.720
So that's more than ten.

04:58.720 --> 05:06.560
So we write down six and carry one to the tens place here.

05:06.840 --> 05:13.720
And now we have six from seven nine and plus one carry.

05:14.000 --> 05:18.640
What we have here is seven plus seven which is 14.

05:18.680 --> 05:24.840
Since we don't have any numbers here or here we will just end the subtraction.

05:24.840 --> 05:26.640
And that's our summary.

05:26.840 --> 05:32.430
And yeah that's basically the summary of this decimal addition.

05:32.710 --> 05:36.710
Now let's break down the algorithm that makes this work right now.

05:36.830 --> 05:40.630
Algorithm for for decimal addition.

05:40.870 --> 05:45.390
Now let's suppose x or let's use a different color.

05:45.390 --> 05:52.030
Here x and y are two n digit decimal numbers.

05:52.030 --> 05:55.750
Now we will add them digit by digit from right to left.

05:56.270 --> 06:00.070
Now let's call x I.

06:00.550 --> 06:10.470
This is the digit from the number x at position I and y I the digit from the number y at position I,

06:10.910 --> 06:14.630
and we have the carry and sum.

06:14.910 --> 06:16.990
And why does this work here?

06:17.270 --> 06:19.430
Because we are using positional notation.

06:19.630 --> 06:25.950
Every digit to the left here is worth ten times more.

06:26.310 --> 06:27.630
Here this 10th.

06:27.630 --> 06:29.470
And this is 100th.

06:29.670 --> 06:31.670
So and this goes on right.

06:31.910 --> 06:36.670
And that is how the addition algorithm works.
