1 00:00:07,590 --> 00:00:15,240 Who in this lecture, we're going to talk about Question 20, how does the binary number system work? 2 00:00:15,840 --> 00:00:24,390 The binary number system is used to represent numbers using only two digits, zero and one, for example, 3 00:00:24,540 --> 00:00:30,270 that this email number 13 is one one zero one in the binary number system. 4 00:00:30,690 --> 00:00:38,160 As you probably know, every piece of data is stored in the computer's memory as a series of bits beat 5 00:00:38,160 --> 00:00:43,980 is the smallest unit of information, and it can only have two values zero or one. 6 00:00:44,340 --> 00:00:48,810 This means every information we want to store in the computer's memory. 7 00:00:49,050 --> 00:00:56,820 A number string, a complex object, or an entire program is in the end stored as a series of zeros 8 00:00:56,820 --> 00:01:00,720 and ones in this lecture will focus on the numbers. 9 00:01:01,050 --> 00:01:08,030 As we mentioned, the binary number system represents numbers as the zeros and ones, so it fits perfectly 10 00:01:08,040 --> 00:01:10,800 how data is stored in a computer's memory. 11 00:01:11,160 --> 00:01:17,700 You may think that the binary number system is not something you need to understand, but it all happens 12 00:01:17,700 --> 00:01:18,630 under the hood. 13 00:01:19,020 --> 00:01:25,140 After all, there are programmers all around the world who have no idea how the binary number system 14 00:01:25,140 --> 00:01:27,180 works and they are doing fine. 15 00:01:27,720 --> 00:01:34,290 But there are a lot of aspects of programming that are affected by how binary number system work, and 16 00:01:34,290 --> 00:01:39,180 it's not possible to understand all of them without knowing the binary system at all. 17 00:01:39,630 --> 00:01:41,010 If you are not convinced. 18 00:01:41,130 --> 00:01:44,130 Let me give you a little spoiler from the next lecture. 19 00:01:44,430 --> 00:01:51,600 We will talk about a banking applications client can lose all protection granted by daily transaction 20 00:01:51,600 --> 00:01:57,600 limits, and in the end, have the account cleaned out by someone who accessed it illegally. 21 00:01:58,230 --> 00:02:03,870 It could be a void that if the programmer understood how operations on binary numbers work. 22 00:02:04,350 --> 00:02:10,980 All right, before we try to understand the binary number system, let's do so with the system we use 23 00:02:10,980 --> 00:02:12,090 on a daily basis. 24 00:02:12,240 --> 00:02:16,950 The decimal number system the base of the system is number 10. 25 00:02:17,310 --> 00:02:23,120 You must know that you can build a volume number system based on the number rather than zero. 26 00:02:23,280 --> 00:02:30,300 But 10 was probably the most natural for the human race as we have 10 fingers and we started our journey 27 00:02:30,300 --> 00:02:33,360 of understanding mathematics by counting them. 28 00:02:33,840 --> 00:02:36,570 Let's consider the following decimal number. 29 00:02:36,930 --> 00:02:39,360 You probably don't need much explaining here. 30 00:02:39,600 --> 00:02:41,490 I simply know what this number is. 31 00:02:41,700 --> 00:02:47,700 You can imagine what it means to have a $231 or any other currency. 32 00:02:47,700 --> 00:02:54,870 Use a folder with eight hundred and thirty one pictures or a book with eight hundred and thirty one 33 00:02:54,870 --> 00:02:55,560 pages. 34 00:02:55,950 --> 00:03:00,510 We are so used to the system that we don't even think about the numbers. 35 00:03:00,690 --> 00:03:03,990 We simply see them and know by instinct what they mean. 36 00:03:04,350 --> 00:03:06,060 But let's break it down. 37 00:03:06,420 --> 00:03:08,160 Each digit has its base. 38 00:03:08,460 --> 00:03:12,330 The further to the left it is, the more significant it is. 39 00:03:12,660 --> 00:03:16,620 It means it carries more weight of the number eight. 40 00:03:16,640 --> 00:03:20,260 Here means 800 free means 30. 41 00:03:20,340 --> 00:03:22,260 Why one simply means one. 42 00:03:22,710 --> 00:03:27,330 We could mark it digit with an index counting them from right to left. 43 00:03:27,610 --> 00:03:35,040 Now, for each of the digits, we want to calculate 10 to the power of the index multiplied by the digit 44 00:03:35,040 --> 00:03:35,730 itself. 45 00:03:36,270 --> 00:03:40,410 The sum of those numbers is the final number we want to represent. 46 00:03:40,770 --> 00:03:49,380 Let's make sure of that eight times 100 plus three times 10 plus one times one is 831. 47 00:03:49,770 --> 00:03:54,240 Also, remember that the number two the power of zero is one. 48 00:03:54,830 --> 00:04:02,340 No, it is clear why numbers most to the left are most significant because we multiplied by 10 to the 49 00:04:02,340 --> 00:04:04,470 largest power rate. 50 00:04:04,770 --> 00:04:08,640 We now understand exactly how did this small number system works. 51 00:04:08,970 --> 00:04:11,370 Let's move on to the binary number system. 52 00:04:11,730 --> 00:04:14,160 It actually works almost the same. 53 00:04:14,430 --> 00:04:17,280 The only difference is the base of the system. 54 00:04:17,610 --> 00:04:19,710 It will not be done, but two. 55 00:04:20,250 --> 00:04:23,430 Let's consider this number this time. 56 00:04:23,460 --> 00:04:29,940 You probably don't feel what this number means, but don't worry, we'll figure it out in a second. 57 00:04:30,330 --> 00:04:37,620 Let's start with the same as before by marking each digit with its index starting from the right in 58 00:04:37,620 --> 00:04:39,360 the decimal number system. 59 00:04:39,510 --> 00:04:45,090 We calculated the powers of 10 and then multiplied them by the digit itself. 60 00:04:45,300 --> 00:04:48,930 Here it's the same, but we calculate the powers of two. 61 00:04:49,530 --> 00:04:56,640 Let's calculate the sum it's eight plus four plus zero plus one would be 13. 62 00:04:56,940 --> 00:05:01,820 This means one one zero one in the binary number system is 13. 63 00:05:02,010 --> 00:05:02,580 Did this among? 64 00:05:02,640 --> 00:05:04,550 No system, right? 65 00:05:04,850 --> 00:05:10,880 This gives us the basics that are needed to understand some operations related to programming. 66 00:05:11,360 --> 00:05:17,930 The important thing to realize is that on a limited number of beats, we can store unlimited number. 67 00:05:18,290 --> 00:05:21,110 It's actually the same with the decimal number system. 68 00:05:21,470 --> 00:05:28,700 For example, the largest number we can represent with three digits is nine hundred ninety nine in the 69 00:05:28,700 --> 00:05:30,110 binary number system. 70 00:05:30,320 --> 00:05:32,150 Let's consider for beats. 71 00:05:32,540 --> 00:05:40,160 In this case, the largest number that can be represented is 15, because if each bit is set one, then 72 00:05:40,160 --> 00:05:44,900 the number is eight plus four plus two plus one, which is 15. 73 00:05:45,410 --> 00:05:50,390 Each numeric type in C-sharp occupies a certain number of bits in the memory. 74 00:05:50,720 --> 00:05:54,230 For example, an integer takes 42 bits. 75 00:05:54,530 --> 00:06:00,380 The largest number we can represent with end is this, which is only two more than two billion. 76 00:06:00,710 --> 00:06:02,570 And here is something interesting. 77 00:06:02,840 --> 00:06:07,670 This number is actually two to the power of 31, not 42. 78 00:06:07,970 --> 00:06:09,890 So what happened with one bit? 79 00:06:10,190 --> 00:06:15,230 Well, remember that with integers, we can also represent negative numbers. 80 00:06:15,560 --> 00:06:21,080 This one bit is saved to store information, whether the number is negative or not. 81 00:06:21,260 --> 00:06:24,590 Which leaves us 31 bits for the actual number. 82 00:06:25,070 --> 00:06:30,320 Here are sizes and ranges of the integral numeric types used in C-sharp. 83 00:06:30,770 --> 00:06:37,280 There is one more thing that we must understand since each numeric type because its size limit and it 84 00:06:37,280 --> 00:06:40,130 simply can't represent a number that is larger. 85 00:06:40,460 --> 00:06:47,180 What happens when some arithmetic operation exceeds this limit in such situations? 86 00:06:47,330 --> 00:06:49,520 Something quite interesting happens. 87 00:06:49,880 --> 00:06:58,310 For example, if I add two billion to two billion when operating on IANS, I will get this result in 88 00:06:58,310 --> 00:06:59,090 this lecture. 89 00:06:59,270 --> 00:07:01,460 I will explain to you how it works. 90 00:07:01,640 --> 00:07:08,030 But in the next one, we'll learn how to handle such situations when programming before we can understand 91 00:07:08,030 --> 00:07:09,840 what happens and why. 92 00:07:09,860 --> 00:07:11,330 The result is negative. 93 00:07:11,480 --> 00:07:14,870 We must understand how this binary numbers work. 94 00:07:15,110 --> 00:07:16,190 But as before? 95 00:07:16,280 --> 00:07:19,190 Let's start with decimal numbers for simplicity. 96 00:07:19,550 --> 00:07:22,820 You probably know this technique of adding numbers. 97 00:07:23,090 --> 00:07:30,680 If not, please read the article I linked in the resources attached to this lecture with binary numbers. 98 00:07:30,800 --> 00:07:32,030 It works the same. 99 00:07:32,360 --> 00:07:35,030 Let are binary far-reaching to binary. 100 00:07:35,030 --> 00:07:35,690 15. 101 00:07:36,050 --> 00:07:41,720 Remember, 13 is one one zero one and 15 is one one one one. 102 00:07:42,110 --> 00:07:45,770 First, we add the numbers from the first column to the right. 103 00:07:46,010 --> 00:07:50,510 One plus one is two, but we can't use two in the binary number system. 104 00:07:50,750 --> 00:07:53,300 This means we need to carry it over to. 105 00:07:53,300 --> 00:07:59,720 The next column will write zero in the first column of the result, because the module of the sum we 106 00:07:59,720 --> 00:08:05,720 calculate that would was to enter the base of the system, which also is to is zero. 107 00:08:06,260 --> 00:08:12,470 Now the second column again, the sum is two, so we carry over to the next goal. 108 00:08:12,930 --> 00:08:14,720 Again, the third column. 109 00:08:14,990 --> 00:08:16,610 Now this one is free. 110 00:08:16,880 --> 00:08:21,500 We carry over one to the next go on and we leave one in the result. 111 00:08:21,980 --> 00:08:28,460 This is because the modulo of the sum we calculated wood was free and the base of the system, which 112 00:08:28,460 --> 00:08:29,960 is two, is one. 113 00:08:30,620 --> 00:08:34,400 Finally, the Forbes.com: The sum is free again. 114 00:08:34,400 --> 00:08:38,240 So we carry over one and we leave one in the result. 115 00:08:38,630 --> 00:08:44,510 It turned out that we actually needed the fifth column to fit the one that we carried from the fourth 116 00:08:44,510 --> 00:08:45,050 column. 117 00:08:45,380 --> 00:08:46,850 This time it's simple. 118 00:08:47,240 --> 00:08:50,090 The sum is one, and we added to the result. 119 00:08:50,720 --> 00:08:52,970 All right, we have our result. 120 00:08:53,240 --> 00:08:57,650 It's 16 plus eight plus four plus zero zero. 121 00:08:57,740 --> 00:08:59,030 What is 28? 122 00:08:59,390 --> 00:09:03,620 This is correct because 13 plus 15 is also 28. 123 00:09:04,100 --> 00:09:06,620 But notice a very important thing. 124 00:09:06,890 --> 00:09:10,520 We needed to use one more digit to represent this number. 125 00:09:10,880 --> 00:09:14,330 Now let's go back to thinking about computers. 126 00:09:14,760 --> 00:09:20,900 If we had a numeric type which only has four beats, it would simply not be able to hold the result 127 00:09:20,900 --> 00:09:21,500 we had. 128 00:09:21,920 --> 00:09:23,510 So what would happen? 129 00:09:23,900 --> 00:09:30,240 Well, the last most significant bit would just be discarded as the actual result. 130 00:09:30,260 --> 00:09:38,330 The computer could see would not be one one one zero zero, which is 28, but one one zero zero, which 131 00:09:38,330 --> 00:09:44,000 is 12, something completely different and simply wrong from the arithmetic point of view. 132 00:09:44,450 --> 00:09:50,480 Now you know what adding two billion to two billion gave some weird number before. 133 00:09:50,780 --> 00:09:53,300 If you are curious why it was negative. 134 00:09:53,420 --> 00:09:57,260 Remember that the most significant bit represents a sign. 135 00:09:57,500 --> 00:10:02,490 So if it happens to be one, then C-sharp will interpret the whole number as something. 136 00:10:02,590 --> 00:10:08,650 Latif, because in this beat zero means positive numbers and one means negative numbers. 137 00:10:09,250 --> 00:10:09,970 All right. 138 00:10:10,360 --> 00:10:16,720 This lecture was touching very low level topics, but now you understand the basics of the binary number 139 00:10:16,720 --> 00:10:17,350 system. 140 00:10:17,680 --> 00:10:22,840 In the next lecture, we'll talk about how it affects our everyday programming. 141 00:10:23,440 --> 00:10:29,740 During the interview, you can be asked, for example, what is the decimal representation of number 142 00:10:29,740 --> 00:10:31,000 one zero one? 143 00:10:31,360 --> 00:10:38,050 Well, it's five, because it's two to the power of zero plus two to the power of two, which gives 144 00:10:38,050 --> 00:10:40,180 one plus four Woody's five. 145 00:10:41,180 --> 00:10:48,230 Why arithmetic operations in programming can give unexpected results, like, for example, adding two 146 00:10:48,230 --> 00:10:55,790 large integers can give a negative number because there is a limited number of bits reserved for each 147 00:10:55,820 --> 00:10:56,750 numeric type. 148 00:10:56,930 --> 00:10:59,900 For example, for integer, it's 32 bits. 149 00:11:00,350 --> 00:11:06,980 If the result of the arithmetic operation is so large that it doesn't fit on this amount of bits, some 150 00:11:06,980 --> 00:11:12,890 of the bits of the result will be trimmed, giving an unexpected result that is not right. 151 00:11:13,430 --> 00:11:14,120 All right. 152 00:11:14,540 --> 00:11:15,740 That's it for this lecture. 153 00:11:16,130 --> 00:11:18,830 Thanks for watching and see you in the next one.