WEBVTT 1 00:00:01.720 --> 00:00:05.220 So let's now have a bit of a discussion about binary. 2 00:00:05.220 --> 00:00:07.690 What it is, and how you can actually use it. 3 00:00:07.690 --> 00:00:11.260 So binary is actually a number system just like decimal but 4 00:00:11.260 --> 00:00:16.440 whereas decimal is based on ten and uses the digits zero to nine binary 5 00:00:16.440 --> 00:00:20.750 is actually based on two and so therefore can only use the digits zero to one. 6 00:00:20.750 --> 00:00:24.460 So I'm just gonna show this little table on the screen. 7 00:00:24.460 --> 00:00:27.320 And in decimal, we're actually very familiar with 8 00:00:27.320 --> 00:00:31.570 the idea that the position of a digit represents its value. 9 00:00:31.570 --> 00:00:33.520 So you may remember doing hundreds. 10 00:00:33.520 --> 00:00:37.100 Tens and units in school to get used to that idea. 11 00:00:37.100 --> 00:00:40.470 So if we enter the digits into columns as you can see on the screen we 12 00:00:40.470 --> 00:00:43.310 can see that the column values are just powers of ten. 13 00:00:43.310 --> 00:00:46.310 So with the first column representing 10 to the power of 0, 14 00:00:46.310 --> 00:00:48.020 anything to the power of 0 is 1. 15 00:00:48.020 --> 00:00:50.170 In the second column is 10 to the power of 1. 16 00:00:50.170 --> 00:00:53.940 And anything to the power of 1 is itself and so on. 17 00:00:53.940 --> 00:00:56.630 So 10 squared, 10 cubed, etc. 18 00:00:56.630 --> 00:01:00.220 So, thus the 9 in the second column as you can see on the screen were 90, 19 00:01:00.220 --> 00:01:02.530 and the 9 in the third column is worth 900. 20 00:01:02.530 --> 00:01:06.840 So it's easy to see that the maximum number we can represent with four digits 21 00:01:06.840 --> 00:01:10.640 then is 9999, as you can see on the screen And 22 00:01:10.640 --> 00:01:13.920 if we need to store 10,000, then we have to go into a fifth column. 23 00:01:13.920 --> 00:01:17.040 So binary, as it turns out, works in exactly the same way. 24 00:01:17.040 --> 00:01:20.400 But because it's working in base two rather than base ten, 25 00:01:20.400 --> 00:01:23.410 the column headings are powers of two, they're not powers of ten. 26 00:01:26.660 --> 00:01:30.070 So as you can see in this screen, in binary each binary digit or 27 00:01:30.070 --> 00:01:31.970 bit is worth the power of 2, 28 00:01:31.970 --> 00:01:36.420 and it's corresponding to the column that it's in, just like in the decimal system. 29 00:01:36.420 --> 00:01:38.890 So 1 in the first column is still worth 1. 30 00:01:38.890 --> 00:01:41.580 In the next column it is worth 2, and so on. 31 00:01:41.580 --> 00:01:43.150 And as you can see from the slide, 32 00:01:43.150 --> 00:01:46.580 a byte where all the bits are 0 holds the value 0. 33 00:01:46.580 --> 00:01:50.130 If the first bit is one, then the value is one. 34 00:01:50.130 --> 00:01:53.800 So, as an example to represent decimal 47, 35 00:01:53.800 --> 00:01:59.445 we need 32 plus 8 which gives us 40 plus 4 which gives us a total of 44, and 36 00:01:59.445 --> 00:02:03.975 We need two, which gives us a total of 46, and then plus one, to give it 47. 37 00:02:03.975 --> 00:02:06.285 So in binary, the decimal value of 47 is 00101111, 38 00:02:06.285 --> 00:02:10.655 as you can see on the screen there. 39 00:02:10.655 --> 00:02:13.027 If every one of the first 8 bits is one. 40 00:02:13.027 --> 00:02:18.257 Now what we actually have is 128 plus 64 plus so on so forth. 41 00:02:18.257 --> 00:02:20.727 So it's easy in this case to look at the next column 42 00:02:20.727 --> 00:02:24.717 because we can actually have one less than it. 43 00:02:24.717 --> 00:02:25.567 Or there is 255. 44 00:02:25.567 --> 00:02:29.667 Now when we actually add 1 to 255 the number in the first column becomes 2. 45 00:02:29.667 --> 00:02:32.304 So we enter 0 and we actually carry 1. 46 00:02:34.350 --> 00:02:38.700 And on the next slide you can see that the number in the next column then becomes 2. 47 00:02:38.700 --> 00:02:42.810 So again we enter 0 and carry the 1 and so on. 48 00:02:42.810 --> 00:02:46.910 Once we reach the eighth bit what we do is set that to 0. 49 00:02:46.910 --> 00:02:49.420 And we have to move into another part. 50 00:02:49.420 --> 00:02:54.280 Now unlike a decimal where we don't have the concept of restricting the number of 51 00:02:54.280 --> 00:02:57.950 digits because we're not really trying to store decimal numbers in a computer or 52 00:02:57.950 --> 00:03:03.110 such, binary doesn't have that Or binary has a problem or has a restriction. 53 00:03:03.110 --> 00:03:04.120 Because binary, 54 00:03:04.120 --> 00:03:09.510 we actually have the concept of eight bits making a byte, by a eighth, hence byte. 55 00:03:09.510 --> 00:03:13.660 And because we're actually storing binary numbers in physical computer hardware, 56 00:03:13.660 --> 00:03:17.960 a decision had to be made early on for example As to how many transistors 57 00:03:17.960 --> 00:03:21.200 at the hyper level would be used in each memory location. 58 00:03:21.200 --> 00:03:23.450 So the number they eventually choose was 8. 59 00:03:23.450 --> 00:03:26.010 And as a result, when dealing with binary numbers, 60 00:03:26.010 --> 00:03:28.440 we have to consider a digit moving into the next byte. 61 00:03:28.440 --> 00:03:32.830 But it's nothing really more than starting a new column like we did in decimal 62 00:03:32.830 --> 00:03:33.910 when we added 1 to 9999. 63 00:03:33.910 --> 00:03:38.860 It also turns out it's actually very easy to display numbers in binary in Python. 64 00:03:38.860 --> 00:03:42.680 So I'm just gonna switch over to my Intellijay, IDE, and 65 00:03:42.680 --> 00:03:44.960 we're just gonna type a bit of code in. 66 00:03:44.960 --> 00:03:48.410 As I mentioned it's very easy to display numbers in binary in Python. 67 00:03:48.410 --> 00:03:51.310 Now with covered formatting strings and using replacement fields, 68 00:03:51.310 --> 00:03:55.410 the one thing we haven't mentioned is that you can add a number base to the format To 69 00:03:55.410 --> 00:03:59.460 display a number in binary ex decimal or [INAUDIBLE] which we'll look at shortly. 70 00:03:59.460 --> 00:04:03.390 So, the [INAUDIBLE] I'm about to type in will display the numbers from 0 71 00:04:03.390 --> 00:04:04.120 to 16 in binary. 72 00:04:04.120 --> 00:04:08.940 So we can type for i, in range, 17, and then 73 00:04:12.160 --> 00:04:17.265 print Is a replacement field zero column 74 00:04:17.265 --> 00:04:22.155 greater than two in 75 00:04:25.520 --> 00:04:30.770 is, and then we can top zero on the replacement field greater than O eight B. 76 00:04:30.770 --> 00:04:33.750 In closing off the field, top format I. 77 00:04:33.750 --> 00:04:36.840 And, I made an arrow there at the start of this line. 78 00:04:36.840 --> 00:04:39.020 However I can fix that or close off the replacement field. 79 00:04:39.020 --> 00:04:43.200 So what we're doing is we're showing the decimal equivalent 80 00:04:43.200 --> 00:04:47.270 of the field with the 2, and we're right align, right aligning to the value of i, 81 00:04:47.270 --> 00:04:48.540 then we're specifying a of 8. 82 00:04:48.540 --> 00:04:54.290 Over here the, the binary right align and adding beta with the value of i in binary. 83 00:04:54.290 --> 00:05:00.330 So if we actually run that You can see we're getting the right result. 84 00:05:00.330 --> 00:05:04.990 Zero in binary is 0000 one in binary is and so on and so forth, as per the slides. 85 00:05:04.990 --> 00:05:08.400 So looking at the pattern of the binary numbers from 0 to 16 is a good 86 00:05:08.400 --> 00:05:11.530 way to understand how binary numbers work. 87 00:05:11.530 --> 00:05:14.830 What I'm talking about is we can see the one moving across here. 88 00:05:14.830 --> 00:05:19.900 So we've got the value zero which is gonna be all zeroes, one has got the bit 89 00:05:19.900 --> 00:05:23.520 right most bit set and notice when we move to two the next bit gets set and 90 00:05:23.520 --> 00:05:28.860 then three the two is set, four we've got the next bit set and so on and so forth. 91 00:05:28.860 --> 00:05:31.580 You can also extend the range to print out high binary numbers so 92 00:05:31.580 --> 00:05:35.690 experiment with this and get a feel for how the binary numbers increase as you add 93 00:05:35.690 --> 00:05:38.690 one to it and that will really help you understand. 94 00:05:38.690 --> 00:05:41.240 The conversions that are actually happening when 95 00:05:41.240 --> 00:05:43.540 you're converting from decimal to binary. 96 00:05:43.540 --> 00:05:46.040 Now subtraction actually works the same as it does in decimal, so 97 00:05:46.040 --> 00:05:47.880 I'm going to move back to the next slide. 98 00:05:50.020 --> 00:05:53.330 So as I mentioned subtraction also works Works the same as it does in decimal. 99 00:05:53.330 --> 00:05:56.840 If there's not a number big enough in the current column 100 00:05:56.840 --> 00:05:58.740 then you can actually borrow from the next column. 101 00:05:58.740 --> 00:06:01.310 Now you'll be glad to know that we're not going to look at multiplication and 102 00:06:01.310 --> 00:06:02.420 division here. 103 00:06:02.420 --> 00:06:04.160 If you were to perform multiplication or 104 00:06:04.160 --> 00:06:09.260 division long hand the process is identical to what you would do in decimal, 105 00:06:09.260 --> 00:06:11.980 although you'd actually tend to have many more digits to deal with in 106 00:06:11.980 --> 00:06:16.190 binary just because we're dealing with a base-2 number system. 107 00:06:19.170 --> 00:06:22.000 So this next slide shows seven, which in binary is one, 108 00:06:22.000 --> 00:06:23.310 one, one as you can see on the screen there. 109 00:06:23.310 --> 00:06:27.670 And it shows up being multiplied by three or one, one in binary. 110 00:06:27.670 --> 00:06:30.150 And it shows you that long hand if you want to experiment with it, 111 00:06:30.150 --> 00:06:33.980 but you really don't have to perform arithmetic like this on binary numbers 112 00:06:33.980 --> 00:06:35.290 That's what we've got computers for. 113 00:06:35.290 --> 00:06:37.950 It's just good to have a sort of basic understanding of this 114 00:06:37.950 --> 00:06:41.080 without necessarily needing to know or to know it in detail. 115 00:06:41.080 --> 00:06:43.810 Now there's a few more operations that can be performed on 116 00:06:43.810 --> 00:06:48.320 binary numbers that are beyond the basic arithmetic that we're used to and 117 00:06:48.320 --> 00:06:49.960 understanding those can actually be useful. 118 00:06:49.960 --> 00:06:55.470 So I'm just gonna move to the next slide Now 119 00:06:55.470 --> 00:06:59.610 this slide shows what happens when you shift a binary number left. 120 00:06:59.610 --> 00:07:02.880 So just like a decimal when you want to multiply by ten, 121 00:07:02.880 --> 00:07:05.235 you insert a zero at the end of the Of the number. 122 00:07:05.235 --> 00:07:08.975 In binary the same thing, multiplies by two instead of ten. 123 00:07:08.975 --> 00:07:14.115 So shifting right divides by two just like it divides by ten in decimal. 124 00:07:14.115 --> 00:07:16.745 And as it turns out, you can have a binary point in binary as well. 125 00:07:16.745 --> 00:07:21.380 So 11 or 1011, as you can see on the screen there, 126 00:07:21.380 --> 00:07:24.940 Shift right would become 101.1 which is 5.5 in decimal. 127 00:07:24.940 --> 00:07:31.080 Now the final three operations we want to look at briefly are going to be or, 128 00:07:31.080 --> 00:07:33.810 and, and xall, which is short for exclusive all. 129 00:07:33.810 --> 00:07:35.100 The first thing or. 130 00:07:35.100 --> 00:07:39.090 So the binary or operation checks each bid and its two upper hands. 131 00:07:39.090 --> 00:07:42.290 And for each bid it actually sets the bid in the result to one 132 00:07:42.290 --> 00:07:45.170 if either of the upper hands had a one in that position. 133 00:07:45.170 --> 00:07:50.350 And probably the next lab will help you understand that. 134 00:07:50.350 --> 00:07:56.970 So this slide actually shows 4 or 8 giving 12 and then 12 or 11 giving 15. 135 00:07:56.970 --> 00:07:59.860 So that's how OR actually works. 136 00:07:59.860 --> 00:08:05.170 The next one, the AND operation, This sets each bit of the result to one, 137 00:08:05.170 --> 00:08:08.450 if the corresponding bit in both upper hands is one. 138 00:08:08.450 --> 00:08:09.780 So let's have a look at that in the next slide. 139 00:08:12.670 --> 00:08:17.600 So as you can see here, 12 and eight is eight because there's only one position 140 00:08:17.600 --> 00:08:21.950 that's got both a one, or that has a one in both 12 and eight as you can see there. 141 00:08:21.950 --> 00:08:26.750 So eight and seven is zero, as no positions contain a one in both numbers. 142 00:08:26.750 --> 00:08:31.620 And just moving on to the final slide, this is Exclusive OR, or XOR. 143 00:08:31.620 --> 00:08:32.670 And with this one, 144 00:08:32.670 --> 00:08:36.550 XOR sets the corresponding bit of the result to one if either but not 145 00:08:36.550 --> 00:08:40.810 both of the upper ANDs has a one in that position as you can see in this slide. 146 00:08:40.810 --> 00:08:43.480 Now, XOR is useful because if you repeat the operation, 147 00:08:43.480 --> 00:08:44.550 you get the original number back. 148 00:08:44.550 --> 00:08:45.900 Now we'll move on to the next slide. 149 00:08:48.980 --> 00:08:53.290 So in this one you can actually see 12 XOR eight is four or equals four and 150 00:08:53.290 --> 00:08:55.850 four XOR eight is equal to 12. 151 00:08:55.850 --> 00:09:00.070 Now this has got useful applications in cryptography, for example. 152 00:09:00.070 --> 00:09:03.910 And was also used to draw the cursor on early monochrome screens. 153 00:09:03.910 --> 00:09:08.570 By XORing each row of a character's eight bit representation on the screen The image 154 00:09:08.570 --> 00:09:11.730 would actually be inverted to represent the cursor being on that character. 155 00:09:11.730 --> 00:09:14.310 So XOR again restores the character. 156 00:09:14.310 --> 00:09:15.640 Now moving on to the last slide. 157 00:09:18.270 --> 00:09:21.890 This actually shows you a much smaller four by five character 158 00:09:21.890 --> 00:09:23.710 XOR in each row with one one one one. 159 00:09:23.710 --> 00:09:25.430 And you can sort of see what happened there. 160 00:09:25.430 --> 00:09:27.940 Now that's really all you need to know about binary at this stage, 161 00:09:27.940 --> 00:09:31.330 we're not really going to perform binary arithmetic manually 162 00:09:31.330 --> 00:09:33.350 Because we've got computers to do that. 163 00:09:33.350 --> 00:09:36.490 But, that said understanding how numbers are stored 164 00:09:36.490 --> 00:09:39.750 inside the computer is actually very useful especially when we have 165 00:09:39.750 --> 00:09:42.850 to consider how many bytes we have to allocate for storing our data. 166 00:09:42.850 --> 00:09:44.890 Now it also explains why, for 167 00:09:44.890 --> 00:09:49.120 example skey Cannot be used to represent all the characters that are used in 168 00:09:49.120 --> 00:09:54.210 the various languages around the world as ASCII characters must fit into one byte. 169 00:09:54.210 --> 00:09:57.830 Now Unicode resolves this by using two or four bytes per character and 170 00:09:57.830 --> 00:09:59.720 allowing many more characters to be represented. 171 00:09:59.720 --> 00:10:03.300 Now if some of that or all of that didn't make a great deal of sense, don't worry. 172 00:10:03.300 --> 00:10:05.990 It's useful to understand what's going on inside the computer 173 00:10:05.990 --> 00:10:08.820 There's not actually a central and the remainder of this course 174 00:10:08.820 --> 00:10:12.220 does not require you to actually understand the binary number system. 175 00:10:12.220 --> 00:10:14.050 So that's it, I'm going to finish the video here. 176 00:10:14.050 --> 00:10:17.980 In the next video we're going to start looking at hexidecimal and octal. 177 00:10:17.980 --> 00:10:19.340 So see you in the next video.