Apna College - Binary Number System | DSA Series by Shradha Khapr..
Category
📚
LearningTranscript
00:00Hi everyone and welcome to the complete DSA series where we are going to cover our next
00:09chapter which will be regarding binary number system.
00:12Now this is a really important chapter because it will teach us a lot about how data is stored
00:17inside a computer's memory because we have already learned that data is stored in the
00:22form of binary numbers.
00:23So how can we derive and calculate those binary numbers, we will be learning from that in
00:29this chapter.
00:30Apart from this, if you want to learn any other topic related to data structures algorithm,
00:34then it is available on this channel in this playlist, so we can go and learn from there
00:38as well.
00:39So let's start learning about binary number system.
00:47So the number system that we have already seen before today, we had seen it in math
00:53which was our decimal number system.
00:59Now what is a number system?
01:02Number system is basically how many digits you are using for your calculation.
01:06Like in math, we use digits from 0 to 9, all the numbers we see in math, they are all formed
01:14using these 10 digits.
01:15So what happens here is that we have 10 digits in math and 10 is called dec and that is why
01:24the name of math number system which we use in our day-to-day life, whenever we talk about
01:28our numbers, we call it our decimal number system.
01:31But computers understand only two states, either 0 or 1.
01:37So the language of the computer is a mathematics type language, but it has only two digits
01:450 and 1.
01:46That is why the number system of computers is called a binary number system.
01:52Now it is not like that in number system, there are only two number systems that exist.
01:55There are other number systems as well.
01:57Like one number system is hexadecimal number system.
02:00Hexadecimal means 16.
02:03Hex means 6, dec means 10.
02:05So 6 plus 10 is 16.
02:07Another one is octal number system.
02:09Octal means 8.
02:11So like our decimal number system which we use in math, we also call it base 10 number
02:17system.
02:18Why base 10?
02:19Because it has 10 digits.
02:21Similarly, binary number system is called base 2 number system.
02:25Hexadecimal number system is called base 16 number system.
02:28Octal number system is called base 8 number system.
02:31Now we will not go into detail of hexadecimal and octal.
02:34But we study binary number system in detail so that we understand that how are our numbers
02:39actually treated inside a computer's memory.
02:42So when we talk about binary number system, it is clear that all the numbers in binary
02:47number system will always have 0 or 1.
02:51For example, if I write 1101001, then this number will be a binary number.
02:58But if I write 12396, then this number will be a decimal number.
03:04So there is a difference between binary and decimal numbers.
03:07And we can also convert the numbers of these two number systems into each other.
03:12So this entire chapter is going to be about this conversion.
03:17How we can convert our normal math numbers into binary numbers and vice versa.
03:22So first of all, let's learn about how to convert decimal numbers to binary numbers.
03:27How to convert simple math numbers like 1, 2, 3, 4, 5, 6 into binary numbers.
03:34Let's take an example for that.
03:36Let's suppose we start with the number 42.
03:38Now if I want to convert 42 into binary form,
03:43then I will have to do its repeated division with 2.
03:51I will have to divide my number repeatedly with whichever base I want to convert.
03:57How will we divide?
03:59Just like we take out LCM in math, the process will be similar.
04:03For example, we will divide 42 by 2.
04:05Remainder will be written on the right side.
04:08Remainder will be 0 here.
04:11And the answer will be 21.
04:13We will divide by 2 again.
04:15We will keep doing it until the remainder is 0.
04:17So the remainder will be 1.
04:19And the answer will be 10.
04:21Then we will divide by 2 again.
04:23Remainder will be 0.
04:25Quotient will be 5.
04:27Then we will divide by 2 again.
04:29Remainder will be 1.
04:31Quotient will be 2.
04:33Then we will divide by 2 again.
04:35Remainder will be 0.
04:37Quotient will be 1.
04:39Then we will divide by 2 again.
04:41Remainder will be 1.
04:43Quotient will be 0.
04:45When we write this number from bottom to top,
04:47i.e. backward,
04:49this number will be 101010.
04:51And this number is the binary form of 42.
04:55How do we represent it?
04:57We write it like this.
04:5942 base 10
05:01is equal to
05:03this number
05:05base 2.
05:08So 42 in which base?
05:10In 10 base.
05:12Decimal 42 is equivalent to this number
05:14of binary.
05:16So in this way,
05:18we can convert any decimal number
05:20to its binary form.
05:22Let's solve another example.
05:24Let's suppose now our number
05:26is equal to 50.
05:28We have 50 which we have to convert
05:30to its binary form.
05:32So we started with 50.
05:34We will divide 50 by 2.
05:37Remainder will be 25.
05:39Then divided by 2,
05:41remainder will be 1.
05:43Quotient will be 12.
05:45Then divided by 2,
05:47remainder will be 0
05:49Detached again.
05:51Quotient will be 1.
05:53Then we will divide by 2.
05:55Remainder will be 1.
05:57Quotient will be 0.
05:59This will be called algebra in hours.
06:01Now when omega is equal to 1,
06:03we can erase all these marks.
06:05then 1 and then 0. So this number in binary is equal to 50 of decimal. So in this way
06:14we can convert any decimal number in its binary form and it's very simple. So here
06:20we have to pause once and for your homework, not homework, you have to solve it right now.
06:26You have to convert 2 to 10 all of these numbers into their binary form. From 2 to 10, all
06:34these numbers have to be converted into their binary form and it has to be written on a
06:39paper. Later on, I will show you the binary form of some common numbers, so you can tally
06:45your answer with that answer. So later on, we will be looking at their values, so they
06:49have to be calculated right now. So in this way, our process remains. If we want, we
06:54can also write our own code for this whole process. So how to convert a decimal number
06:58into a binary number, let's talk about its code. Let's suppose we have any number.
07:03This number is basically our decimal number. In fact, we can also call it our decimal number.
07:09We have to convert any decimal number into our binary number. Let's take our answer.
07:15What will this answer be? This answer will be our binary number. We can initialize this
07:20answer with 0. So basically, what is the process of conversion? The process of conversion
07:26is that we divide the number by 2 again and again and get the remainder of that number.
07:32What I have to do again and again is that I have to divide the number by 2 and get the
07:36remainder of that number. So we have already seen this kind of process when we asked a
07:41question in the last chapter of functions to calculate the sum of digits of a number.
07:46So in the sum of digits of a number, I had to get the remainder again and again. So here
07:51we are doing something similar. Basically, I can run a loop until my decimal number is
07:57greater than 0. What will happen in this loop? First of all, we have to divide our decimal
08:04number by 2 and get the remainder. So first of all, let's get the remainder. Remainder
08:10is going to be my decimal number modulo 2. After that, the second step is that we have
08:16to actually divide that number by 2 and get the question. Remainder is already there,
08:21but we also need the question. So let's reset this decimal number to decimal number
08:28divided by 2. These two steps are very simple. Now I have the remainder. Now if we have to
08:35create the remainder from top to bottom, then it is a very simple straightforward process.
08:40First, we multiply this by 10. Then we multiply the answer by 10 and add this to it. Then
08:45we multiply the answer by 10 and add this to it. It is easy to make a forward number.
08:50It is a little difficult to make a backward number. But how do we calculate a backward
08:54number? So for that, basically, I will show you a small example. Let's suppose I have
08:59a number 5. If we convert 5 to binary, then its remainder is 1. So when 1 comes to me,
09:08we add it to our answer. There is only 1 in the answer. After that, the question will
09:13come 2. So we will divide it again by 2. Remainder comes 0 and this comes 1. Now this
09:18remainder, the second one, where do we add it? We add it just before the previous one.
09:23That means we are adding 0 before 1 because we have to build backwards from bottom to
09:28top. So first 0 should come and then 1 should come. Now if we divide 1 by 2 again, the
09:34question will come 0 and the remainder will come 1 again. Now this 1, let's bring it
09:40here. This last 1, we will add it before this 0. So what we are doing is, we are adding
09:47numbers backward. Because we need a backward number. So why not? If we are taking it out
09:52from the beginning, then add it backward. First push this 1 in the last, then push this
09:561 in 0, then push this 1 in this. So my final number will be 101. Now this number which
10:02I have taken out, if I say it in a very simple way, is this number not 1 into 1 plus 0 into
10:1110 plus 1 into 10 to the power 2? Can we say that this is the number? Of course we can.
10:18Because if this is the number, what did we learn in Maths? This is our first position,
10:23this is our 10th position, this is our 100th position. If something happens after this,
10:28this is our 1000th position. So in this way, the positions keep increasing. So can we
10:33write this same number like this? This will be equal to 100, this will be equal to 0,
10:38this will be equal to 1. And if we add these, the answer will be 101, i.e. our original
10:43value. So basically, when we are taking out a digit, then why not multiply this digit
10:48by 10 to the power something? So in the first unit, when it comes out for the first time,
10:54how will this be multiplied? This will be multiplied by 10 to the power 0, this will
10:59be multiplied by 10 to the power 1, and this third digit will be multiplied by 10 to the
11:04power 2. And in this way, as we go down, the powers of 10 keep increasing. So I hope
11:09we have understood how we can make a whole number from existing small digits. And how
11:14will we make that number? We will make that number by multiplying by the power of 10.
11:19So now I know that I have to take out my remainder. I have to multiply this remainder
11:24by the power of 10 and add it to the answer. So from here, I will have 1, from here I
11:29will have 0, from here I will have 100. The total answer will be 101, which is the correct
11:34answer. So every time I have to multiply by the power of 10 and add it to the answer.
11:39Then I have to update the power of 10. This is my job. So why don't we take one more
11:45variable, which we will call our power variable, which we will initialize with 1. This 1 is
11:52now 10 to the power 0. 10 to the power 0 is 1. We will update it later to 10 to the power
11:591, then update it to 2, then update it to 3 and keep updating it in this way. So here
12:04what we have to do is, I have to add to the answer, I have to multiply the remainder
12:11by the power of 10 and add it to the answer. And what we will do later is, we will update
12:18the power later. If we have to make 10 to the power 0 to 10 to the power 1, then we
12:23will have to multiply it by 10. If we have to take this power to the next one, then we
12:27will have to multiply it by 10. So we will multiply all the powers by 10. And this process
12:32will keep repeating again and again until my decimal number is greater than 0. And
12:38our final answer will be in this variable, which we can print. I will show you a process
12:46of dry run for a very simple number. Let's suppose n is equal to 6. Let's do it according
12:52to the code. According to the code, n is equal to 6. So first of all, we will take out the
12:56remainder of this. What will be the remainder? Here this is my answer, which is 0 right now
13:01and this is my power, which is also equal to 1. Now what will be the remainder? We will
13:06divide 6 by 2. The remainder of 6 by 2 will be equal to 0. And what will happen after
13:10n is updated? n will be updated to be equal to 3. So when we add 0 into 1 in the answer,
13:16our answer will still be 0. Now we will divide 3 and get the remainder. When we divide 3
13:21and get the second remainder, the remainder will be equal to 1. And our power will already
13:26be updated. The power is now equal to 10. So we will multiply the remainder by power
13:31into 10. The answer will be equal to 10 and we will store this 10 in the answer. So the
13:36answer is 10. And if we divide n more, then this final will be equal to 1. Our power is
13:43also updated to be 100. Next time, we will divide 1 by 2. The remainder will be equal
13:48to 1 and the quotient will be 0. Now we will multiply this third remainder with power.
13:53The answer will be 100. We will add 100 into the answer. We added 100 into the answer.
13:58So what is our answer? Our answer is 110. And 110 is equivalent for 6 of binary. So
14:10in this way, we have converted our conversion logic into code. So let's convert this logic
14:15into C++ code. Let's take a decimal number equal to 50. And let's make a function. Let's
14:24call this convertDecimalToBinary. In decimal to binary, we will get a decimal number. First
14:32of all, we need our answer. We will initialize it from 0. Along with that, we will take our
14:39remainder. Then we will run a loop until our decimal number is greater than 0. First of
14:46all, we need to get the remainder. So the remainder will be the decimal number. We will
14:52take the modulo with 2. Then we want to update the decimal number. After that, we have to
15:02add our remainder into power. And for the next iteration, we will update the power. We
15:09will multiply the power with 10. So the whole process will be done to calculate our number.
15:14And we can return our answer from here. What is this answer? This answer is the binary
15:19form of our number. So we can get the answer of decimal to binary from here. Let's save
15:29it and let's run. So for 50, we got 110010. This is what we calculated 110010. In fact,
15:38we can calculate all the numbers from 2 to 10. Or from 1 to 10, i is equal to 1, i less
15:45than or equal to 10, i plus plus. Every time we want to cout decimal to binary for this
15:54i. This will be printed for us. So the binary version of all the numbers from 1 to 10 is
16:04something like this. So it was a very simple straightforward logic. And this kind of logic
16:10also works in vice versa. Next, if we talk about binary to decimal conversion. For example,
16:17we have a binary number 101010. So how can we convert it into decimal form? To convert
16:24any binary number into decimal form, we multiply it with the power of 2. We go from right to
16:31left in this direction. First, we will multiply the digit with 2 to the power 0, second with
16:372 to the power 1, third with 2 to the power 2, fourth with 2 to the power 3, fifth with
16:422 to the power 4, and sixth with 2 to the power 5. And after multiplying all of them,
16:47we basically add them. For example, first of all, 0 will be multiplied with 2 to the power
16:520, so we will get 0. 0 can be multiplied with anyone. 1 will be multiplied with 2 to the
16:57power 1, so we will get 2. 0 will be multiplied with 2 to the power 2, so we will get 0. 1
17:02will be multiplied with 3 to the power 2, so we will get 8. 0 will be multiplied with 4
17:06to the power 2, so we will get 0. 1 will be multiplied with 5 to the power 2, so we will
17:10get 32. The answer is going to be 32 plus 8, it is going to be 42. And if we remember,
17:16earlier when we took it out for 42, the binary form was 101010. The same binary form was
17:21given to us here in the beginning. So, in this way, by multiplying right to left with
17:26the power of 2, if we keep taking the sum and adding it, we get our final decimal number.
17:35Let's run it on another example. For example, we have 110010. So, when this is multiplied
17:42with 2 to the power 0, it will give 0. When this is multiplied with 2 to the power 1,
17:46it will give 2. When this is multiplied with 2 to the power 2, it will give 0. When this
17:50is multiplied with 2 to the power 3, it will give 0. When this is multiplied with 2 to
17:54the power 4, it will give 16. When this is multiplied with 2 to the power 5, it will
17:59give 32. 32 plus 16, plus 2. What is the value? The value we have is equal to 50.
18:05So, if we remember, this was our binary form for 50, which we calculated last time.
18:10So, here we have to pause once. And once 1100101 is the decimal form of this number,
18:17which you have to calculate. Now, let's discuss the next code, how we can convert
18:22any decimal number from binary. For that, if we want, we can take any example.
18:27Let's suppose we have any binary number with which we have started. Binary number
18:32can be anything. First, we will calculate our answer, which is 0 in the beginning.
18:36This answer will be our decimal number. And along with that, we know that this time
18:41we have to multiply with powers of 2. So, we will take a power variable, which we
18:48will initialize with 1. 1 means 2 to the power 0. Now, what is our process?
18:53The process basically happens, if we have any binary number, let's suppose 101.
18:58First of all, I have to take out its last digit, which is going to be remainder.
19:03Then, what I have to do is, in the second step, I have to multiply this remainder with
19:09its power. Whatever the power is, 2 to the power something will be our power.
19:13So, I will call it POW. So, whatever the remainder is, I have to multiply it with
19:18power. And then, I have to add this to my answer. So, we will add this to our answer.
19:24This will be the second step. And then, we have to do two updations. What will be the
19:29updations? First, I have added 1 to my answer. Next time, 0 should be in the remainder.
19:34So, I will update my number. Number will be divided by 10. This is our updation.
19:40Second updation is that I will update the power also. In the beginning, power 2 to
19:44the power 0, then 2 to the power 1, then 2 to the power 2, then 2 to the power 3.
19:48So, every time, what will be my power? Every time, my power will be power multiplied by
19:532. This is also my updation. So, two steps are for my updation. And these two steps
19:58we wrote in the last function. And with these two steps, we perform our work.
20:02So, how will my function look? Till the time my binary number is greater than 0,
20:11I have to do these two things. First of all, remove the remainder. Remainder will be
20:15binary number modulo 10. And then, I have to add my binary number multiplied by power
20:22to the answer. Then, we will update. First, we will update the binary number.
20:31Binary number divided equal to 10. And apart from that, power will be power into equal to 2.
20:37So, these two steps are for updating. And in the last, we will return this answer.
20:44So, this will be our overall logic to convert any binary number into decimal.
20:49It is a very simple and straightforward logic. Let's convert it into code.
20:54Let's make a function in code. Integer. Let's call it binary to decimal.
21:01In binary to decimal, we will get a binary number. First of all, we will define our answer.
21:07And we will define our power. Till the time my binary number is greater than 0.
21:13Till then, what we have to do? Till then, first of all, we have to remove the remainder
21:18which is binary number modulo 10. Now, inside the answer, we have to add remainder into power.
21:27Then, we have to update. What will happen if we update?
21:30If we update, binary number is going to get divided by 10 and power is going to get multiplied by 2.
21:37In the last, we will return our answer which is the final decimal form of the number.
21:46Let's save it. And here, we will do cout. Basically, binary to decimal.
21:54Let's suppose, I want the decimal conversion of 101. So, this is going to be equal to 5.
22:01So, we have 5. If I want 1010, it will be 10.
22:07So, we will get 10. In this way, we can convert any number into binary to decimal.
22:13Let's see some common numbers whose binary forms we should remember.
22:17The first one is 0. The binary form of 0 is 0.
22:21In binary numbers, whether we write 0, 00, 000, or 40, all have the meaning of 0.
22:29For example, in math, whether we write 25, 025, 00025, all have the meaning of 25.
22:38Similarly, in binary numbers, the 0 in the beginning, you can add as many as you want.
22:44So, the binary form of 0 is 0, 1 is 1.
22:50We can also put 0001. The binary form of 2 is 103, 3 is 11, 4 is 100, 5 is 101, 6 is 110, 7 is 111, 8 is 1000, 9 is 1001, 10 is 1010.
23:07So, we should remember all the binary forms from 0 to 10.
23:10Now, there is a trick to get rid of these binary forms, which is not a trick, but a logical way, but we can also think of it as a trick.
23:19For example, I want to get rid of the binary form of 25. So, how to get rid of it?
23:24To get rid of the binary form of 25, basically there will be a lot of positions of 0 and 1, which I have to fill.
23:31Now, each position contributes the value of 2 to the power of 0.
23:38That is, it contributes the value of position 1, it contributes the value of 2, it contributes 4, it contributes 8, it contributes 16, it contributes 32, it contributes 64, it contributes 128, and so on.
23:50Now, in the binary form of 24, whichever bit will be 1, can we say that in the answer, when we are converting in decimal, the same value will be added in the answer.
24:00So, basically, in the powers of 2, we have to find those numbers that we can add to make 25.
24:06Now, I know that that number will not be greater than 25, because the power of 5, that is, by using 32, it cannot become 25.
24:13So, I know that I do not have to see so many values. I just have to fill this much space to get rid of the binary form of 25.
24:20Now, for the binary form of 25, what can I do? I can start looking at it with the greatest power.
24:25The biggest here is 16. What will I do? I will add 16 to the answer, so 1 will come here.
24:3016 will be added to the answer, so my remaining number is equal to 9.
24:34Now, 8 is also smaller than 9, so I will also add this to the answer, so my remaining number is equal to 1.
24:40Now, 4 is greater than 1, so we will not add this, we will not add 2 either, we will add 1.
24:45If we add 1, it becomes 1. 1-1 is equal to 0. My final answer becomes 0.
24:50That means I have added 16, plus 8, plus 1 to make my number, which is equal to 25.
24:56So, the binary form of 25 is 1-1-0-0-1.
25:00If we want, we can also write 0-0-0-0-0 as 1-1-0-0-1, this is also the correct answer.
25:07The initial zeros do not matter.
25:09So, in this way, we can make a binary form of any number by assuming the position.
25:15Let's look at another example.
25:17Let's suppose I want to find the binary form of 36.
25:21So, there will be different positions in this way, which I will have to consider.
25:25There will be 1, 2, 4, 8, 16, 32.
25:30I do not want numbers greater than 32, I do not want those which are greater than my number.
25:34So, I have included the biggest 32, added 1, it becomes minus 32, 4 is left.
25:4116 cannot be included, 8 cannot be included, 4 can be included.
25:45So, 1 is added here.
25:47Now, as soon as 4 is added, 0 is left.
25:49So, we will not add these two.
25:51So, the binary conversion of 36 will be this number.
25:55So, in this way, there is a small way to calculate binary numbers, which is completely logical.
26:00Because I know that every place is contributing some power, so on the basis of that power, I have calculated my number.
26:06Also, one more interesting thing which we should learn from here.
26:09Because in all these powers, when their sum is taken,
26:13then there is only one odd number, which is this 1.
26:17If we take the sum of even numbers, then the final result is always an even number.
26:22So, if we have to make an odd number, like 37, like 41, like 19.
26:30So, in this way, the binary conversion of odd numbers, the last bit will always be 1.
26:39Why? Because to make any number odd, all the other powers are contributing even.
26:45But if I have to make these numbers odd, then the last bit will always have to be 1, so that my final number can be odd.
26:53And to confirm that, what you can do is, by pausing here,
26:57you can calculate the binary form of 37, 41 and 19 with the help of this trick.
27:02And in that, you will notice that every time the last bit will always be 1.
27:08Otherwise, an odd binary number cannot be made.
27:10Apart from this, if we talk about binary number addition,
27:14whenever we want to add binary numbers, we can.
27:17For example, when 0 is added with 0, then it gives 0.
27:21When 0 is added with 1, then it gives 1.
27:23Or when 1 is added with 0, then also it gives 1.
27:26When 1 is added with 1 in binary,
27:29otherwise, if you add 1 in math, then it will become 2.
27:33But what is the binary form of 2?
27:34Binary form of 2 is 10.
27:36So, instead of 2, we should have 10.
27:39So, basically, when we add 1 and 1 in binary,
27:42our answer is equal to 1 and 0.
27:44And if there are more numbers on the left side,
27:47then what happens to 1?
27:481 gets carried.
27:49So, remember that in binary numbers, addition occurs like this.
27:54Specifically, when we are adding 1 with another 1.
27:58Now, next, we are going to talk about a special concept called 2's complement.
28:03For 2's complement, let's take an example of a number.
28:05Let's suppose I have a number equal to 10.
28:07I have defined an integer equal to 10.
28:10Now, this integer is equal to 10.
28:12But how will it be stored in the memory?
28:15The binary form of 10 is 1010.
28:18So, in the memory, I know how much?
28:20Instead of 4 bytes, the integer is allocated.
28:224 bytes means 32 bits.
28:24So, 32 such spaces will be allocated to a number.
28:30This total will become 32.
28:32In this, 1010 will be stored at the end.
28:36And in the first sub, we will get 0.
28:40So, in this way, a number of 32 bits will be formed,
28:43which is eventually stored in the memory.
28:45This is the way to store positive numbers.
28:48But here, instead of 10,
28:52if we had minus 10,
28:54then the way to store it in the memory would be different.
28:58To store it in the memory,
29:00we calculate a special form of it,
29:02which is called 2's complement.
29:04How to calculate 2's complement of any number?
29:07Let's learn that first.
29:09To calculate 2's complement of any number,
29:11first of all, we have to convert it into a binary form.
29:14What will be the binary form of number 10?
29:17The first step of number 10 is the binary form 1010.
29:21We have also written 1010 at the end.
29:24So, first of all, we will calculate it in 1010.
29:27After that, the second step is that
29:29the binary form of it,
29:31we will prefix it with a 0.
29:34For example, this is 1010.
29:37So, we will put a 0 in front of it.
29:39And this leftmost bit,
29:42we call it our most significant bit.
29:48In short, we call it MSB.
29:51And this is the most important bit of a binary number.
29:55Why is it most important?
29:57Because this bit tells us the sign of any number.
30:01That any number is positive or any number is negative.
30:04If MSB is 0, it means the number is positive.
30:08If MSB is 1, it means the number is negative.
30:12For example,
30:14value of both minus 5 and plus 5 is 5.
30:17But this sign tells us whether the number is positive or negative.
30:22Similarly, in any binary number,
30:24MSB tells us whether the number is positive or negative.
30:27So, first of all, we prefix MSB with 0.
30:30Now, this is a positive number.
30:32So, this is our MSB.
30:35After that, the third step is
30:37that we get the 1's complement of this number.
30:40Now, what is this 1's complement?
30:421's complement means that we reverse the whole number.
30:46Where there is 0, there will be 1.
30:48Where there is 1, there will be 0.
30:50So, in the second step,
30:52this was our first step in which we simply calculated the number.
30:56In the second step, we added 0 in front of our number.
30:59So, it became 0, 1, 0, 1, 0.
31:01In the third number, when we get the 1's complement,
31:04then this 0 will become 1.
31:06Then it will become 0. Then it will become 1.
31:08Then it will become 0. Then it will become 1.
31:10What did I do to each bit? I flipped each bit.
31:13To flip, we say to get the 1's complement.
31:16And the fourth step is that we add 1 to it.
31:20So, we will add 1 to it.
31:22This is the fourth step.
31:24So, what will be the final answer?
31:26We will add 1 to 1.
31:28So, 0 will come. Carry will become 1.
31:30We have already learned this in addition.
31:320 became 1. Carry became 1. So, 1 came.
31:34Here, 1, 0, 1.
31:36So, this will become our final form which will be stored in the memory.
31:39So, minus 10 of decimal is equal to 10110 of binary.
31:44This is our final answer.
31:47Also, in this answer, if we see MSB,
31:49what is MSB? MSB is 1.
31:511 means negative number.
31:53So, if we store this in the memory,
31:55then it is going to be a negative number.
31:57And this final form which we made after so many steps,
32:01this final form is called 2's complement of a number.
32:05Once complement is there,
32:07if we directly reverse it and add 1 to it,
32:09then it becomes 2's complement.
32:11And this whole process follows whenever
32:13we have to store a negative number in the memory
32:15in the binary form.
32:17Now, let's suppose we have already
32:19a given number. Let's reverse it.
32:21Let's suppose we have already
32:23a given number, binary number
32:25which is equal to this.
32:27I know that I have a binary number.
32:29And we already know that
32:31it is a negative number.
32:33So, how do we find out what our original number was?
32:35So, this is our process for negative numbers
32:37in which we convert decimal to binary.
32:41Now, let's talk about the process
32:43where we convert binary to decimal.
32:45And for this conversion, we should know
32:47that we have a negative number.
32:49Here, we know that we have a negative number.
32:51Only then we will be able to understand
32:53whether it is a part of number or a sign bit.
32:55It is MSB. So, it is a negative number
32:57which means it is our MSB.
32:59So, to convert negative numbers to decimal,
33:01we have to take out their 2's complement again.
33:03That is, first of all, we take out
33:05their 1's complement.
33:07What will be the 1's complement of this number?
33:09This number's 1's complement will be 01001.
33:11And after that, the second step is
33:13to add a plus 1 to it.
33:15So, if we add a plus 1 to it,
33:17what will be the number?
33:19The number will be 0.
33:21Here, 1 will be carried.
33:231, 0, 1, 0.
33:25And what is this number?
33:271, 0, 1, 0 is basically
33:2910 of decimal.
33:31And because I already knew
33:33that it is a negative number,
33:35I know that I have got minus 10.
33:37So, this value is equal to minus 10.
33:39So, in this way,
33:41any number can be converted
33:43from binary to decimal and from decimal to binary.
33:45So, let's solve a basic question on this.
33:47Let's suppose we have to convert
33:49minus 8 to binary
33:51and the reverse of it.
33:53So, first of all, we will convert minus 8 to binary.
33:55Minus 8 is already given.
33:57To convert this number to binary,
33:59first of all, we have to take out its binary form.
34:018 is its binary form,
34:03which is 10000.
34:05The second step is to add MSB to this binary form.
34:07So, MSB is added.
34:09So, it becomes 0100.
34:11The third step is to take out
34:13its 1's complement.
34:15Its 1's complement will be 1011.
34:19The fourth step is to add
34:21plus 1 to this.
34:23As soon as we add plus 1, it becomes 0,
34:25carry forward becomes 1.
34:27Again 0, carry forward becomes 1.
34:29And this becomes 1.
34:31So, the final answer will be
34:3311000
34:35in binary is equal to
34:37minus 8 of decimal.
34:39Now, how will we reverse this?
34:41If I have been given
34:43this value, 11000,
34:47and I have to find out
34:49which number it is.
34:51I already know that it is a negative number.
34:53So, the first step is to take out its 1's complement,
34:55which is 0111.
34:57The second step is to add plus 1 to it.
34:59So, here I have added plus 1.
35:01So, it becomes 0, carry forward becomes 1.
35:030, carry forward becomes 1.
35:05And this becomes 0.
35:07Now, 1100 is the binary form of
35:091108.
35:11So, the original number was minus 8.
35:13So, in this way,
35:15we can relate both the binary
35:17to decimal and decimal to binary conversion
35:19for our negative numbers.
35:21So, what we have processed
35:23is that we have taken out
35:252's complement.
35:272's complement means 1's complement plus 1.
35:29So, 2's complement is a very important
35:31concept whenever we talk about binary number system.
35:33So, here,
35:35the homework problem for you is that
35:37you have to first take out
35:39its binary form, i.e. 2's complement.
35:41Then, from that binary form,
35:43you have to take out minus 12
35:45and verify that the
35:47final form that you had taken out
35:49is giving us the correct reverse answer or not.
35:51So, this was
35:53the lecture of our binary number system.
35:55In today's lecture,
35:57we basically learned decimal conversion from binary.
35:59We learned binary conversion from decimal.
36:01We also learned codes of both.
36:03We also talked about
36:05the common decimal numbers
36:07whose binary conversions
36:09we should remember.
36:11Plus, I also taught you a trick
36:13on how to easily convert
36:15without actually using
36:17the LCM-like technique.
36:19In this way, we can find out
36:21how to add binary numbers.
36:23We also saw
36:25how to add 2's complement
36:27and 1's complement
36:29logics.
36:31This is an important logic that we should remember.
36:33So, we did a lot of things.
36:35In between, I gave you some homework problems
36:37which are very important to do.
36:39In future, we will study
36:41bitwise operators which work on
36:43binary numbers.
36:45They work on the binary form of numbers.
36:47So, to study bitwise operators,
36:49this understanding that we have built today
36:51is very important.
36:53Only then, we will be able to use bitwise operators
36:55efficiently.
36:57So, I hope that we have learned a lot of new things
36:59from today's lecture.
37:01You can mark your attendance in the comments
37:03and also on Twitter.
37:05The link is given in the description below.
37:07You can mention today's date and
37:09whatever concept you learned today
37:11which you liked the most
37:13and which you learned new, you can write it.
37:15That's all for today. See you in the next lecture.
37:17Till then, keep learning and keep exploring.